函数调用后无法定义变量

我有很好的C ++经验,现在我正在尝试用纯粹的C做一些事情。但我注意到一个非常奇怪的事实。

以下代码:

#include  int main() { double a; scanf_s("%lf", &a); double b; b = a + 1; printf("%lf", b); return 0; } 

在sourceLair中编译OK(使用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,我有一个坏消息:Visual Studio附带的Microsoft编译器不支持C99,只支持C89,并且在该语言版本中,只能在范围的开头声明变量。 非声明语句后的声明是错误。

因此,要么重写代码以使其符合C89,要么使用支持C99的编译器(例如GCC,可以在安装MinGW后使用,或者clang,它只有实验支持与VS集成)。