| Identifiers |
Sequence of letters and digits starting with a letter or underscore;
case sensitive.
|
| Comments |
/* Dit is commentaar! */
|
| Data types |
int
long int
long
short int
char
float
double
|
| Declaration of variables |
type variable_list;
int i;
char c;
float x, y;
|
| Assignment |
value = expression;
i = 2;
c = 'a';
x = x + 2 * y;
|
| Main program |
void main ()
{
int i, j;
i = 1;
j = 3;
i = i + j;
}
|
| Compound statement |
{
statement1;
. . .
statementn;
}
|
| Arithmetic operators |
Addition +
Subtraction -
Multiplication *
Division /
Modulo %
Increment ++
Decrement --
Unary + +
Unary - -
|
| Logical operators |
Less than
<
Less than or equal to <=
Greater than >
Greater than or equal to >=
Equal
==
Not equal
!=
Logical and &&
Logical or
||
Negation
!
|
| Reading formatted input |
scanf (formats_string, list_of_vars);
|
| |
scanf ("%d", &intvar);
scanf ("%4d%4.1f", &intvar, &floatvar);
|
| Writing formatted output |
printf (formats_and_text_string, list_of_vars);
|
| |
printf ("Waarde integer is: %d\n", i);
printf ("Waarde float is: %f\n", f);
|
| If statement |
if (expression)
statement_if_true;
|
| |
if (x < 1)
y = 2;
|
| |
if ( (x == 0) && (y != 0) )
{
y = 2;
x++;
}
|
| |
if (expression)
statement_if_true;
else
statement_if_false;
|
| |
if (x < 1)
y = 2;
else
y = 3;
|
| While statement |
while (expression) statement;
|
| |
while (x < 1) x++;
|
| |
while (x < 1)
{
x++;
}
|
| Repeat statement |
do
statements;
while (condition);
|
| |
do
x++;
y += x;
while (x <= 0);
|
| For statement |
for (expression1; expression2; expression3)
statement;
|
| |
for (i=1; 1<=10; i++)
printf ("%d", i);
|
| Case statement |
switch (expression)
{
case l1:
. . .
case lk: statementsl;
break;
. . .
case m1:
. . .
case mk: statementsm;
break;
default: statements;
break;
}
|
| |
switch (c = getchar())
{
case 'a':
case 'b':
case 'c': putchar ('1');
break;
case 'd': putchar ('2');
break;
default : putchar ('3');
}
|
| Read from file |
FILE *fin; /* file identifier /*
int m1k, m1r;
/* open input file */
if ((fin=fopen("nameOfFile", "r")) == NULL)
{ printf ("File bestaat niet!\n");
return (1);
}
/* read from input file */
fscanf (fin, "%d%d", &m1k, &m1r);
/* close input file */
fclose (fin);
|
| Write into file |
FILE *fout; /* file identifier /*
int m1k, m1r;
float v1k, v1r;
/* open output file */
if ((fout=fopen("nameOfFile", "w")) == NULL)
{ printf ("File kan niet geopend worden!\n");
return (1);
}
/* write into output file */
fprintf (fout, "%8d%8d", m1k, m1r);
fprintf (fout, "%8.2f%8.2f", v1k, v1r);
/* close output file */
fclose (fout);
|
| Testing for end-of-line |
while ((c = getchar()) != '\n') statement;
|
| Testing for end-of-file |
while ((c = getchar()) != EOF) statement;
|
| Function definition |
int max (int a, b)
{
return (a>b?a:b);
}
|
| |
void test ()
{
int i;
i = 2;
. . .
}
|
| Pointer variable definition |
type *identifier;
|
| |
int *pointerToInt;
int normalInt;
normalInt = 33;
*pointerToInt = normalInt;
printf ("Int value = %d\n", *pointerToInt);
|
| Dereferencing |
*identifier
|
| Pointer type |
typedef int * pint;
pint p;
|
| Nil pointer |
NULL
|
| Memory allocation |
type *identifier;
identifier = (type*) malloc (sizeof(type));
|
| Memory deallocation |
free ((type*)identifier);
|
| Array |
type array-identifier[size];
|
| Array type |
typedef knownID newID[size];
|
| |
typedef int VEC5[5];
typedef int VEC3[3];
typedef float FVEC[10];
VEC5 v5;
VEC3 v3;
FVEC f;
|
| Two-dimensional array |
type identifier[index1][index2];
|
| |
int x[2][3];
|
| |
/* dynamic allocation and use of matrix */
int **ptrToMat;
int k, r; /* loop variables */
int K = 5; /* number of columns */
int R = 6; /* number of rows */
/* allocate rows */
ptrToMat = (int **) malloc (R*sizeof(int));
/* allocate columns */
for (r=0; r<R; r++);
ptrToMat[r] = (int *) malloc (K*sizeof(int));
/* fill matrix row by row */
for (r=0; r<R; r++);
for (k=0; k<K; k++);
ptrToMat[r][k] = r*k;
|
| Two-dimensional array type |
typedef int TWO[3][4];
|
| |
typedef int SINGLE[5];
typedef SINGLE TWO[4];
|
| Structure variable |
struct
{ components;
} identifier;
|
| |
struct
{ int i;
float f;
} a;
|
| Structure type |
typedef struct type
{ components;
} type;
|
| |
typedef struct student
{ char name[30];
int StudNum;
} STUDENT;
typedef STUDENT CLASS[100];
CLASS Stats;
|
| Field access using pointers |
pointer -> field
|