What is "Clean C" and how does it differ from standard C?

Clean C is a term coined in Harbison & Steele book (C: A Reference Manual, Prentice Hall). A program is said to be written in Clean C if it can be compiled by a standard C compiler and a standard C++ compiler; and its execution would not produce different observable behavior from the two compilers (the issues of optimizations being irrelevant).


One that strikes me as being the most obvious is that in C++, you have to cast the return value of malloc. Also structs are automatically typedefed in C++.

Always use a C compiler for C code, not C++. C++ isn't perfectly compatible with C.

A few others differences may be:

  • In C, declaring void func(); declares a function that hasn't specifed what its arguments are, whereas in C++, void func(); is equivalent to the C void func(void)', taking no arguments;
  • Prototypes are required in C++, whereas it's generally just a warning in C;
  • The type of character constants (like 'a') is int in C and char in C++;
  • The type of string literals is char [] in C and const char [] in C++;
  • Some legitimate variable names in C, like class, are reserved keywords in C++.

Tags:

C++

C