用VC++编译C程序[2005]

标签 c visual-c++

我可以看到我的 VC++ 2005 速成版完美地编译了以下代码

/*Following Code compiles fine*/
#include <stdio.h>

main()
{
    int i;
    int;
    5;

    printf("\n\r Whats up??");
    getchar();
    return 0;
}

但是我观察到,对于顺序稍有不同的相同代码,它会产生编译错误。

/*Following code gives error*/

#include <stdio.h>
main()
{
    int;
    5;

    int i;
    printf("\n\r Whats up??");
    getchar();
    return 0;
}

“int i”的位置在这两种情况下都不同。 我收到错误消息“错误 2 错误 C2143:语法错误:缺少‘;’在第 6 行的“类型”之前。

为什么会这样?

谢谢,

最佳答案

发生这种情况是因为编译器似乎有错误。第一个程序和第二个程序都不应该被编译。

我猜第二个程序没有被编译,因为旧 C 中的声明必须放在代码块的开头。

我手边没有旧的 C 标准,但根据当前的 C 标准(6.7 声明)

2 A declaration other than a static_assert declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.

似乎旧的 C 允许空声明。至少在 Rationale for International Standard— Programming Languages— C Revision 2 20 October 1999 中写道:

6.7 Declarations 25 The C89 Committee decided that empty declarations are invalid, except for a special case with tags (see §6.7.2.3) and the case of enumerations such as enum {zero,one}; (see §6.7.2.2). While many seemingly silly constructs are tolerated in other parts of the language in the interest of facilitating the machine generation of C, empty declarations were considered sufficiently easy to 30 avoid.

考虑到某些编译器实现将此类错误称为它们自己的语言扩展。:)

关于用VC++编译C程序[2005],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27345350/

相关文章:

c++ - 打开 NFC - 错误 LNK2019 : unresolved external symbol __imp__ShellExecuteW@24

c++ - Visual 2010 中带有 lambda 函数和枚举的 C2665,是错误还是正常?

visual-studio - MS Visual C/C++ 的 LIB 和 LIBPATH 环境变量有什么区别?

C 程序段错误

使用 fork() 进行 C 系统编程

c - 在其他结构中使用结构。 C

visual-c++ - 微软的 cryptoAPI "Microsoft Enhanced RSA and AES Cryptographic Provider"选择在 win7 上不起作用

c++ - 组织解决方案,项目和SVN

C - fork() & execl() & 循环 + "half pyramid output"

c - 使用递归的解决方案数量