13

What are the differences between pure C and C in C++?

What are some details about 'Clean C' on which Lua is based? What are those features?

8

2 Answers 2

23

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).

5
  • @pst "is a term coined in Harbison & Steele book" Isn't that the reference?
    – Mysticial
    Mar 14, 2012 at 2:15
  • @pst I cannot find the page number in my H&S copy, but a number of people refer to this term and H&S in comp.lang.c
    – ouah
    Mar 14, 2012 at 2:20
  • 1
    Try around section 1.1.5 Standard C++ (p.5 in the 5th edition of C:ARM).
    – ldav1s
    Mar 14, 2012 at 2:43
  • 1
    @Mysticial No, it is a reference. The ANSI standard is 'the' reference.
    – user207421
    Mar 14, 2012 at 3:52
  • 5
    What a horrible misnomer. I would define "Clean C" as C code which does not use cast operators, does not use non-prototype function declarations, does not invoke undefined behavior or depend on implementation-defined behavior, and does not require the compiler to issue any diagnostic messages required by the standard. This, of course, is mutually exclusive the with "actual" definition of "Clean C", which requires horrible casting madness in order to use malloc... Jan 15, 2013 at 6:41
3

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++.
5
  • see also david.tribble.com/text/cdiffs.htm
    – Christoph
    Mar 14, 2012 at 21:29
  • What does it mean that void func() hasn't specified its arguments? How can this be used practically? Oct 27, 2012 at 10:30
  • @VictorZamanian The calling convention allows you to pass an incorrect number of arguments in C without crashing. Of course, if the callee does something bad with your "incorrect" arguments, it's undefined behavior. You are simply declaring the return is void. C allowed some extreme sloppyness like that. I think any arguments you pass to something declared like that will be assumed to need to be converted to int. Not sure and I really don't care for trivia like that. I declare my parameters. :)
    – doug65536
    Jan 15, 2013 at 3:13
  • Ah, thanks. Didn't know that. Might be worth declaring void as the parameter in that case. :-) Jan 15, 2013 at 17:42
  • "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;" This is no longer the case in C23. Jul 6 at 12:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.