2

I am just learning programming really, and, while reading C++ Primer Plus (5th Ed.), I came across the statement that "any valid C program is a valid C++ program" and I am curious to see how far I can take that statement.

The book is pretty good, but is that statement necessarily true?

Is there any reason to not create a new file in CodeBlocks and choose that it be a C++ file despite my intentions of writing only C code for a particular project, instead of choosing to call it a C file? For example, maybe I will start writing something in C and realize down the road that I want to start using some of the C++ features, would it not have mattered what I decided in the beginning since I can just change the file extension afterwards?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Leonardo
  • 1,452
  • 3
  • 15
  • 26
  • 4
    Just because you _can_ write C++ which is also valid C does not mean you _should._ Pick one for a given program and stick with it. – Matt Ball Nov 09 '12 at 03:15
  • 2
    That statement seems obviously false to me. `int main(void) { int class = 3; printf("%d\n", class++); }`. That's a valid C program (with the #include) but not a valid C++ program. – David Schwartz Nov 09 '12 at 03:17
  • Why isn't it valid C++? No return value? – 1'' Nov 09 '12 at 03:23
  • 3
    It's not valid C++ because `class` is a reserved name in C++. You cannot name a variable `class` in C++, but you can in C. – user93353 Nov 09 '12 at 03:27
  • Here's an easy candidate for C, but not C++: `int main(void) {int i = 1; int arr[i]; return 0;}` – chris Nov 09 '12 at 04:25
  • While all these examples are correct, there are couple of important notes. First, most (if not all) **logical** incompatibilities are related to C99 compared to C++. There are still mainstream compilers not supporting C99 (hint: VC), so we can't always talk about C99 exclusively. Second, when using C++ compiler on C program (especially in C90), 99% of the errors will relate to problematic type enforcement which should better be fixed (and it will compile with C compiler as well). – SomeWittyUsername Nov 09 '12 at 08:06
  • @icepack: No, the main incompatibility that affects **all** properly written C software is that C++ lacks implicit conversions from `void *` to other pointer types. This breaks all correct usages of `malloc`. – R.. GitHub STOP HELPING ICE Nov 09 '12 at 22:17
  • I do not consider this logical incompatibility. That's stronger typing and C program will only benefit from it – SomeWittyUsername Nov 10 '12 at 00:39

4 Answers4

4

C and C++ are two very different languages. Yes, C++, for the most part, cooperates well with standard C syntax. But C++ is object oriented and C is not. Organization is very different between the two.

Learn C first. Learning C++ is a lot easier after learning C, and it is usually implied that you know at least basic C syntax before tacking an idea with C++.

John
  • 2,675
  • 2
  • 18
  • 19
  • 1
    "*But C++ is object oriented and C is not.*" C++ is also generic and C is not. C++ is also functional (to an extent) and C is not. I'm not sure why people get so hung up on OO... – ildjarn Nov 09 '12 at 03:17
  • 1
    You are correct, and I am learning C first and I have started with K&R, but I think you are missing my question entirely. I am just curious if it matters to the "computer/compiler" not to advocates of either language. Sorry if I confused you. – Leonardo Nov 09 '12 at 03:18
  • 1
    Ah, then maybe this will help http://stackoverflow.com/questions/1201593/c-subset-of-c-where-not-examples – John Nov 09 '12 at 03:26
3

The statement is outright false. There are plenty of C programs that are not valid C++ programs, and I would go so far as to say that any non-trivial C program that's properly written is not a valid C++ program. Hint: char *p = malloc(n); is not valid C++.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
0

mostly no, but I think some libraries you may not be able to access if you choose C.

0

I remember some C++ sentences cannot be compiled if your file is C language file(*.c), but it is usually OK, vice versa. Maybe due to that C++ is a superset of C.

And I think it is OK that whether your create a *.c or a .cc/.cpp file. The important thing when writing a C program and a C++ program, in my view, is the thinking style. C is a process language and C++ is an OO language.

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171