Section Navigation

General Syntax

The C language is a procedual language, meaning that the program flow follows a predefine order of commands. Although most programs allows different behavior, depending on information only available during run time, all posible branches have to be defined during compilation time.

Example: The user gives a number as input and the program is supposed to print out whether the number is odd or even. Because the number is supplied by the user, the value is not known during compilation time, the time where a working program is created from the source text. Therefore the source text has to cover both possibility (printing on screen that the number is even and printing on screen that the number is odd) plus the command section, which is doing the check and then branches in one of the possiblity.

It is clear that writing a program is like creating a 'tree' with all the different branches for all the possibility the program can offer. During run time the program follows only one single path along the tree. It will becomme clear in the following sections.

The source code, from which the compiler creates an executable (the actual program) is a list of statements, which falls in one of the following categories:

Because the C-compiler ignores all formating of the source text, such as line breakes or white spaces (more than one spaces between printable characters), ends of commands and declarations have to be indicated with semi-colons and a set of statements are grouped with curly brackets. So the following two sections of source code are completely indentical for the compiler:

{ a=10; printf("Hello World\n"); }

and

{
a = 10 ;
printf("Hello World\n") ;
}

The meaning of the commands becomes clear in the following sections (it asign the value '10' to a placeholder (variable) with the name 'a' and then print the string 'Hello World' on the screen). Note that C is case-sensitive, so that the compiler distinguish between the variable a and A. However it is good practice not to use both variables name, because it is a frequent source of errors and makes the code harder to understand.

The top most grouping in the source code are functions. There must be one function with the name main, which is the starting point of the program (aka the root of the tree). Functions are explained in detaill in one of the next sections, so we give here only the minimal syntax from the main-function which is the function name (here main), an empty pair of round brackets as the signature for a function and a curly bracket, which holds then the actual commands/declaration/flow control statements.

So a minimal program is

main()
{
printf("Hello World\n");
}

next