1

The code is simple as follows code1:

#include<stdio.h>
void main()
{
  int a=0;
  printf("c");
  int b=1;
  printf("q");
}

The question is when I compile this code in Visual C++6.0 or Visual Studio 2010 there is an error about "missing ;" I know the err is not about ";". There is no error when I write code like this code 2:

 #include<stdio.h>
    void main()
    {
      int a=0;
      int b=1;
      printf("c");

      printf("q");
    }

But when I compile code1 with gcc or the IDE c-free 5 , It has no problem.

Is this a problem due to the compiler that Microsoft use in its prodcut or something I don't know. I don't remember that any book said the variables must put above in C file. These compiler have different standards??

5
  • 3
    Yes, Visual C++ implements a pre-C99 version of the standard with some C99 features sprinkled in.
    – cnicutar
    Apr 24, 2014 at 14:24
  • Generally in any C code you are supposed to declare all variables at start of the function but nowadays compilers are intelligent and hence they allow you to declare variables in between the code. Apr 24, 2014 at 14:25
  • 2
    To make clear what cnicutar said: yes, in old C variables have to be declared at beginning of the function. Nullpointer: no, it's not (only) a problem of compilers but of standard. In C89 variables had to be at beginning (a compiler may relax that rule but then code wasn't not C89). Apr 24, 2014 at 14:25
  • @Nullpointer No, the C language allows variables to be declared anywhere. It has been allowed for the past 15 years and has nothing to do with "intelligent compilers", but rather conforming compilers. Visual Studio is not a conforming compiler, so all bets of what it will do are off.
    – Lundin
    Apr 24, 2014 at 14:31
  • 2
    When you are using Visual C++, write your files as .cpp. C++ has all the parts of C you will ever want to use.
    – Ben Voigt
    Apr 24, 2014 at 14:31

3 Answers 3

2

Visual Studio until recently did not have any support for C99 but currently supports a sub-set of C99 which allows mixing code and declarations, in gcc if you use -std=c90 -pedantic you will receive a warning for you first code sample:

warning: ISO C90 forbids mixed declarations and code [-Wpedantic]

by default gcc uses -std=gnu90 but if you add the -pedantic flag you should see the same warning.

Also main should return int not void.

6
  • @BenVoigt I may have used the wrong link but recent versions of visual studio does not complain about this. Apr 24, 2014 at 14:34
  • Interesting. Because the official statement was that Visual C++ would continue to ship a C90 compiler for backward compatibility.
    – Ben Voigt
    Apr 24, 2014 at 14:37
  • @BenVoigt I having trouble finding a MS link but this has more evidence Apr 24, 2014 at 14:38
  • Isn't gcc by default -std=gnu99 nowadays?
    – Lundin
    Apr 24, 2014 at 14:39
  • @Lundin in the gcc doc I linked it says The default, if no C language dialect options are given, is -std=gnu90; this is intended to change to -std=gnu11 in some future release. Apr 24, 2014 at 14:40
1

when I compile this code in Visual C++6.0 or Visual Studio 2010

Visual Studio does not conform well to the C standard, particularly not to the two latest versions of the standard. So formally, Visual Studio is not a C compiler. This is why you get strange compiler errors.

But when I compile code1 with gcc or the IDE c-free 5 , It has no problem.

Yes there is, this code will not compile on a conforming C compiler (for a hosted system). GCC correctly reports the following error:

error: return type of 'main' is not 'int' [-Wmain]
0

The language gcc targets is somewhat related to C99; The language the Microsoft compiler targets is somewhat related to C89.

They are different languages and have different rules.

I believe you can make the microsoft compiler behave like a C89 compiler with an option (/Za if I'm not mistaken); and you can make gcc behave like a C89 compiler with the options -std=c89 and -pedantic-errors; the options -std=c99 and -pedantic-errors make gcc behave like a C99 compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.