The equivalent C code of a valid C++ code with a W

2020-08-09 04:53发布

问题:

The following code containing a while loop compiles in C++.

#include <iostream>
using namespace std;

int main() {
    while (int i = 5)
    {
        break;
    }
    return 0;
}

However, the following equivalent C code results in an error if compiled in C:

#include <stdio.h>

int main() {
    while (int i = 5)
    {
        break;
    }
    return 0;
}

Compiler output:

> prog.c: In function 'main': prog.c:5:9: error: expected expression
> before 'int'   while (int i = 5)prog.c: In function 'main':
> prog.c:5:9: error: expected expression before 'int'   while (int i =
> 5)

Why does this happen? I tried to look up the documentation for the while loop in C, but haven't been able to locate that either.

回答1:

C and C++ are different languages. <iostream> is not part of C library, and using and namespace are C++ keywords only. Don't mix the languages, as they are not at all the same.

Also, as @sasquatch mentioned, it is illegal in C to declare a variable in the while condition.

You should not expect C++ code to compile in C. You should also not expect the other way around, since C is not a proper subset of C++.



回答2:

In C++, a condition is tested for before each iteration of a while loop. Verbatim from the C++ reference:

condition - any expression which is contextually convertible to bool or a declaration of a single variable with a brace-or-equals initializer. This expression is evaluated before each iteration, and if it yields false, the loop is exited. If this is a declaration, the initializer is evaluated before each iteration, and if the value of the declared variable converts to false, the loop is exited.

Whereas in C, an expression is tested for before each iteration of a while loop. Verbatim from the C reference:

expression - any expression of scalar type. This expression is evaluated before each iteration, and if it compares equal to zero, the loop is exited.



回答3:

In C, the while expects an expression inside the parenthesis. What you have is a declaration of the variable. You would have to declare the variable before the loop and then write the expression as i == 5 to compile in C.

This post covers what C expects compared to C++ in more detail. The same rules that they explain for an if also applies to a while.



回答4:

There is a difference in the definition of the while statement in C++ and C.

In C++ the while statement is defined the following way

while ( condition ) statement

where in turn the condition is defined like

condition:
expression
attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause
attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list

As you can see apart from an expression the condition may be a declaration with some initializer. The value of ibitializer is converted to an expression of type bool and the while statement is executed depending on the boolean value.

So in your C++ program the value of the initializer of the declaration in the condition of the while statement is equal to 5

while (int i = 5)

As it is not equal to zero then it is converted to boolean true.

In C the while statement is defined the following way

while ( expression ) statement

As you can see yourself here is explicitly specified that only expressions may be used. C does not allow to use declarations in the while statement. So this statement

while (int i = 5)

will not be compiled in C.

It is not the only difference between C++ and C. For example this conditional operator below will be compiled in C++ and will not be compiled in C

int x = 10;
int y = 20;

( x < y ? x : y ) = 20;

Or this statement will be compiled in C++ and will not be compiled in C

int x;
int y = 20;

++( x = y );

The code snippet below will yield different results in C++ and C

if ( sizeof( 'A' ) == 1 ) puts( "They are equal" );
else puts( 'They are not equal" );

Or consider the following example

int x = 10;
void *vp = &x;
int *ip;

ip = vp;

This code snippet will be compiled in C and will not be compiled in C++. So you should be caution.

Moreover C and C++ have even different fundamental types. For example in C there is integer type _Bool that is absent in C++. On the other in C++ there is type bool and corresponding boolean literals false and true that is absent in C. In C++ there is pointer literal nullptr that is absent in C. Or in C there are compound literals that are absent in C++. Or in C++ there is the range based for statement that is absent in C and so on.:)



回答5:

There are multiple issues with the code you have above.

First off you may want to get rid of both the name space and the header. They are not even being used in the code and are not able to be read by the c compiler either.

Second, you cannot define variable types in any looping commands. For example while (int i != 5) and for (int i =0 ...) is invalid in c. You have to define the variable before the loop.

Finally, although I believe the c++ standard allows you to define a variable in a while statement, I highly suggest not doing so. Its semi confusing to have a comparison statement mixed with a variable definition ie: int x, bool y, double z, etc. etc.