Game Development Community

why would anyone use C?

by Grant McNeil · in Technical Issues · 07/05/2002 (12:37 pm) · 15 replies

hey,

Just a question to any C programmers out there, why use C? I have always used C++, and I have never wanted to learn C. I guess if that was the language you first used, then you would tend to use it, but i havn't found any advantages of using C instead of C++.

Why use C?

-Grant McNeil

#1
07/05/2002 (12:44 pm)
All i can think of is...

Its faster, especially if you want to make an emulator. If you code it in c++, and do things like put the CPU core in a class, it'll go slower than just doing it straight out in C (I've heard there is some sort of processor overhead using classes).
#2
07/05/2002 (12:52 pm)
C is as close as you can get to the iron without using assembler, and when it's performance (which it is in a 3D engine's core) then every cycle counts.

Also, C is a procedural language, C++ is object-oriented. Despite the marketing hype, OO is not the answer to all problems, and some things are simply solved better in C than in C++. (yes, you can rape C++ and write what's essentially C code in it, but when you do that, why not use C right away?).

Finally, some people simply prefer it. I'm one of 'em. Maybe it's because I learned it first, maybe it just relates better to the way I think, maybe because it's similiar to the other languages I use regularily (PHP or Perl).
#3
07/05/2002 (6:07 pm)
In addition... Good Cs are available for most of the weird little platforms. If you're doing embedded systems programming, your choices are C or ASM.

C++ compilers are _massive_ undertakings compared to C compilers. Very few embedded systems need the power of C++ (Look! My egg timer was designed using UML. I've a complete OOP design!), or else need to be more reliable than C++ allows (example: a nuclear reactor control system needs to be extremely reliable. As part of the validation process, it needs to be demonstrated that the code will always fail safe. C++ adds an extra layer of (potentially unvalidatable) of abstraction. Simpler is better, QED you want to write in something like C).

On a more relevant note, most of the time it really doesn't matter. I don't think you're going to run into a lot of people that code C, only C, and are unwilling to ever learn any other language ever. You want to use the language that fits the task...
#4
07/06/2002 (4:41 am)
Speaking as someone who learned C and programmed it in for years before switching to C++...I'd never want to go back to C!

When I first started learning C++, I disliked it.. in retrospect it was mostly because it forced me to do things in a different way than I was used to..Really more of an OO vs Procedural thing than C++ vs C... But as the language matured and I got more and more exposure to C++ and OOP in general there was some point where everything clicked into place and I was fully converted.

In any case, most of the speed arguments just aren't true anymore. If you know what you're doing you can write C++ code (including class usage) that is just as fast as C. In fact, in some cases it can be faster because the stricter type safety of C++ can sometimes allow for tighter optimizations than a legacy C compiler could safely manage. Yes you take a speed hit if you use exceptions, rtti, virtual functions, etc, but you don't need to use them... So you can save those features for the non-critical parts of your code and get the best of both worlds. You can also do really cool metaprogramming with C++ templates that allow for cool compile-time optimizations that would be much harder (if not impossible) to pull off with C's weak preprocessor.

There are certainly places where C (or asm) is still king, such as the small embedded systems, but as someone who primarily programs for the PC I'd never switch back to straight C.
#5
07/06/2002 (4:28 pm)
Why would anyone care if someone would use C instead of C++ ?
#6
07/08/2002 (2:58 pm)
Because if your doing a game devolpment project, its probably only going to use one languge (depending), and people who use C could be losing a good C++ programmer who doesn't know C.
#7
07/08/2002 (4:00 pm)
I would hope anyone who is a C++ programmer can also program in C, since C++ is mostly a superset of C (not exactly, there's a few pathological cases where you can write the same code in C or in C++ and have a different meaning based on the standard syntax of each).

Of course, the C++ programmer writing C is probably going to be fighting C the whole way, reinventing methods of doing object orientation using struct handles, class-like naming schemes and a 'this'-like parameter to grouped functions.
#8
07/08/2002 (6:46 pm)
C++ has a great deal of overhead that you don't tend to see or even know it occuring, and it can be different amounts depending on what compiler you're using.

I was told once that you really shouldn't use a feature of a language unless you actually know what it's doing, and unfortunately a lot of the time I *don't* know what C++ is doing. V-Tables, const this pointers, and most importantly *where* it stores this stuff can be a mystery too me if you get too far into the code.

As for C programers being able to program in C++, yeah, they can do it, but it won't really be "C++" it would be C, with some attempts to do the OO that's in C++ or the STL if they're feeling praticularly adventurous.

C, by it's nature is a smaller, more compact language that really gives you all the choices in implementation. If you're coding c++, you're usually leaving some of the implementation up to the compiler.

... and as my friend so aptly put it:

r341 pr0gr4|\/|3rz us3 C!

:)
#9
07/09/2002 (12:12 am)
All of your arguments could be used to promote assembly over C. And while C++ certainly is 'heavier weight' than plain C, you gain a lot in return for a small amount of overhead, just like when you use C with a good compiler over assembly.

In any case, anyone who hopes to get a professional job in the game industry would do well to learn C++ as well as possible. Just look towards Torque, Doom3, to get a glimpse of the future. The primary issue with C++ (at least over the past 4 years or or so) hasn't really been the overhead (which is very often overblown by people who just dislike C++/OOP), but the standards compliance of available compilers, and this point has been improving quickly recently, though it still clearly has some ways to go.

Don't become like one of those people who everyone laughs at because they still write entire (almost invariably crappy, as it happens) games in assembly just because you have some religious bias against C++.
#10
07/09/2002 (7:45 am)
Ah, the invariable C versus C++ debate... maybe in another 5-10 years, this argument will become moot as the improved compilers (hopefully) converge and render the differences negligible.

And yes, as George has stated a few times, the speed and overhead argument carries less weight now.

As for my personal usage: if I need to create a large application or if it will be potentially be maintained by someone else, I tend to use C++. For small efforts, or for efforts requiring some driver work, I tend to use C. This reminds me of the vi versus emacs debate.
#11
07/09/2002 (12:02 pm)
It's all about data structure. Pick your poison.
#12
07/09/2002 (3:22 pm)
@george: Yeah, all of my arguments work for asm over C. The key difference is that standard C is portable, whereas asm is not. I was also told that little snippet about C++ from a really good asm / C programmer. He really believes (and I happen to agree) that if you understand what the code is doing down by the processor (asm level if you will), your code tends to be inherently better (not always).
#13
07/09/2002 (4:02 pm)
There are actually a few different flavors of portable assembler available that you could use instead of C, if you were so inclined. Personally I don't think that portability is the only selling point (in fact I don't think its even the main one) of C over asm. C is just much easier to work in to get things done. If you take a 1% speed hit but speed up your development time by 50%, its almost always worth it to do that. Not to mention that C is thousands of times easier to maintain than assembly, especially if you weren't the author of the original code.

Likewise I think C++ offers similar advantages over C. For those who really enjoy working in C, that's fine, I won't fault you for that, but it really irks me that the C diehards tend towards making baseless claims about the efficiency of C++ that in most cases were never true and in some cases were true when C++ was immature but aren't true anymore.

Also, I don't buy into the argument that C++ hides too much from the programmer unless you qualify it by saying it hides too much from the newbie C++ programmer. For those who take the time to really learn the internals of C++ and understand things like why ++iterator is a better choice than iterator++ in a tight for loop, the under-the-hood goings-on of C++ are just as clear as when working with C.
#14
07/10/2002 (6:49 am)
yeah, and if you understand all of that it's fine. I have nothing against those who choose to program in plain C and those who program in C++. As I said, I'm a C++ programmer, but until recently I've shyed away from templating and STL because I didn't fully understand what the compiler was doing or how it was implementing things. Now that I understand better, I'm starting to use various portions of it.

The problem I have is when people start using the more advanced features of both C / C++ / STL without a true understanding of what they're doing. It tends to make sloppy code and problems that are hard to come out of. In my experience it's not the "how" so much in programming as much as it is the "why". C has its advantages, just like C++ has its advantages. If you want to avoid the overhead of C++ classes, you program in C and use structs. A friend of mine actually had to do this to get around some interesting limitations of old Mac systems.

ASM also has its advantages over C if you know what you're doing. Its far better for bit by bit manipulation and tight, fast instructions. If you want your speed and customizability, you play in ASM.

Had some sort of point about John Carmack and OGL extensions, but I lost it. Argh. :)
#15
07/13/2002 (3:07 am)
Heh, Carmack's antisocial carcass lives only a stone throw away (assuming you arent some kind of gimp or nancy boy). He never answers any programming or personal questions, but I understand why. I've heard he passionately debates that C is better than C++ for game programming, but he really does not seem like the passionate type. Whenever he actually makes an appearance at a LAN party, he sets up and manages his affairs rather than enjoy a game or two.

I'm not really all that interested in one man's opinion anyway, and neither should you. We -should- all choose from experience and preference. It is recommended to start with C, then move on to C++, then if you are a sell-out slave to bill gates, you can attempt c#. Mind you, I know nothing about C# except that it's supposed to be 'easy.'

Easy and Good are usually opposites.