在C中使用for循环时出错
for ( int iIdx = 0; iIdx < argc; ++iIdx )
_tprintf( TEXT( "Arg %d: %s\n" ), iIdx, argv[ iIdx ] );
_tprintf( TEXT( "\n" ) );
这在 C 中有效吗?因为当我尝试编译它时出现错误,如果我从 for 循环的初始值设定项部分中删除 int,它会编译得很好......
for ( int iIdx = 0; iIdx < argc; ++iIdx )
_tprintf( TEXT( "Arg %d: %s\n" ), iIdx, argv[ iIdx ] );
_tprintf( TEXT( "\n" ) );
Is this valid in C? Because I get an error when I try to compile it, if I remove the int from the initializer section of the for loop, it compiles fine...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它在 C99 之前的 C 语言中无效。
在 C89/90 及更早版本中,声明需要位于每个块的开头。您不能将声明和普通代码交错。
for
内的声明不算在块的开头。It is not valid in C before C99.
In C89/90 and earlier, declarations need to be at the start of each block. You can't interleave declarations and normal code.
A declaration inside the
for
does not count as being at the start of a block.是的。 Microsoft 的 C 编译器 (
cl
) 不支持现代 C (C99) 。像这样的 For 循环初始值设定项是 C99 中的新增内容。Yes. Microsoft's C compiler (
cl
) does not support modern C (C99). For loop initializers like that are new in C99.