C++ to C

Started by
48 comments, last by MaulingMonkey 18 years, 7 months ago
Hello everyone, I made a lounge post a day or two ago about programming in C when you started with C++. Although some told me to stay away, while others told me to go for it. I decided I would learn plain C, for anything, just to say I can program in C/C++ not just C++. I should also state that I know using const, classes, templates, etc; Are more 'correct', and using C for a project that could be accomplished just the same in C++ would be a waste really. I merely want to take a break from C++ and learn C to broaden my knowledge. I've been looking though a bunch of tutorials on C, and trying to lay-out in my brain the things I can do in C++ but can't do in C, so I thought I'd start a little list and everyone maybe could add to it to help me out, this may also be pretty helpful to others in a similar situation, maybe coming from C to C++ as well. I'll start this off ... - When declaring a variable of struct and enum you must use the keyword struct and enum to declare the variables (unless a typedef is used). - #define or enum must be used for constants. - No classes of course (but I know OO design can still be implemented). - Cannot 'pass by reference' as in C++ (must use pointers and & operator when passing). - Cannot use new and delete must use malloc and free, you must manually cast void* to correct type when allocating. (Hope the statements above are correct :S) EDIT: Of course the differences in I/O libraries as well. I've been reading up on this also clicky! (Thanks dcosborn for the link) Since I have this list above, I want to use this post to compile a list of syntax differences that are directly visible when writing the code. Anything that the compiler does ... I'm less concerned with, and can find that information in the link above. Thanks for any help / insight.
Advertisement
No templates, no namespaces, no overloading, no references, no inheritence, no member functions, no exceptions.
Note that C does have const (and volatile).

I guess it depends on the area, but here in Detroit much of the work is embedded for automotive applications and there's embedded medical applications close by in Ann Arbor as well. Almost all use C exclusively (typically with MISRA C requirements, which stipulate no dynamic memory allocation is allowed among other requirements).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Another one is that you have to declare your variables at the top of the function.

Also, not to de-rail your thread, but have you thought about learning another modern language instead of getting into C? If you already know C++, I think that adding C# skills to your toolbox would be more useful in than straight C skills. Just something to think about...

-John
- John
You should have a look at the 'C++ tutorial for C users' (http://www.4p8.com/eric.brasseur/cppcen.html), it's a list of (I think) all the differences between the two languages and they are explained very clearly there.. but to be honest, I don't understand why you want to learn C when you already know C++. C++ has many advantages and is definatly not slower when you don't use it's 'extra' features like classes.

Bas
I think that this document is the most complete around here.
[ILTUOMONDOFUTURO]
C++ is a super-set of C (it meens that C++ has everything C does). Don't waste your time. There are tons of things you must master to become a pro. If you finished your first C++ steps, proceed to advance C++ topics. This reminds me my C# book. Half of the book has been wasted for comparison between C#, C++, Java.... I DONT CARE, I JUST WANT TO LEARN C# !!!
Quote:Original post by basananas
I don't understand why you want to learn C when you already know C++.


Well, there's a C compiler for every platform in Christendom. The same cannot be said for C++.
Quote:Original post by cpp_boy
C++ is a super-set of C (it meens that C++ has everything C does).


This is not entirely true. From "The C++ Programming Language, Third Edition and Special Edition":
"With minor exceptions, C++ is a superset of C (meaning C89, defined by ISO/IEC 9899:1990). Most differences stem from C++’s greater emphasis on type checking. Well-written C programs tend to be C++ programs as well."

Two examples:
- "C allows transfer of control to a labeled-statement to bypass an initialization; C++ does not."
- "In C, an array can be initialized by an initializer that has more elements than the array requires."
Also, you can do the following things in C but not C++
int i;int i;int i;

unsigned k;

there's also a lot of c99-stuff that hasn't been taken into c++ (yet?).

learning c isn't just about learning what c++ is and what c is not, but it's about learning a different, less object oriented code style. i did the c++ -> c -> c++ path myself, and i learned a lot from it. it's a good experience.

edit: another difference (iirc) is that in c, int is implicit. this means that you can just write something like "i;" if you want an integer called "i". this isn't allowed in c++.

This topic is closed to new replies.

Advertisement