error C2143: syntax error : missing ';' before 'type'

许久之前,就想看看C和C++,看看这两种神奇的编程语言,但是一直被这样或者那样的琐事给耽搁了(狂喷自己的拖延症)。

今天翻开自己的移动硬盘找到一本古老的书籍:《The C Programming Language 中文版》,浏览一下,缓解一下内心的空虚。

事情并没有想象中的那么顺利,刚看了一个例子的两种方法,但是当把他们组合起来时,就遇到了困难:

错误提示:

  error C2143: syntax error : missing ‘;‘ before ‘type‘

  error C2065: ‘fahrr‘ : undeclared identifier

代码如下:(hello.c)

 1 #include <stdio.h>
 2 main()
 3 {
 4     float fahr, celsius;
 5     float lower, upper, step;
 6
 7     lower = 0; /* 温度表的下限*/
 8     upper = 300; /* 温度表的上限*/
 9     step = 20; /* 步长*/
10
11     fahr = lower;
12     while (fahr <= upper) {
13         celsius = 5.0 * (fahr-32.0) / 9.0;
14         printf("%3.0f %6.1f\n", fahr, celsius);
15         fahr = fahr + step;
16     }
17
18     int fahrr;
19
20     for (fahrr=0;fahrr<=300;fahrr=fahrr+20)
21     {
22         printf("%3d %6.1f\n",fahrr,(5.0/9.0)*(fahrr-32));
23     }
24     printf("Press ENTER to continue...");
25 }

错误出现在18和20行;

但是如果以第18行分开上面和下面都是可以执行的;

一番百度之后:

1.有人说:这段代码在 VC++ 和 DEV C++ 下调试是通过的,自己试了一下,发现在VC++下并没有通过;

2.有人说:用的是C语言环境,要把变量定义放到前面,改了一下:

 1 #include <stdio.h>
 2 main()
 3 {
 4     float fahr, celsius;
 5     float lower, upper, step;
 6
 7     int fahrr; //修改的部分
 8
 9     lower = 0; /* 温度表的下限*/
10     upper = 300; /* 温度表的上限*/
11     step = 20; /* 步长*/
12
13     fahr = lower;
14     while (fahr <= upper) {
15         celsius = 5.0 * (fahr-32.0) / 9.0;
16         printf("%3.0f %6.1f\n", fahr, celsius);
17         fahr = fahr + step;
18     }
19
20     //int fahrr; //修改的部分
21
22     for (fahrr=0;fahrr<=300;fahrr=fahrr+20)
23     {
24         printf("%3d %6.1f\n",fahrr,(5.0/9.0)*(fahrr-32));
25     }
26     printf("Press ENTER to continue...");
27 }

结果正常通过。

在网上看了一番之后,得出结论:

  在纯C中,在一个代码块中变量声明必须在最前面。

error C2143: syntax error : missing ';' before 'type'

时间: 2024-08-28 20:50:54

error C2143: syntax error : missing ';' before 'type'的相关文章

一个关于调用函数函参写void的错误error C2143 syntax error missing &#39;)&#39; before &#39;type&#39;

error C2143 syntax error missing ')' before 'type' 错误代码: srand((unsigned)clock(void)); 错误原因: 画蛇添足的写了void 修改: 把void去掉即可 总结: void代表空,就是实实在在的什么都没有,除非是在声明,否则平常用的时候就是空. 一个关于调用函数函参写void的错误error C2143 syntax error missing ')' before 'type'

vs2012编译C代码,总是出现error C2143: syntax error : missing &#39;;&#39; before &#39;type&#39;

今天编译一个动态库,里面有用C编写的源文件, 为了调试自己加了一些简单的信息输出语句,但是总是编译不过, 最后精简到只是定义一个函数的局部变量也会编译失败, 最后Google了才明白,vs在编译C代码的时候会有一些特殊的check, 其中一种就是需要函数定义局部变量要在开头. 举例如下: 错误的代码 void func1() { int x: //do some work int y; //do other work } 这样的代码在编译的时候就会在int y 这里报error C2143: s

error C2143: syntax error : missing &#39;;&#39; before &#39;{&#39;

这是我在实现哈夫曼树的时候,遇到的错误,具体为什么我也不清楚!!!因为这是我用学校实验室的电脑编译出现的错误(用的软件是VC6.0,贼老的版本!!!),我自己的是Code Blocks(没有出错)??? 代码如下: for ( i = 1; i <= n; i++ ) { huffNode HT[i](w[i],0,0,0);//初始化前n个节点(构造哈夫曼树的原始节点) } 然后,就有错了(-_-!) error C2057: expected constant expression erro

突然出现错误gdiplustypes.h(280): error C2059: syntax error : &#39;constant&#39;等未知错误

gdiplustypes.h(280): error C2059: syntax error : 'constant'gdiplustypes.h(280): error C2238: unexpected token(s) preceding ';'gdiplustypes.h(281): error C2059: syntax error : 'constant'gdiplustypes.h(281): error C2238: unexpected token(s) preceding '

LINK : fatal error LNK1117: syntax error in option &#39;VERSION:1.6.5&#39;

今天在用vs2015编译ogre 1.6.5的代码时发生连接错误 LINK : fatal error LNK1117: syntax error in option 'VERSION:1.6.5'. 解决:On the Settings (Project > Settings > Linker) removing the "Version" entry. 没有深入搞懂为什么,先Mark下. 参考: http://stackoverflow.com/questions/20

syntax error : missing &#39;;&#39; before &#39;type&#39;

#include <stdio.h> #include <stdlib.h> int main (int argc, char **argv) { char a[] = {'a','b','c','d','e'}; char b[] = "abcde"; char c[][3] = {{ 'a' , 'b' , 'c' },{ 'd' , 'e' , 'f' },{ 'g' ,'h' , 'j' },{ 'k' , 'h' , 'l' }}; char *p =

Command line option syntax error.type Command /? for help

电脑装思维导图的时候,报错显示"Command line option syntax error.Type Command /? for help."就查了一下,原来是系统没有C++2005,需要安装,就上网下载了一个vcredist_x86.exe,但是双击安装,仍然出现这个错误. 没办法,接着上网查吧,是什么原因呢?网上说是因为该文件安装不支持中文安装路径,然后我就把文件夹改成了英文名称的,但是双击还是出现这个错误.可能有的人用这种方法成功了吧~本着没有解决不了的问题的思想,接着奋

Type Syntax error, insert &quot;)&quot; to complete Expression

今天倒持了 几个小时! 愣是 没有明白 ,为什么我的JSP的第一行没有代码?  还是报错! 错误是: Description Resource Path Location Type Syntax error, insert ")" to complete Expression left.jsp /qyrs/WebRoot/admin/iframe line 1 Client-side JavaScript Problem 翻译: 描述资源路径位置类型语法错误,插入")&quo

java.lang.Error: Unresolved compilation problems: Syntax error on token &quot;return&quot;, delete this token Type mismatch: cannot convert from Init to String

java.lang.Error: Unresolved compilation problems:   Syntax error on token "return", delete this token  Type mismatch: cannot convert from Init to String Dubbo 在调用服务时候报的错误! 那么问题来了.这什么错? ? 英语不好.     Type mismatch: cannot convert from Init to Strin