Andrey Bushman
Andrey Bushman

Reputation: 12486

File won't compile in MS Visual Studio, but will in GCC. Why?

I wrote such sample code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

char* print_errno_msg(int value);

int main(void){
    struct stat buffer;
    int status;
    status = stat("./main.c", &buffer);
    char *msg = print_errno_msg(errno); /* syntax error : missing ';' before 'type' */

    printf("status = %i; errno = %i; %s\n", status, errno, msg); /* 'msg' : undeclared identifier */
    errno = 0;
    int ch = getchar(); /* syntax error : missing ';' before 'type' */
    return 0;
}

char* print_errno_msg(int value){
    static char *messages[] = {
        "",
        "Operation not permitted",
        "No such file or directory",
        "No such process"
    };
    return messages[value];
}

It was fine compiled and executed in Ubuntu 12.04 via gcc. I have tried to compile and run it in Windows 8 via MS Visual Studio 2012. I have created empty c++ project and created the new file: main.c. But I get errors at compiling process (read the comments in the code, please).

I not understand that error messages. Is my syntax not right? Why it happened?

Regards

Upvotes: 0

Views: 1018

Answers (1)

Mike Kwan
Mike Kwan

Reputation: 24447

You are using Unix header files which are not available on Windows.

Another thing is that VC++'s C compiler supports only C89. This does not allow mixing of declarations and code. All declarations must be at the start of a scope.

Upvotes: 3

Related Questions