Why does the FILE pointer have to be declared main () in Visual Studio 2010?

I tried to compile a simple ansi C example in Visual Studio 2010 and ran into this error compilation:

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 "FILE * fp;" line from main (), the program compiles gracefully.

Program2:

#include <stdio.h>

FILE *fp;

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

I do not understand why this behavior, anyone can answer?

+5
source share
1 answer

Visual ++ C90. C90 . , fp main printf:

int main(void) {
    // Declarations first:
    FILE *fp;

    // Then statements:
    printf("Hello world!\n");
    fp = fopen("test.txt", "r");
    return 0;
}

C99 ++ , . Visual ++ C99. ++ ( .cpp), .

+16

All Articles