Going from C++ to C, what do I need to know?

Ok, so I know some C++ (not extensively, but I took two classes that covered through classes, basic enumeration, and basic vector manipulation [not that I remember anything about enumeration and vectors lol]), but the university I'm going to be transferring to used straight up C in several of their classes.

I know C++ is derived from C, but what major differences/obstacles should I know about when going backwards like this? From what I've read a lot of the syntax is the same or similar, so I just need something that'll give me a quick rundown of what I need to know to code in C without too much hassle.

Thanks!

~i31337
No classes, no STL, no templates.
No member functions (all functions must be global)

No references (but pointers are still OK)

No new/delete

No namespaces

structs exist in a separate namespace, so you usually will want to typedef them:

1
2
3
4
5
6
7
8
9
10
11
12
struct A
{};

typedef struct
{} B;

int main()
{
  A a;  // OK in C++, error in C
  struct A a;  // OK in both C, C++
  B b;  // OK in both C, C++
}
Last edited on
closed account (Lv0f92yv)
Memory allocation is done with malloc(...), deallocation done with free(...)

input output is different, done primarily with printf/fprintf and scanf/fscanf (see man pages on those)

file handling is a little different, uses FILE* pointers with fopen(...) and string constants "r", "rb", "w" etc to specify file modes.
For string manipulation, C++ has <string> and <regex>, the latter being a C++0x feature.

For string manipulation, C has <string.h>.

-Albatross
Ok, thanks guys! It doesn't seem like it'll be too hard to pick up on as I go then, so I'll skip the C class they have there then. It still kind of baffles me as to why they're using C. Apparently they used to use C++ but have since reverted to using C in their classes again.
The real difference, beyond convenient syntax, is how you think about solving problems. C++ allows (but of course, does not insist) one to think in terms of objects, and objects being versions of other objects (inheritance). C makes such things much harder to implement (not quite impossible; the first C++ compiler of note, CFront, worked on the principle of turning C++ into some horrible C code and then compiling that C).

In the same way that it's common for people used to coding in C to write C in C++ (and indeed, I've seen FORTRAN and COBOL written in C++), a tricky thing for an experienced C++ coder, used to objects and classes and inheritance etc.) will be changing how to think about the problem in terms of the programming language space.

C is by no means a bad choice of programming language; it's relatively simple in syntax, forces the coder to have a solid understanding of what's actually happening in terms of memory, provides a basis in syntax to go on to C++ and Objective-C (which languished for a while but is now on the rise, which is nice - I've been using it for a few years and it's really quite charming in contrast to C++), and it's everywhere. They have decided that C suits their purpose better than C++, and I can imagine many cases where that would be a good decision.
Last edited on
Did anyone mention yet that C doesn't allow you to overload functions?
Did anyone mention yet that C doesn't allow you to overload functions?


You can find out by reading the previous posts.
Topic archived. No new replies allowed.