具有 While 循环的有效 C++ 代码的等效 C 代码无法编译
The equivalent C code of a valid C++ code with a While loop does not compile
以下包含 while
循环的代码在 C++ 中编译。
#include <iostream>
using namespace std;
int main() {
while (int i = 5)
{
break;
}
return 0;
}
但是,如果在 C 中编译,以下等效 C 代码会导致错误:
#include <stdio.h>
int main() {
while (int i = 5)
{
break;
}
return 0;
}
编译器输出:
> 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)
为什么会这样?我试图在 C 语言中查找 while
循环的文档,但也没能找到它。
C 和 C++ 是不同的语言。 <iostream>
不是 C 库的一部分,using
和 namespace
只是 C++ 关键字。不要混合使用这些语言,因为它们完全不同。
此外,正如@sasquatch 提到的,在 C 语言中在 while
条件下声明变量是非法的。
您不应该期望 C++ 代码在 C 中编译。您也不应该期望相反的方式,因为 C 不是 C++ 的适当子集。
在 C 中,while 需要括号内的表达式。你所拥有的是变量的声明。您必须在循环之前声明变量,然后将表达式写为 i == 5 才能在 C 中编译。
This post 更详细地介绍了 C 与 C++ 相比的期望。他们为 if 解释的相同规则也适用于 while。
您上面的代码存在多个问题。
首先,您可能想要删除名称 space 和 header。它们甚至没有在代码中使用,也不能被 c 编译器读取。
其次,您不能在任何循环命令中定义变量类型。例如 while (int i != 5) and for (int i =0 ...) 在 c 中是无效的。您必须在循环之前定义变量。
最后,尽管我相信 c++ 标准允许您在 while 语句中定义变量,但我强烈建议您不要这样做。将比较语句与变量定义混合在一起会造成半混淆,即:int x、bool y、double z 等。
在 C++ 中,在 while
循环的每次迭代之前测试 condition
。来自 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.
而在 C 中,在 while
循环的每次迭代之前测试 expression
。来自 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.
C++ 和 C 中 while 语句的定义不同。
在 C++ 中,while 语句定义如下
while ( condition ) statement
条件又定义为
condition:
expression
attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause
attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list
如您所见,除了表达式之外,条件可能是带有一些初始值设定项的声明。 ibitializer的值被转换为bool
类型的表达式,并根据布尔值执行while语句
所以在你的C++程序中while语句条件中声明的初始值等于5
while (int i = 5)
因为它不等于零所以它被转换为布尔值 true
.
在 C 中,while 语句定义如下
while ( expression ) statement
如您所见,这里明确规定只能使用表达式。 C 不允许在 while 语句中使用声明。所以这个声明
while (int i = 5)
不会在 C 中编译。
这不是C++和C的唯一区别。例如下面这个条件运算符将在C++中编译,而不会在C中编译
int x = 10;
int y = 20;
( x < y ? x : y ) = 20;
或者这个语句会被C++编译,不会被C编译
int x;
int y = 20;
++( x = y );
下面的代码片段将在 C++ 和 C 中产生不同的结果
if ( sizeof( 'A' ) == 1 ) puts( "They are equal" );
else puts( 'They are not equal" );
或者考虑下面的例子
int x = 10;
void *vp = &x;
int *ip;
ip = vp;
此代码片段将在 C 中编译,不会在 C++ 中编译。所以你要小心了。
此外,C 和 C++ 的基本类型甚至不同。例如在 C 中有整数类型 _Bool
而在 C++ 中是不存在的。另一方面,在 C++ 中有类型 bool
和相应的布尔文字 false
和 true
在 C 中不存在。在 C++ 中有指针文字 nullptr
在 C 中不存在. 或者在 C 中有 C++ 中不存在的复合文字。或者在 C++ 中有基于 for 语句的范围,而 C 等中不存在。:)
以下包含 while
循环的代码在 C++ 中编译。
#include <iostream>
using namespace std;
int main() {
while (int i = 5)
{
break;
}
return 0;
}
但是,如果在 C 中编译,以下等效 C 代码会导致错误:
#include <stdio.h>
int main() {
while (int i = 5)
{
break;
}
return 0;
}
编译器输出:
> 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)
为什么会这样?我试图在 C 语言中查找 while
循环的文档,但也没能找到它。
C 和 C++ 是不同的语言。 <iostream>
不是 C 库的一部分,using
和 namespace
只是 C++ 关键字。不要混合使用这些语言,因为它们完全不同。
此外,正如@sasquatch 提到的,在 C 语言中在 while
条件下声明变量是非法的。
您不应该期望 C++ 代码在 C 中编译。您也不应该期望相反的方式,因为 C 不是 C++ 的适当子集。
在 C 中,while 需要括号内的表达式。你所拥有的是变量的声明。您必须在循环之前声明变量,然后将表达式写为 i == 5 才能在 C 中编译。
This post 更详细地介绍了 C 与 C++ 相比的期望。他们为 if 解释的相同规则也适用于 while。
您上面的代码存在多个问题。
首先,您可能想要删除名称 space 和 header。它们甚至没有在代码中使用,也不能被 c 编译器读取。
其次,您不能在任何循环命令中定义变量类型。例如 while (int i != 5) and for (int i =0 ...) 在 c 中是无效的。您必须在循环之前定义变量。
最后,尽管我相信 c++ 标准允许您在 while 语句中定义变量,但我强烈建议您不要这样做。将比较语句与变量定义混合在一起会造成半混淆,即:int x、bool y、double z 等。
在 C++ 中,在 while
循环的每次迭代之前测试 condition
。来自 C++ reference 的逐字记录:
condition
- any expression which is contextually convertible tobool
or adeclaration
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.
而在 C 中,在 while
循环的每次迭代之前测试 expression
。来自 C reference 的逐字记录:
expression
- any expression ofscalar
type. This expression is evaluated before each iteration, and if it compares equal tozero
, the loop is exited.
C++ 和 C 中 while 语句的定义不同。
在 C++ 中,while 语句定义如下
while ( condition ) statement
条件又定义为
condition:
expression
attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause
attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list
如您所见,除了表达式之外,条件可能是带有一些初始值设定项的声明。 ibitializer的值被转换为bool
类型的表达式,并根据布尔值执行while语句
所以在你的C++程序中while语句条件中声明的初始值等于5
while (int i = 5)
因为它不等于零所以它被转换为布尔值 true
.
在 C 中,while 语句定义如下
while ( expression ) statement
如您所见,这里明确规定只能使用表达式。 C 不允许在 while 语句中使用声明。所以这个声明
while (int i = 5)
不会在 C 中编译。
这不是C++和C的唯一区别。例如下面这个条件运算符将在C++中编译,而不会在C中编译
int x = 10;
int y = 20;
( x < y ? x : y ) = 20;
或者这个语句会被C++编译,不会被C编译
int x;
int y = 20;
++( x = y );
下面的代码片段将在 C++ 和 C 中产生不同的结果
if ( sizeof( 'A' ) == 1 ) puts( "They are equal" );
else puts( 'They are not equal" );
或者考虑下面的例子
int x = 10;
void *vp = &x;
int *ip;
ip = vp;
此代码片段将在 C 中编译,不会在 C++ 中编译。所以你要小心了。
此外,C 和 C++ 的基本类型甚至不同。例如在 C 中有整数类型 _Bool
而在 C++ 中是不存在的。另一方面,在 C++ 中有类型 bool
和相应的布尔文字 false
和 true
在 C 中不存在。在 C++ 中有指针文字 nullptr
在 C 中不存在. 或者在 C 中有 C++ 中不存在的复合文字。或者在 C++ 中有基于 for 语句的范围,而 C 等中不存在。:)