Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declarations/definitions as statements in C and C++

I was confused when this wouldn't compile in C:

int main() {     for (int i = 0; i < 4; ++i)         int a = 5; // A dependent statement may not be declaration      return 0; } 

I'm used to C++ where this will compile. I just stared dumbfounded for a while until I remembered an answer here on SO about how in C and C++ different things are considered "statements". This was in regard to a switch statement. A "statement" after the for loop brackets must be present both in C and C++. This can be done in both either adding a semicolon or creating a { } squiggly bracket block.

In C++ "int a = 7;" is considered a declaration, definition, and initialisation. In C I believe it is also considered all of these, however in C it is not considered a "statement".

Could someone exactly clarify why in C this isn't a statement whereas in C++ it is? This is confusing my concept of what a statement is, because one language says it is, and another says it isn't, so I'm kind of confused.

like image 224
Zebrafish Avatar asked Apr 16 '18 16:04

Zebrafish


People also ask

Is a declaration a statement in C?

A declaration is a C language construct that introduces one or more identifiers into the program and specifies their meaning and properties.

What are declarations and definitions in C?

The main difference between Declaration and Definition in C is that declaration of a variable indicates the compiler about the name and the type of the variable, while the definition of a variable indicates the compiler where and how much storage to create for a variable.

What is a declaration statement?

What is a Declaration? A declaration is a written statement, sworn to be the truth under penalty of perjury by any person who has direct knowledge about the issues in a court case.

What is difference between declaration and definition?

Declaration means that variable is only declared and memory is allocated, but no value is set. However, definition means the variables has been initialized. The same works for variables, arrays, collections, etc.


1 Answers

C++ allowed that the "substatement" of an iteration statement was implicitly a compound statement ([stmt.iter])

If the substatement in an iteration-statement is a single statement and not a compound-statement, it is as if it was rewritten to be a compound-statement containing the original statement. Example:

while (--x >= 0)    int i; 

can be equivalently rewritten as

while (--x >= 0) {    int i; } 

the C standard does not have this language.

Additionally, the definition of a statement changed in C++ to include a declaration statement, so even if the above change wasn't made, it would still be legal.


The reason that adding braces makes it work is because your declaration now becomes a compound-statement which can include declarations.

You are allowed to have an identifier in a loop body without braces, so you can do this instead:

int a = 5; for (int i = 0; i < 4; ++i)     a; 
like image 191
AndyG Avatar answered Oct 02 '22 14:10

AndyG