Search code examples
cdbackwards-compatibilityphobos

Is D backwards compatible with C if you use the C libraries?


If I import the std.c libaries instead of including the libraries in C, would C code compile with a D compiler, or are there other backwords compatibility issues?


Solution

  • There are several subtleties in D that can make C code not behave exactly as you may want it to. For instance, integer promotion rules are not quite the same (but almost), and initialization rules are different (floating point values -- including arrays of such -- are initialized to NaN, for example). Further, C function pointer syntax was deprecated recently, so you might have to translate some C type syntax to the equivalent D syntax.

    In general, though, there is great focus on backwards compatibility, and most C code should compile fine (or with a very minor amount of changes) in D with the same semantics as in C.

    Also note that std.c is deprecated; please use core.stdc instead.