How can I convert c99 source code automatically to c89? I want to compile c99 libraries with Visual C++, but MSVC only supports c89. Many changes are only syntactic, such as struct initializers, and you could write a tool to "de-c99" code automatically. Does this preprocessor exist?
-
1I believe the reason to create C99 was exactly to avoid such "preprocessors" :) For the "future" (yes, pun intended) - avoid modern features if you use the old tools. – Viktor Latypov May 29 '12 at 17:53
-
1It would be less work if you compiled it as C++ instead... – K-ballo May 29 '12 at 17:59
-
2You could use a modern third party compiler such as Intel's ICC which can be plugged into Visual Studio. – Paul R May 29 '12 at 18:03
-
1Check out this answer: http://stackoverflow.com/a/146419/10077 – Fred Larson May 29 '12 at 18:03
-
@PaulR: How do I this? and with GCC, it's possible too? thanks! – Jack May 29 '12 at 18:54
-
@Jack: you just buy ICC from Intel and install it - you can use it standalone and it also integrates into Visual Studio. Some other compilers may also work with Visual Studio but not gcc AFAIK. – Paul R May 29 '12 at 20:26
-
See also https://stackoverflow.com/q/19121453 – Arto Bendiken Sep 01 '18 at 04:32
3 Answers
The commercial Comeau C/C++ compiler can do this.
Alternatively, use a proper C compiler (eg GCC or Clang via MinGW, Pelles C, Intel) and link the resulting object files. However, not all of these (in particular MinGW) support Microsoft's debug format.

- 164,997
- 36
- 182
- 240
What you need is a program transformation system. Such a tool reads source code, builds compiler data structures (such as abstract syntax trees, allows you to apply (source-to-source) transformations on those structures, and then can regenerate source from the modified data structures.
You need one that can parse C99, and transform to C89. Our DMS Software Reengineering Toolkit can do that, using its C Front End (which can handle both dialects of C, including MSVC 89, as well as ObjectiveC). Having a mature parser is necessary if you want to do this.
Many folks may suggest that all you need is a C99 parser. As a practical matter, that isn't true; to do any interesting transformations on typical computer languages you need symbol table data, some data flow, etc. For more details, see my essay on Life After Parsing, and how DMS provides that life.
One such aspect is how you encode replacing struct initializers. You might do that by custom code that walks a C99 AST, finds such struct initializers, and procedurally hacks the tree. Yes, that works, but it isn't easy and you have to know a huge amount about the structure of the tree. DMS offers source-to-source rewrites, so you can write patterns that recognize the idioms you want modify, and patterns that produce the resulting idioms, all using C surface syntax.

- 93,541
- 22
- 172
- 341