C语言 error C2143: syntax error : missing ';' before 'type'

本文介绍了一个关于C语言编程中变量声明位置的问题,在VS2010中,若变量声明位于可执行语句之后,则会导致编译错误。文章详细解释了问题的原因,并提供了正确的代码示例。

1.问题描述

问题展示如图所示:所有IDE为VS2010。

源代码展示:源代码是一个将华氏温度转换为摄氏温度的程序。
#include <stdio.h>

//void Fahr_Celsius()
int main()
{
	//int fahr, celsius;
	int lower, upper, step;
	lower = 0;
	upper = 300;
	step = 20;
	printf("Hello world!\n");
	int fahr, celsius;
	fahr = lower;
	while (fahr<=upper)
	{
		celsius = 5 * (fahr - 32) / 9;
		printf("%d\t%d\n", fahr, celsius);
		fahr = fahr + step;
	}
	return 0;
}
2.问题原因
在C语言中,所有变量都必须先声明后使用。声明通常放在起始处,在任何可执行语句之前。声明用于说明变量的属性,它由一个类型名和一个变量表组成。
在这里是因为第12行的变量声明语句放在了可执行语句之后。

3.问题解决

将所有变量声明放在所有可执行语句之前。修改后代码如下:
#include <stdio.h>

int main()
{
	int fahr, celsius;
	int lower, upper, step;
	lower = 0;
	upper = 300;
	step = 20;
	printf("Hello world!\n");
	//int fahr, celsius;
	fahr = lower;
	while (fahr<=upper)
	{
		celsius = 5 * (fahr - 32) / 9;
		printf("%d\t%d\n", fahr, celsius);
		fahr = fahr + step;
	}
	
	return 0;

}

4.问题总结

这个问题虽然是C语言的规定,但是这个问题主要出现在编译阶段,由编译器解释,在VS2017是不会出现这个问题,但是在VS2010会出现这种问题。

lj.c C:\Users\Lenovo\Desktop\lj.c(5) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(5) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(5) : error C2143: syntax error : missing ')' before 'type' C:\Users\Lenovo\Desktop\lj.c(5) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(5) : error C2065: 'i' : undeclared identifier C:\Users\Lenovo\Desktop\lj.c(5) : warning C4552: '<' : operator has no effect; expected operator with side-effect C:\Users\Lenovo\Desktop\lj.c(5) : error C2059: syntax error : ')' C:\Users\Lenovo\Desktop\lj.c(5) : error C2143: syntax error : missing ';' before '{' C:\Users\Lenovo\Desktop\lj.c(6) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(6) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(6) : error C2143: syntax error : missing ')' before 'type' C:\Users\Lenovo\Desktop\lj.c(6) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(6) : error C2065: 'j' : undeclared identifier C:\Users\Lenovo\Desktop\lj.c(6) : warning C4552: '<' : operator has no effect; expected operator with side-effect C:\Users\Lenovo\Desktop\lj.c(6) : error C2059: syntax error : ')' C:\Users\Lenovo\Desktop\lj.c(6) : error C2143: syntax error : missing ';' before '{' C:\Users\Lenovo\Desktop\lj.c(21) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(21) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(21) : error C2143: syntax error : missing ')' before 'type' C:\Users\Lenovo\Desktop\lj.c(21) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(21) : warning C4552: '<' : operator has no effect; expected operator with side-effect C:\Users\Lenovo\Desktop\lj.c(21) : error C2059: syntax error : ')' C:\Users\Lenovo\Desktop\lj.c(21) : error C2143: syntax error : missing ';' before '{' 执行 cl.exe 时出错. lj.exe - 1 error(s), 0 warning(s)
10-29
C:\Users\Lenovo\Desktop\lj.c(19) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(19) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(19) : error C2143: syntax error : missing ')' before 'type' C:\Users\Lenovo\Desktop\lj.c(19) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(19) : error C2065: 'i' : undeclared identifier C:\Users\Lenovo\Desktop\lj.c(19) : warning C4552: '<' : operator has no effect; expected operator with side-effect C:\Users\Lenovo\Desktop\lj.c(19) : error C2059: syntax error : ')' C:\Users\Lenovo\Desktop\lj.c(19) : error C2143: syntax error : missing ';' before '{' C:\Users\Lenovo\Desktop\lj.c(33) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(34) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(34) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(34) : error C2143: syntax error : missing ')' before 'type' C:\Users\Lenovo\Desktop\lj.c(34) : error C2143: syntax error : missing ';' before 'type' C:\Users\Lenovo\Desktop\lj.c(34) : warning C4552: '<' : operator has no effect; expected operator with side-effect C:\Users\Lenovo\Desktop\lj.c(34) : error C2059: syntax error : ')' C:\Users\Lenovo\Desktop\lj.c(34) : error C2143: syntax error : missing ';' before '{' C:\Users\Lenovo\Desktop\lj.c(35) : error C2065: 'min_index' : undeclared identifier 执行 cl.exe 时出错.
最新发布
11-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值