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 declarati
According to cppreference, C++ includes following types of statements
:
While C considers following types of statements
:
As you can notice, declarations are not considered statements
in C, while it is not this case in C++.
For C++:
int main()
{ // start of a compound statement
int n = 1; // declaration statement
n = n + 1; // expression statement
std::cout << "n = " << n << '\n'; // expression statement
return 0; // return statement
} // end of compound statement
For C:
int main(void)
{ // start of a compound statement
int n = 1; // declaration (not a statement)
n = n+1; // expression statement
printf("n = %d\n", n); // expression statement
return 0; // return statement
} // end of compound statement