C error C2143 syntax error missing before ‘type’

1. Problem descriptionThe problem display is shown in the figure: All ides are VS2010.

Source code display: Source code is a Fahrenheit to Celsius temperature of the program.

#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. Cause of the problem

In C, all variables must be declared before they are used. Declarations are usually placed at the beginning, before any executable statement. Declare the properties used to describe variables, which consist of a type name and a variable scale.

Here because the variable declaration statement on line 12 is placed after the executable statement.

3. Problem solving

Place all variable declarations before all executable statements. The modified code is as follows:

#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. Problem summary
Although this problem is a stipulation of C language, it mainly appears in the compilation stage, which is interpreted by the compiler. This problem will not appear in VS2017, but will appear in VS2010.


Read More: