I think I have an advanced knowledge of C++, and I'd like to learn C.
There are a lot of resources to help people going from C to C++, but I've not found anything useful to do the opposite of that.
Specifically:
C++ is mostly backwards compatible with C. However it is very possible to write C code that will not compile as C++... usually by using a C++ keyword as a variable name or something. That depends on the software doing the interpreting.
Switching from C to C++ can be both easy, as there are many similarities between the two languages, and hard, as there are many differences that require forgetting what you know and habits that you may have developed from programming in C.
C is worth learning in 2022 because it is easy to grasp. It gives you basic knowledge about the inner workings of computer systems and the core concepts that drive programming.
There's a lot here already, so maybe this is just a minor addition but here's what I find to be the biggest differences.
Library:
Idioms:
vector
and string
save you a lot of work), smart pointers (you can't really do "smart pointers" as such in C. You can do reference counting, but you have to up and down the reference counts yourself, which is very error prone -- the reason smart pointers were added to C++ in the first place), and the lack of RAII generally which you will notice everywhere if you are used to the modern style of C++ programming. class
in C++. You have to maintain a convention of prefixing everything you want associated with a type.enum
but lots of older code uses #define
instead), and for generics (where C++ uses templates).Advice:
Make heavy use of forward declaration to make types opaque. Where in C++ you might have private data in a header and rely on private
is preventing access, in C you want to push implementation details into the source files as much as possible. (You actually want to do this in C++ too in my opinion, but C makes it easier, so more people do it.)
C++ reveals the implementation in the header, even though it technically hides it from access outside the class.
// C.hh class C { public: void method1(); int method2(); private: int value1; char * value2; };
C pushes the 'class' definition into the source file. The header is all forward declarations.
// C.h typedef struct C C; // forward declaration void c_method1(C *); int c_method2(C *); // C.c struct C { int value1; char * value2; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With