2014-05-01 3 views
0

내 대학에서 C 언어를 배우려면 VS Express 2012를 설치해야합니다.Visual Studio Express 2012에서 C 코드를 실행할 때의 문제

#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); 
} 
: 나는 문제없이 실행하지만이 같은 루프 "를"간단하게 작성할 때 다음 비어있는 새 프로젝트를 만들고 소스 파일 폴더에 새 항목을 추가하고

에 "Hello World"를 Source.c하는 Source.cpp 변경

이 오류를 나에게 많이 쓴다 :

------ 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에 표시

+1

'i' 변수를 함수의 시작 부분에 선언하십시오. –

+0

@AlexFarber Works. 감사. 왜 내가 루프 안에서 변수를 선언 할 수 없는지 설명 할 수 있습니까? – Luchnik

+0

@Luchnik C에서는 임의의 범위에서 지역 변수를 선언 할 수 없지만 함수 코드 블록의 시작 부분에서만 선언 할 수 있습니다. –

답변

1

, 내가 생각

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에서 이것은 이전에 사용할 수 없었던 새로운 기능입니다.

Mr. Herb Stutter's mouth에서 직접 VC++ 컴파일러에 대해

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

,

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

0

코드가 코드를 컴파일하는 아래 현재 C 표준을 준수하지 않기 때문에이 발생합니다. C99 표준에서는 int for i 루프를 선언 할 수 있습니다. -std = c99로 컴파일하십시오.

관련 문제