Where you can and cannot declare new variables in C?

I also often hear that putting variables at the top of the function is the best way to do things, but I strongly disagree. I prefer to confine variables to the smallest scope possible so they have less chance to be misused and so I have less stuff filling up my mental space in each line on the program.

While all versions of C allow lexical block scope, where you can declare the variables depends of the version of the C standard that you are targeting:

C99 onwards or C++

Modern C compilers such as gcc and clang support the C99 and C11 standards, which allow you to declare a variable anywhere a statement could go. The variable’s scope starts from the point of the declaration to the end of the block (next closing brace).

if( x < 10 ){
   printf("%d", 17);  // z is not in scope in this line
   int z = 42;
   printf("%d", z);   // z is in scope in this line
}

You can also declare variables inside for loop initializers. The variable will only exist only inside the loop.

for(int i=0; i<10; i++){
    printf("%d", i);
}

ANSI C (C90)

If you are targeting the older ANSI C standard, then you are limited to declaring variables immediately after an opening brace1.

This doesn’t mean you have to declare all your variables at the top of your functions though. In C you can put a brace-delimited block anywhere a statement could go (not just after things like if or for) and you can use this to introduce new variable scopes. The following is the ANSI C version of the previous C99 examples:

if( x < 10 ){
   printf("%d", 17);  // z is not in scope in this line

   {
       int z = 42;
       printf("%d", z);   // z is in scope in this line
   }
}

{int i; for(i=0; i<10; i++){
    printf("%d", i);
}}

1 Note that if you are using gcc you need to pass the --pedantic flag to make it actually enforce the C90 standard and complain that the variables are declared in the wrong place. If you just use -std=c90 it makes gcc accept a superset of C90 which also allows the more flexible C99 variable declarations.

Leave a Comment