KR C vs ANSI C (error C2143: syntax error : missing ';' before 'type' in Visual Studio 2008)
2009-07-16 22:54
387 查看
今天在VS2008下编译c程序遇到的,在网上搜索了一下,也有同学碰到过,就直接转贴于此。
不幸的是偶然将这个文件保存成了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
注: 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要注意了。
from: http://blog.csdn.net/fancylea/archive/2009/06/10/4256793.aspx
void foo() { int p = 0; if ( p == 0 ) { int i = 0; } int a; } int main() { unsigned char ByteNum; ByteNum = 0; int i;//error C2143: syntax error : missing ';' before 'type' 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要注意了。
from: http://blog.csdn.net/fancylea/archive/2009/06/10/4256793.aspx
相关文章推荐
- 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)
- error C2143: syntax error : missing ';' before 'type' in Visual C++
- 【VC6.0】error C2143: syntax error : missing ';' before 'type'
- error C2143: syntax error : missing ';' before 'type'
- error C2143: syntax error : missing ';' before 'type'
- VC6.0 error C2143: syntax error : missing ';' before 'type'。。
- 编译错误syntax error : missing ';' before 'type'原因探寻(VS 编译C文件)
- 奇怪的 error C2143: syntax error : missing ';' before 'type'
- VS编译错误: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'
- 编译错误syntax error : missing ';' before 'type'原因探寻
- 编译错误syntax error : missing ';' before 'type'
- C++随记:【cstdio(25) : error C2143: syntax error : missing '{' before ':'】
- 编译错误syntax error : missing ';' before 'type'原因探寻
- error C2143: syntax error : missing ';' before '*....
- error C2143: syntax error : missing ';' before 'tag::id' 'BOOL' : redefinition; typedef cannot be ov
- syntax error : missing ';' before 'type'错误