Académique Documents
Professionnel Documents
Culture Documents
main()
{
local variables to function main ;
statements associated with function main ;
}
f1()
{
local variables to function 1 ;
statements associated with function 1 ;
}
f2()
{
local variables to function f2 ;
statements associated with function 2 ;
}
Case Sensitive
• Case matters in C.
A is not a
Structure of my first ‘C’ program
/* This is
a comment */
// This is a one-line comment
void main()
{
int x;
x=5;
x=add1(x);
printf("%d“,x);
Returns an integer result
}
Expects an integer parameter
int add1(int i)
{
int y;
y=i+1;
return (y);
}
Bitwise Operators
<< left shift
>> right shift
| bitwise OR
& bitwise AND
^ bitwise XOR
~ bitwise NOT
Bitwise operators (contd.)
AND: z = x & y OR: z = x | y
x 0 1 0 1 x 0 1 0 1
y 0 0 1 1 y 0 0 1 1
z 0 0 0 1 z 0 1 1 1
NOT: z = ~x XOR: z = x ^ y
x 0 1 x 0 1 0 1
z 1 0 y 0 0 1 1
z 0 1 1 0
Shift operators
Right shift by 5 unsigned
if (x==1)
{
printf(“Hello”);
printf (“hi\n”);
}
else
{
printf(“World’);
}
Conditional Loops
while (expression) statement
while (x<1000)
{
x++;
y=y+10;
}
Conditional Loops
do statement while (expression)
x = 0;
do
{
x++;
} while (x<1000);
Unconditional Loops
for( initialization; expression; increment )
statement
for (i=0;i<100;i++)
{
printf("Hi.");
}
The switch statement
The C switch allows multiple choice of a selection of items
at one level of a conditional where it is a far neater way
of writing multiple if statements
switch (expression)
{ case item1:
statement1;
break;
case item2:
statement2;
break;
case itemn:
statementn;
break;
default:
statement;
break;
}
Arrays
• Declaration:
int x[10];
double y[15];
x[5]=10;
char c[15];
• Note arrays start with 0 element
Pointers
• A pointer in C is the address of something
• The unary operator `&' is used to produce
the address of an object
int a, *b, c; // b is a pointer
b = &a; // Store in b the address of a
c = *b; // Store in c the value at
// address b (i.e., a)
• Pointers to pointers
• Pointers to functions
Character Pointers & Arrays
char *y;
char x[100];
y = &x[0];
y = x; // Does the same as the line above
*(y+1) gives x[1]
*(y+i) gives x[i]
Structures
• User defined ‘data type’
struct emprec
{
char name[25];
int age;
int pay;
};