free geoip How To: Compile ISO C ‘99 Code in Visual Studio - Jayson's Blog

How To: Compile ISO C ‘99 Code in Visual Studio

This will be short how to on how to get ISO C ‘99 code to compile in Visual Studio 200x (in this case, Visual Studio 2012), since Microsoft only supports ANSI C ‘89 (and barely at that). Microsoft have made it very clear that they will not support C99, and instead urges users to move to Visual C++ instead. Ok that’s great…unless you need to write straight up C.

Background: I’m brushing up on my ISO C for an upcoming interview I have next week. I haven’t written a line of C since the early 00’s, but fortunately it’s coming back fairly quickly (it’s a fairly simple language despite its reputation). Normally I’d fire up my favorite Linux distribution (in this case Ubuntu) and start hammering away in VIM or Emacs, but this is lightweight one off stuff that I’m doing. I have never used Visual Studio to write C (but plenty of C++ of course), but figured it’d be a no brainer. Immediately I started running into strange errors, mainly syntax errors that simply were absolutely false, they syntax was fine. Also, C89 doesn’t support the <stdbool.h> header, which isn’t a huge problem, just change the bools to ints and test for 0/1.

But it was the syntax errors that were completely throwing me. Then I figured it out: C89 doesn’t support inline variable declaration; the variable must be defined and initialized before you can use it. Where this becomes really apparent is in ‘for’ loops. A standard for loop in C is simple enough:

   1: for(row = 0; row < SIZE; row++) /* Invalid in C89 */

Instead you have to do the following

   1: int row = 0;
   2: for(row; row < SIZE; row++) /* Valid in C89 */

Not a big deal, but for large nested loops it becomes extremely cumbersome. Plus all of the other features that are in C99 make it a no-brainer to use.

I then started wondering what they other differences between 89/99 and found a laundry list of items (as an exercise to the reader, just Google it). I’ve always coded in C99 and didn’t realize how large the chasm was between the two. Unacceptable. I was about to ditch the whole Visual Studio endeavor and just hammer it out on a GCC compiler. But me being the stubborn developer that I am figured that had to be a solution. Since these are one-offs, I don’t need strict ANSI/ISO conformance, nor portability…I just need the code to compile and work, then I’ll throw it away.

So I started poking around the Settings of the C++ project I had created to house the C code (in case you didn’t know, you can write standard C code in C++ projects since C is a subset of C++) and there it was under Project Properties –> Configuration Properties –> C/C++ –> Advanced –> Compile As: Choose Compile As C++ Code (/TP) from the dropdown. Problem solved (99% of the problems at least), the MSVC++ compiler supports C99. <stdbool.h> still isn’t supported, but this doesn’t matter since the C++ compiler recognizes bool as a valid keyword.

If you truly need full C99/C11 support on Windows, you’ll have to install CygWin/MinGW, but it’d probably be easier to create a VM image of a Linux distro. Of course that all being said, it is so rare that anyone writes raw C code these days it probably won’t matter, but in my case it’s necessary as I’m brushing up on some of the fundamentals of programming. C is the granddaddy of ALL programming languages. Knowing the basics will make anyone a better developer.