c - 在 Visual Studio Express 2012 中运行 C 代码的问题

标签 c visual-studio visual-studio-2012

我在大学安装了 VS Express 2012 来学习 C 语言。我创建新的空项目,然后将新项目添加到 Source Files 文件夹并将 Source.cpp 更改为 Source.c

“Hello World”运行没有问题,但是当我像这样编写简单的“for”循环时:

#include <stdio.h>
#include <stdlib.h>

int main() {

    for (int i = 0; i <= 6; i++)
    {
        printf("the i value is: %d\n", i);
    }

    getchar();

    return (0);
}

它给我写了很多错误:

------ Build started: Project: cTest, Configuration: Debug Win32 ------
  Source.c
e:\ctest\source.c(7): error C2143: syntax error : missing ';' before 'type'
e:\ctest\source.c(7): error C2143: syntax error : missing ')' before 'type'
e:\ctest\source.c(7): error C2065: 'i' : undeclared identifier
e:\ctest\source.c(7): warning C4552: '<=' : operator has no effect; expected operator with side-effect
e:\ctest\source.c(7): error C2059: syntax error : ')'
e:\ctest\source.c(8): error C2143: syntax error : missing ';' before '{'
e:\ctest\source.c(9): error C2065: 'i' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

请帮我解决这个问题。

最佳答案

显示于 IdeOne ,

error: ‘for’ loop initial declarations are only allowed in C99 mode note: use option -std=c99 or -std=gnu99 to compile your code

我想,这消除了你的疑虑。在函数的开头声明i。 正确的代码如下 WorkingExample .

来自C99 Wiki ,这是一项之前未提供的新功能。

Intermingled declarations and code: variable declaration is no longer restricted to file scope or the start of a compound statement (block), similar to C++

关于VC++编译器,直接来自Mr. Herb Stutter's mouth ,

Implements Standard C90 exactly (using /TC or naming files as something.c)

关于c - 在 Visual Studio Express 2012 中运行 C 代码的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23406479/

相关文章:

c - c中数字的小数

c - 函数返回 union 时类型不兼容

c - 读取磁盘中的 block (内核编程)

visual-studio - MSBuild:如何确保AfterBuild目标始终在C++项目中运行

visual-studio - 将我在Visual Studio 2008中所有项目的目标框架更改为3.5

c++ - 特征未对齐断言

c# - 添加项目作为引用导致无法加载文件或程序集或其依赖项之一

powershell - 以编程方式将 Visual Studio 2012 连接到 TFS 2012

c - 通过 LDAP C API 从 Active Directory 获取某些特定用户属性的正确方法是什么?

c++ - 调用 D3DX10CreateEffectFromFile() 方法的链接器问题