【发布时间】:2013-09-28 03:01:16
【问题描述】:
我有丰富的 C++ 经验,现在我正在尝试用纯 C 做一些事情。但我注意到一个非常奇怪的事实。
以下代码:
#include <stdio.h>
int main()
{
double a;
scanf_s("%lf", &a);
double b;
b = a + 1;
printf("%lf", b);
return 0;
}
在 sourceLair 中编译正常(据我所知,使用 gcc 编译器),但在 Microsoft Visual Studio(使用 Visual C++)中生成以下错误:
1>Task 1.c(8): error C2143: syntax error : missing ';' before 'type'
1>Task 1.c(9): error C2065: b: undeclared identifier
1>Task 1.c(9): warning C4244: =: conversion from 'double' to 'int', possible loss of data
1>Task 1.c(11): error C2065: b: undeclared identifier
我对代码进行了一些实验,发现在调用任何函数之前放置一个变量声明之前是可以的,但是放置一个声明之后它会导致编译错误。
那怎么了?我使用全新安装的 Microsoft Visual Studio 2012 Express,没有更改任何设置。
【问题讨论】:
-
将你的文件从
.C重命名为.CPP -
@Ajay ...它将是 C++,但我需要纯 C。
-
所以,您需要更改代码。 C 不允许在语句之后声明变量。块中的所有变量必须首先出现在任何语句之前!
标签: c visual-c++ visual-studio-2012