Statement Block {}

Control structures use conditions and loops to determine whether and how often a certain piece of code should be executed. The if-else structure, the while loop, the do-while loop and the for loop therefore each expect one or multiple statements after the condition to execute.

If there are multiple lines of code which should be executed, they are enclosed in curly brackets {} and are referred to as a code block (also block for short).


Single Statement


Block Statement 1
Block Statement 2
if(number > 10)
  printf("Single Statement");

if(number > 10){
  printf("Block Statement 1");
  printf("Block Statement 2");
}

A code block defines a Scope within which symbols can be defined:



New variable: 5
if(number > 10){
  int myNewVariable = 5;
  printf("New variable: %d\n", myNewVariable);
}

Details

Code lines belonging to a code block usually are indented by one tab step. This has no relevance to the compiler but serves as a visual aid for the programmer. This is especially useful when several single statements appear one after the other:








1, 1, 0
1, 0, 1
0, 1, 1
#include <stdio.h>

int main(){
  for(int z = 0; z < 2; ++z)
    for(int y = 0; y < 2; ++y)
      for(int x = 0; x < 2; ++x)
        if(x + y + z == 2){
          printf("%d, ", x);
          printf("%d, ", y);
          printf("%d\n", z);
        }
  return 0;
}

The notation without curly brackets is sometimes still used for small structures, but has the potential for errors when the code is expanded. The following example should turn all numbers positive but unfortunately a debug line was inserted before the sign reversal, which now causes the originally positive numbers to become negative.








-42 is positive
#include <stdio.h>

int main(){
  int number = 42;
  if(number < 0)
    printf("%d is negative", number);
    number = -number;
  printf("%d is positive", number);
  return 0;
}

Definition of Symbols

The curly brackets {} introduce a new Scope. Thus, local symbols can be declaraed and defined which are not visible outside the block.




i has value 50.

'i' undeclared
int number = 42;
if(number > 10){
  int i = 50;
  printf("i has value %d.\n", i);
}
printf("i has value %d.\n", i);

In the C language, variable definition are only valid within the curly brackets. A definition inside the condition is not possible:


syntax error
int number = 42;
if(int half = number / 2){
  printf("half value: %d\n", half);
}

Definition of Variables under C90

Older C standards require all variable definitions to be written at the beginning of a block. This restriction comes from the C90 standard and is valid for any code block, namely also the scope of a function. Starting from C99, that restriction was lifted.

The reason for this restriction was that older compilers translated files with a single pass (hence the name single-pass compiler). Executable code has to be placed in the so-called text-segment of the resulting binary but definitions of variables belong to the so-called data-segment. Single-pass compilers can not switch arbitrarily between those two segments, so they must not occur intertwinded in the code.

These restrictions of the C90 standard do no longer correspond to today's understanding of good programming style. Fortunately, starting with the standard C99, this is no longer a restriction. However, since the C90 standard was a long standing industry default and is till this day used for compatibility reasons with older code, many implementations have to face these challenges.