C error: Expected expression before int C error: Expected expression before int c c

C error: Expected expression before int


This is actually a fairly interesting question. It's not as simple as it looks at first. For reference, I'm going to be basing this off of the latest C11 language grammar defined in N1570

I guess the counter-intuitive part of the question is: if this is correct C:

if (a == 1) {  int b = 10;}

then why is this not also correct C?

if (a == 1)  int b = 10;

I mean, a one-line conditional if statement should be fine either with or without braces, right?

The answer lies in the grammar of the if statement, as defined by the C standard. The relevant parts of the grammar I've quoted below. Succinctly: the int b = 10 line is a declaration, not a statement, and the grammar for the if statement requires a statement after the conditional that it's testing. But if you enclose the declaration in braces, it becomes a statement and everything's well.

And just for the sake of answering the question completely -- this has nothing to do with scope. The b variable that exists inside that scope will be inaccessible from outside of it, but the program is still syntactically correct. Strictly speaking, the compiler shouldn't throw an error on it. Of course, you should be building with -Wall -Werror anyways ;-)

(6.7) declaration:            declaration-speciļ¬ers init-declarator-listopt ;            static_assert-declaration(6.7) init-declarator-list:            init-declarator            init-declarator-list , init-declarator(6.7) init-declarator:            declarator            declarator = initializer(6.8) statement:            labeled-statement            compound-statement            expression-statement            selection-statement            iteration-statement            jump-statement(6.8.2) compound-statement:            { block-item-listopt }(6.8.4) selection-statement:            if ( expression ) statement            if ( expression ) statement else statement            switch ( expression ) statement


{ } -->

defines scope, so if(a==1) { int b = 10; } says, you are defining int b, for {}- this scope. For

if(a==1)  int b =10;

there is no scope. And you will not be able to use b anywhere.


By C89, variable can only be defined at the top of a block.

if (a == 1)    int b = 10;   // it's just a statement, syntacitially error if (a == 1){                  // refer to the beginning of a local block     int b = 10;    // at the top of the local block, syntacitially correct}                  // refer to the end of a local blockif (a == 1){    func();    int b = 10;    // not at the top of the local block, syntacitially error, I guess}