Why FILE pointer need to be declared out main() in Visual Studio 2010?

I was trying to compile a simple ansi C example in Visual Studio 2010 and came across with this error compiling:

Error: patchC.c(5): error C2275: 'FILE' : illegal use of this type as an expression

Program1:

#include <stdio.h>

int main(void) {
    printf("Hello world!\n");
    FILE *fp;
    fp = fopen("test.txt", "r");
    return 0;
}

The same program compiles without errors in gcc v4.5.2.

But, if I put the "FILE *fp;" line out of the main(), the program compile gracefully.

Program2:

#include <stdio.h>

FILE *fp;

int main(void) {
    printf("Hello world!\n");
    fp = fopen("test.txt", "r");
    return 0;
}

I don't figure out why this behavior, anyone could answer?

5
задан James McNellis 4 May 2011 в 02:35
поделиться