K&R C vs ANSI C (error C2143: syntax error : missing ';' before 'type' in Visual Studio 2008)
来源:互联网 发布:c语言结构体数组 编辑:程序博客网 时间:2024/04/28 03:45
今天偶然写了下面的程序(原来我写的程序不一样,下面的只是为了把问题简化)
void foo()
{
int p = 0;
if ( p == 0 ) {
int i = 0;
}
int a;
}
int main()
{
foo();
}
不幸的是偶然将这个文件保存成了test.c
然后编译的时候出现了
error,error C2143: syntax error : missing ';' before 'type'
感觉很奇怪,细细看来所有的语法都似乎都是对的,更奇怪的是把文件改成cpp或者cc能正常编译,把int a;和if调换下也能正常编译。
google了下得到了答案和总结如下:
来自MSDN (http://support.microsoft.com/kb/58559):
这样的错误可能发生在当变量的声明放在可执行代码之后。而这个是在K&R C中规定的,但在ANSI C中废除。
MSDN给出下面的代码会产生这个error
#include<stdio.h>
void main(void)
{
int i;
printf("Hello World/n");
int j;
}
注: The C Programming Language的作者简称K&R,也是C语言之父,经常用K&R C来和ANSI C做对比。这本书的第二版已经用ANSI.
我用的编译器是VS2008,看来微软向来无视标准。
总结:
在 ANSI C或者C++中,在可执行代码中随时定义变量是允许的,但是在K&R C中是不允许的,VS2008实现的C竟然是K&R C。
注意这样的错误也体现在VS中要是用for (int i = 0; i++; i<10)同时你的文件名是.c的也会出现这样的错误。
Code complete中讨论过变量名的最迟绑定有利于增加代码的可读性等。所以在VS中写c要注意了。
本博客搬迁至http://www.macode.net/
- K&R C vs ANSI C (error C2143: syntax error : missing ';' before 'type' in Visual Studio 2008)
- K&R C vs ANSI C (error C2143: syntax error : missing ';' before 'type' in Visual Studio 2008)
- KR C vs ANSI C (error C2143: syntax error : missing ';' before 'type' in Visual Studio 2008)
- error C2143: syntax error : missing ';' before 'type' in Visual C++
- C语言 error C2143: syntax error : missing ';' before 'type'
- error C2143: syntax error : missing ';' before 'type'
- error C2143: syntax error : missing ';' before 'type'
- error C2143: syntax error : missing ';' before 'type'
- error C2143: syntax error : missing ';' before 'type'
- error C2143: syntax error : missing \';\' before \'type\'
- error C2143: syntax error : missing ';' before 'type'
- error C2143: syntax error : missing ';' before 'type'
- error C2143: syntax error : missing ';' before 'type'
- VC++ 2005中编译C出错:error C2143: syntax error : missing ';' before 'type'
- VC++ 2005中编译C出错:error C2143: syntax error : missing ';' before 'type'
- VC++中编译C出错:error C2143: syntax error : missing ';' before 'type'
- VC++中编译C出错:error C2143: syntax error : missing ';' before 'type'
- VC++中编译C出错:error C2143: syntax error : missing ';' before 'type'
- 【视频】Android应用软件前景分析
- 正则表达式基础知识
- 哈希函数
- 在 Spket IDE 1.6.4 中使用 Ext 2.0
- Ghost90提供的c/c++面试题(带答案)
- K&R C vs ANSI C (error C2143: syntax error : missing ';' before 'type' in Visual Studio 2008)
- TCP头校验和计算算法详解(转)
- OSGi和嵌入式Jetty
- Qt多线程编程
- C#异步调用的好处和方法
- Servlet 输出中文乱码的新收获
- 使用jetty作为嵌入式web容器(部署war文件)
- struts 标签 参数
- Web站点风格切换的实现