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?
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++.