Category Archives: Visual Studio

some thoughts about Visual Studio 2013 C99 compiler

So here it is, Visual Studio 2013 with partial C99 support.

It’s good to see Microsoft finally picked up support for C99 because otherwise a lot of code can’t compile because they are C99 compliant.

Well, in my experience with C, the most annoying thing from C89 to C99 is mix variable declaration with code. I simply can’t live without it. I like to declare a variable just before I need to use it other than stack all variables at the start and use it later. I know this is a personal taste thing but C99 do offers more freedom.

Now let’s have a look at VS 2013’s new C99 compiler. I have thrown a not too simple project to it and compilation failed, after some digging around I found the bug is quite interesting. That project uses a lot of “variable declaration inside code” feature and some declaration is immediately after a if switch without bracket. To trigger this bug, that declaration needs to be a struct variable and have to be not inside the file you define your main(). Let’s see a example:

Say you got a file named foo.h with contents like this:

#ifndef __FOO__
#define __FOO__

typedef struct foo {
 int bar;
}foo;

int foo_do(int a, int b);

#endif

and you write your foo.c like this:

#include "foo.h"

int foo_do(int a, int b) {
 int d = 1;
 if (a > b) d = 0;
 foo *c;
 return -1;
}

Even you got your main.c right, the compilation would fail in foo.c in line 6

To fix it, just change

 if (a > b) d = 0; 

to

 if (a > b) {d = 0;} 

or if you change

 foo *c; 

to

 int c; 

It also won’t have trouble compiling.

And if you move foo.h and foo.c to main.c like this:

typedef struct foo {
 int bar;
}foo;

int foo_do(int a, int b) {
 int d = 1;
 if (a > b) d = 0;
 foo *c;
 return -1;
}

/* your code */

int _tmain(int argc, _TCHAR* argv[]) {

/* your code */

then delete foo.h and foo.c, the project would compile just fine.

I do appreciate Microsoft adopting C99, but if they can make their C99 compiler more robust, I would be more happy.

Hope someone at Visual Studio developer team can read this.