Académique Documents
Professionnel Documents
Culture Documents
Comments
/* a comment */
/* And this
is
another comment */
Programmers insert comments to document a program and improve program
readability. Comments are not part of the executable program, they are ignored by the
C compiler. A good programming practice is to have comments in the program. You
can choose a particular style to highlight comment, for example
/********************************************
* This program generates random numbers in *
* the range 0 - 100 and determines and *
* prints the maximum and minimum numbers. *
********************************************/
int main()
{
...
return 0;
}
Constants
A constant is a value that does not change during the program execution.
Examples of constants are integer numbers such as 0, 10, 23987. These are integer
constants. There are also floating constants, like 3.14159, 2.0, and character constants
like 'a', 'B', etc. Character constants must be included in single quotes and in C
language they are closely related to integers. Any sequence of characters written
between double quotation marks is a string constant. A string constant differs from a
character constant. For example, 'A' and "A" are not the same.
If you use the same constant in your program more than once, usually you will define
it using the #define preprocessor directive.
The #define directive tells the preprocessor to replace every occurence of a particular
character string which is called a macro name with a specified value called a macro
body.
For example, you may define the maximum number of input values as
#define MAXNUMBER 10
Also you may consider to define the logical operators to make the program more
readable:
#define TRUE 1
#define FALSE 0
#define NULL 0
#define AND &&
#define OR ||
#define NOT !
if(pass==TRUE)
printf("You passed this subject");
Variables
i = 1;
the value of a constant 1 is assigned to the variable i. Variable names and function
names in C are called identifiers. To make a valid variable name you can use all the
following:
int number;
char c;
float price;
To declare a variable means to specify its type, for example, integer, with a keyword
int, (types of variables will be discussed later) and name. Variables of the same type
can be declared in one line.
To assign a value to the variable, an assignment operator (=) is used, for example i =
1; number = 100; . A variable can be initialised (that is, given an initial value) at the
same time as it is declared.
int number = 0;
char c = 'a';
float price = 2.5;
Expressions
C arithmetic operators:
Symbol Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder or modulus
The last operator in this list requires some explanation. The remainder operator % is
used to obtain the remainder of the division of the first operand by the second
operand. For example, 7 % 3 is 1, 8 % 4 is 0, and 5 % 7 is 5
(2+7%2)+45/15-5 = ?
The solution can be obtained if you execute each operation in sequence, taking into
account the precedence rules:
(2+1)+3-5 = 3+3-5 = 1.
Statements
i = 1;
number = (n1 + n2)*12;
hours = seconds/3600;
return 0;
You learned from the example in Program 1 that a function call is also a statement.
while(count != MAX)
{ /*block of statements within the while
loop*/
sum = n1 + n2;
diff = n1 - n2;
if(diff > 0) /* single statement after if */
printf("The first number is larger that the second.\n");
}
Functions
The default function type of main() function, for example, is an integer. That is why
the C compiler gives you a warning message if the main() function does not have a
return(0); statement at the end. In the example above, the function multiply_int()
returns integer value, which is a value of the integer variable result that is declared in
the first line of the function body.
If a function does not return any value the keyword void is used.
void print_error_message(void)
{
printf("Invalid input. Try again.");
}
This function prints the message on the screen. The first void means that it does not
return any values. The second occurance of the word void indicates that the function
does not take any parameters.
2. The function name should reflect what the function does. For example, the name of
the C standard library function printf() indicates that it "prints formatted data". If
you encounter a function with the name calculate_average(), you would expect that
this function calculates an average value of a set of numbers.
A function name is an identifier, and the same naming rules are applied to names of
functions as to names of variables.
3. A function often requires some information to be passed to it. For example, the
multiply_int() function takes two parameters of integer type. The argument list (or
parameter list) is given in the brackets following the function name, and includes a
list of the types and names of all arguments. Type and name pairs are separated by
commas. The name of a parameter to the function within the function declaration is
called formal, to distinguish it from the actual parameter name, which you use when
making a function call.
If a function does not take any parameters, the keyword void can be used, or the
brackets after the function name remain empty, see, for example, the function
print_error_message() above.
To execute a function you make a function call from the other function body. The
function call includes the name of the function and actual parameters in brackets:
function_name(parameter1, parameter2, ...);. For example, the call for the
function multiply_int() can be done as follows.
Program 2.1.
#include <stdio.h>
int main()
{
int product;
Output:
A variable declared within a block has a block scope, it is also called a local
variable. A local variable can be accessed only within the block where it was
declared. For example, because the variable result is declared within the
multiply_int() function, it cannot be refered to from the main() function.
Summary: