Home > Blockchain >  "if" statement syntax differences between C and C
"if" statement syntax differences between C and C

Time:07-03

if (1) int a = 2;

This line of code is valid C code (it compiles at the very least) yet invalid C code (doesn't compile). I know there are differences between the languages but this one was unexpected.

I always thought the grammar was

if (expr) statement

but this would make it valid in both.

My questions are:

  1. why doesn't this compile in C?
  2. why does this difference exist?

CodePudding user response:

Chris's answer (and others) shows how the grammar is different.

I want to point out that if (1) int a = 2; doesn't make sense in C but it does in C . Since we don't have a block but just 1 statement/declaration there is no possible further use of the variable declared (it goes out of scope immediately). In C this would make no sense to allow, but in C constructors and destructors can have side effects so defining and initializing a variable that goes of of scope immediately can be useful and must be allowed.

CodePudding user response:

This is a subtle and important difference between C and C . In C any statement may be a declaration-statement. In C, there is no such thing as a declaration-statement, and instead a declaration can appear instead of a statement within any compound-statement.

From the C grammar (C17 spec):

compound-statement: "{" block-item-listopt "}"
block-item-list: block-item | block-item-list block-item
block-item: declaration | statement

From the C grammar (C 14 spec):

compound-statement: "{" statement-seqopt "}"
statement-seq: statement | statement-seq statement
statement: ... | declaration-statement | ...

It is not clear why this difference exists, it is just the way the languages evolved

CodePudding user response:

It's because C and C define statement differently.

In C, declarations are not classified as statements. A C compound statement consists of an opening {, an option list of block-items, and a closing }, where a block-item either a declaration or a statement. (This was changed in C99, when C added the ability to mix declarations and statements within a block.)

In C , a declaration is classified as a statement (but only if it's inside a compound statement). This allows a simpler definition of a compound-statement: it's a {, followed by an optional list of statements, followed by a }.

The difference doesn't have much practical effect; it's always possible to work around it. One effect is that it's legal in C for a declaration immediately follow a case label, but in C it's not legal.

CodePudding user response:

In C, a declaration and a statement are distinct entities.

In C , a a subset of declarations called a block-declaration is a type of statement, specifically it is a declaration-statement. These include simple declarations like int a=2.

CodePudding user response:

it is not allowed in c language you can just

{
        int variable;
        variable = some_function();
        if (variable) return 1;
    }
/* variable is out of scope here */
  •  Tags:  
  • c c
  • Related