So I am currently learning C (planning to learn C++ after) but I have hit some dilemmas.
- How common would it be for someone to unknowingly mix C and C++?
- How to avoid mixing of C and C++?
So I am currently learning C (planning to learn C++ after) but I have hit some dilemmas.
The first thing would be to decide which language you are going to learn. It is unnecessary to learn C before learning C++ and vice versa.
Learn your chosen language thoroughly. Very few native english speakers mix german with english, and vice versa. It is the same with programming languages - if you know your chosen language well, you're not likely to mix things in from other languages, even if they have similarities. And, over time, you will recognise dialects.
Read up on incompatibilities between C and C++. The original C++ standard explicitly listed all the compatibilities between it and the preceding standard C (ANSI C89 or ISO C90). There are a lot of good resources on the incompatibilities freely available - some focused on C developers, some focused on C++ developers.
Configure your compiler (or IDE, or build scripts) so the compiler will detect excursions from your language of choice. Also configure it to maximum warning levels (virtually no compiler is configured this way out of the box) and strive to have code that NEVER triggers even the smallest warning from your compiler. Even better, do this with multiple compilers.
Without the steps above, the rules of thumb below will be less effective. After all, rules of thumb are less effective without understanding the underlying reasoning.
Some rules of thumb, if developing in C, avoid are C++-specific features.
#include <stdio.h>
is C, #include <iostream>
is C++.bool
type (and values true
and false
), then #include <stdbool.h>
(C99 or later).int func(void)
means the function takes no
arguments.const_cast
, reinterpret_cast
,
dynamic_cast
, static_cast
). Use the normal C-style cast (e.g.
(int *)
to convert something into a pointer to int)Some rules of thumb, if developing in C++, to avoid C-specific features.
#include <stdio.h>
is C, #include <iostream>
is C++. Make significant use of the C++ standard library.bool
type (and values true
and false
),
don't #include <stdbool.h>
. This is because bool
, true
, and false
are keywords in C++, but macros in standard C.const_cast
, reinterpret_cast
, dynamic_cast
, static_cast
).C++ diverged from C in the mid-1980's, when it had already acquired most of its familiar features. It continues to incorporate features from the recent C99 and C11 revisions. The intersection of C++ and C is a perfectly reasonable and useful language.
Recently, the GCC compiler project switched from C to C++ after verifying that their many thousands (millions?) of lines of sophisticated C code were really already mostly valid C++.
You can proceed to learn C, don't get too caught up in sophisticated features or wacky tricks, and simply switch to invoking the C++ compiler when you want something more structured.
Or, just use the C++ compiler all along, with the -Wall -pedantic
options. There are plenty of resources about this sort of portability: