C For The C++ Programmer

Started by
25 comments, last by Wyrframe 16 years, 10 months ago
MSVC++ (Including the express versions) can be set to compile code as C instead of C++. This takes care of all the places where valid C isn't actually valid C++. (It's not C99 though...)
Advertisement
There are some nice perks in C (99). Designated initializers and variadic macros are among my favorite features. Void pointer casting is implicit. There's no name mangling to worry about. Here's a detailed list of differences between C and C++.
Although I do most of my coding these days in C++ or more recently, Java, I really enjoy C. It is simple, straightforward, and if used properly, can be extremely readable and fun to program in. Best of luck!
You can do exception-lite in C with setjmp/longjmp. Which is what __try and __finally expand to anyway, if I recall correctly (certainly TRY and EXCEPT do).

Try coding in Symbian OS, C++ with extra C style paranoia. Oh my!
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
The thread seems to be about differences in the syntax of the language, and overlooking that there is a completely different thought process in C and C++.

Both languages have had two major updates to the ISO standards and have diverged from their common ancestry (the C89 standard). C's last update was in 2004, c++ in 2003, and both have additional changes in the works. C++ isn't just "C with extra stuff", nor is C just "C++ with the good stuff ripped out", they are both distinct languages with their own ISO standards.


Well-written c++ code relies heavily on things like the STL, virtual functions, and inheritance chains. These features do not exist in c, although you can fudge them in if you try. Your main goal in C++ is to specialize out a solution from something that mostly works, promote specialized behavior when it is common, and rely heavily on abstractions.

Well-written c code does a lot of things that are very illegal in c++. These include implicit casting, implicit declarations, and direct object initialization. Many well-written C applications and libraries rely on const correctness to a far greater degree than their c++ counterparts, which can be another learning curve when starting out. Your main goal in C is to operate on structures in a more general way, adding independent functionality as separate entities rather than inter-related specialized parts. It relies less on abstracted code and more on direct manipulation.


In short, it isn't just about syntax, it is a different methodology.
Quote:Original post by Lode
C has got realloc, allowing you to resize the size of a buffer or array. AFAIK, C++ has no equivalent in its new/delete syntax to do this as easily.

Don't use this code, but this is basically how realloc would work in C++.
template <typename T>T *CPlusPlusRealloc(T *ptr) { T *np=new T(*ptr);delete ptr;return np; }template <typename T>T *CPlusPlusReallocN(T *ptr,size_t n){  T *np=new T[n];  std::copy(ptr,ptr+n,np);  delete[] ptr;  return np;}


C is evil and I hope I never have to go back to it. I'm done with the world of no smart pointers.
Programming since 1995.
Quote:Original post by frob
It relies less on abstracted code and more on direct manipulation.

You can do that just as easily in C++ as in C, the only reason why it is discouraged in C++ is that C++ has better alternatives. Alternatives such as better support for abstractions and encapsulation.

You can apply C thinking to C++ but you won't be using its full potential. You can try to apply C++ thinking to C but there's only so much that could emulate that way.

They are different languages with different approaches, but C++ covers most of what C can effectively be used for. Given that the availability of compilers isn't a problem.
Programming since 1995.
Quote:Original post by T1Oracle
Quote:Original post by frob
It relies less on abstracted code and more on direct manipulation.

You can do that just as easily in C++ as in C, the only reason why it is discouraged in C++ is that C++ has better alternatives. Alternatives such as better support for abstractions and encapsulation.

You can apply C thinking to C++ but you won't be using its full potential. You can try to apply C++ thinking to C but there's only so much that could emulate that way.

They are different languages with different approaches, but C++ covers most of what C can effectively be used for. Given that the availability of compilers isn't a problem.


That was exactly the point I was trying to make in my comment.

Yes you can emulate one language's approach using the other, but acknowledging that fact means you implicitly acknowledge they use different approaches.

The OP was asking about what is involved with learning it as an additional language, not the availability of compilers.
Learning C as a C++ programmer is easy, in my opinion. Here's a quick sketch of differences:

* No classes. Structs can't have constructors or member functions.

* Everything that is a struct must be prefixed with "struct", every time it is declared as a variable or in a parameter list. C programmers fix this by using typedefs: typedef struct tagMYSTRUCT MYSTRUCT; Then MYSTRUCT is enough to declare a struct var.

* No references, only pointers.

* No STL. You want dynamic arrays, write code yourself every time, or write a series of reusable functions to minimize code duplication, and therefore a margin for error.

* No single-line comments, unless you are using Pelles C IDE, which is what I use for C development.

* If you are using COM functions, you are in for some fun. You have to call global versions of interface functions explicitly and pass the interface pointer to them, instead of familiar C++ way of simply using -> to refer to COM interface functions.

Well, I think that's about it. I wrote two medium size programs in C, one of them was a database consisting of several files. I used windows functions such as CreateFile for file I/O since the beginning of time, so I didn't notice the absence of file streams. I also typically use malloc/realloc/free in C++ instead of new, so I didn't notice the absence of new either.
Quote:You can try to apply C++ thinking to C but there's only so much that could emulate that way.
Actually, there is nothing C++ can do that you can't 'emulate' with C. The first C++ compiler actually compiled to C code.

This topic is closed to new replies.

Advertisement