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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
timestee
  • 1,086
  • 12
  • 36
  • 1
    Please pick just one question to ask. – Chris Eberle Mar 14 '12 at 01:40
  • http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B – Ben Voigt Mar 14 '12 at 01:41
  • Maybe check the following link for some helpful information and links: http://stackoverflow.com/questions/1201593/c-subset-of-c-where-not-examples – David Faber Mar 14 '12 at 01:43
  • 3
    Well, google gave me this [What is Clean C?](http://www.nbcr.net/software/doc/fetk/users_guide/maloc/c123.html) page... might be relevant. –  Mar 14 '12 at 01:45
  • First coined in Harbison and Steele? Really. I've been using Clean C, not knowing what it's called. – Kaz Mar 14 '12 at 01:55
  • Why isn't that called "clean C++"? – Jens Gustedt Mar 14 '12 at 09:27
  • 5
    Personally, I'd call it *unclean* C - having to cast void pointers, no designated initialization or compound literals - I feel dirty just thinking about it ;) all joking aside, using the common subset of C and C++ is a sad necessity if you want (or need) to support MSVC... – Christoph Mar 14 '12 at 17:13
  • More recent reference: [Clean C](http://www.qnx.com/developers/docs/qnxcar2/index.jsp?topic=%2Fcom.qnx.doc.neutrino.prog%2Ftopic%2Fhat_Clean_C.html) – chqrlie Feb 06 '22 at 07:05

2 Answers2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ouah
  • 142,963
  • 15
  • 272
  • 331
  • @pst "is a term coined in Harbison & Steele book" Isn't that the reference? – Mysticial Mar 14 '12 at 02: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 '12 at 02:20
  • 1
    Try around section 1.1.5 _Standard C++_ (p.5 in the 5th edition of C:ARM). – ldav1s Mar 14 '12 at 02:43
  • 1
    @Mysticial No, it is *a* reference. The ANSI standard is 'the' reference. – user207421 Mar 14 '12 at 03: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`... – R.. GitHub STOP HELPING ICE Jan 15 '13 at 06: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++.
Ben
  • 34,935
  • 6
  • 74
  • 113
  • see also http://david.tribble.com/text/cdiffs.htm – Christoph Mar 14 '12 at 21:29
  • What does it mean that `void func()` hasn't specified its arguments? How can this be used practically? – Victor Zamanian Oct 27 '12 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 '13 at 03:13
  • Ah, thanks. Didn't know that. Might be worth declaring `void` as the parameter in that case. :-) – Victor Zamanian Jan 15 '13 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. – Mehdi Charife Jul 06 '23 at 12:11