在C中使用for循环时出错

发布于 2024-12-06 11:15:31 字数 234 浏览 4 评论 0原文

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-12-13 11:15:31

它在 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.

掩耳倾听 2024-12-13 11:15:31

是的。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文