LWN.net Logo

Why use C?

Why use C?

Posted Jun 29, 2012 13:45 UTC (Fri) by tshow (subscriber, #6411)
In reply to: Why use C? by niner
Parent article: Why learn C? (O'Reilly Radar)

Because C is available on every platform you might wish to target, which is not true of any other language.

Want to be on Linux? *BSD? Windows? OSX? C is there for you.

Want to run on Wii, PS3, XBox 360, Nintendo DS, DSi, 3DS, PSP or PSVita? C works on all of them, and is the native language of the API on most.

Want to be on iOS, Android or Blackberry? C works fine on all of them.

No other language lets you hit all of those platforms without major hassle. Even C++ is more trouble than C, partly because of implementation differences and partly because in some cases (ie: iOS, where the native language is ObjectiveC) you're working with the fact that the native language of the platform API is one of C's superset descendants.

Our game engine runs on most of the platforms above, with more than 95% of the code being common. The iOS port took almost no time at all, because pure C builds just fine in ObjC. If you want to see Stockholm Syndrome in action, read the forums where people were building C++ projects as ObjC++ on iOS, or (if you have access) trying to get STL working usefully on Nintendo DS. Both can (and have) been done, but the effort expended in the process was instructive.

C also has another advantage; while it lacks some "modern" conveniences (ie: stuff that has been available in LISP since ancient times), that lack makes it easy to do some things that are difficult in most higher level languages. Sometimes you really do need to do low-level memory management, for example, and while garbage collection is an excellent thing to have, not every language that provides it lets you carve off a contiguous chunk of memory and manage it directly.

C also understands binary data natively, which can be a serious advantage in some situations. The perl-esque pack() and unpack() disaster many languages seem to rely on for accessing binary data are awful compared to read(file, &record, sizeof(record));.

None of this is to say that C is some sort of ultimate ideal language. When I'm going to do heavy string processing, or have to deal with yaml or xml or a spreadsheet full of data I'll usually reach for Ruby. I love using lisp and variants when I can. I'm really liking the direction Go is headed in, and I'm somewhat hopeful it can finally be the descendant of C that we should have had 20 years ago.

C also has weaknesses you need to compensate for if you're going to use it cross-platform, chiefly related to data type sizes and alignment.

But C is available everywhere, and it does the job. That really isn't true of any other language currently.


(Log in to post comments)

Why use C?

Posted Jun 29, 2012 19:42 UTC (Fri) by dashesy (subscriber, #74652) [Link]

Good points! C is instruction-agnostic assembly language. You can write C code to run on almost any micro-controller and the result is almost the same as direct assembly. I used C for 8951, because assembly code is hard to remember and maintain and saving few bytes is not worth it. If I want to target a *real* embedded system with known and limited memory, or where code reuse is not good (a firmware) I choose C, but trying to use C to write GUI is insane.

I think part of the reason many people are scared of Linux desktop is the old crappy looking GUI toolkits they saw 10 years ago in their college, and modern and shiny GUI like Qt needs a higher level languages.

Why use C?

Posted Jun 30, 2012 1:04 UTC (Sat) by Wol (guest, #4433) [Link]

If you're targeting hardware with little ram, use Forth! :-)

Anecdotes again :-) but a competent Forth programmer should be able to produce code that's smaller than the equivalent assembly!

A Forth executable is, by it's very nature, stripped of pretty much all extraneous stuff, and very compact into the bargain. So much so that it's very hard for an assembler programmer to keep their version down so small.

Cheers,
Wol

Why use C?

Posted Jul 6, 2012 14:09 UTC (Fri) by jflasch (guest, #5699) [Link]

Why is "C is available everywhere" it seems this discussion about C has missed this critical point and I wonder why ?

OK the answer "C" major design point was to write a language that was easy to build a compiler for thus "C" was the most successful language to be ported to new hardware out there. Because it had no strong copyrights and easy to use compiler tools like Lex, Yacc it was successful. The language it's self is no big deal, but the portability was.

I'm amazed that no one in this long discussion pointed this 1 big fact out "C" became popular not because it was better , but because it was there and that was due to it's major design point, it was easy to build a compiler for. I guess programmers don't know history well.

Why use C?

Posted Jul 6, 2012 14:37 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link]

Pascal compilers are even easier. Yet Pascal was never really successful.

Why use C?

Posted Jul 6, 2012 15:16 UTC (Fri) by jflasch (guest, #5699) [Link]

"C" gave away the yacc and lex syntax Pascal was based on ALGOL 60 which was not so easy of a language, Pascal was highly popular as a PC language but the Unix world never came to it, so I guess "C" also had this Unix bias that kept it going along with easy porting. GCC was a large part of the porting thing, of course. It's funny that the GNU pascal compiler is written in C, then so is Perl and how many other languages , I still think this was more about compilers (IE being there) then syntax.


Why use C?

Posted Jul 6, 2012 17:37 UTC (Fri) by BlueLightning (subscriber, #38978) [Link]

Pascal compilers are even easier. Yet Pascal was never really successful.

Depends how you define successful. Delphi was quite a successful, at least in terms of people using it to produce real software, and that was based on Pascal.

Why use C?

Posted Jul 10, 2012 9:03 UTC (Tue) by anselm (subscriber, #2796) [Link]

Pascal compilers are even easier. Yet Pascal was never really successful.

Yes, but that didn't have anything to do with the compiler – it was really because, unlike C, Pascal was originally supposed to be a teaching language, and lacked many important features it would have needed to succeed in the »real world«, like the idea of libraries or a workable string type. Pascal in its original, official form as described by Jensen and Wirth was good for writing toy programs but not a lot else.

Many of the missing features were added after the fact by various Pascal implementations, but there was little if any standardisation of these extensions. When Donald E. Knuth implemented the second version of TeX in the early 1980s, he solved that problem by basically inventing a new language, WEB, that compiled to his system's version of Pascal, but he still had to jump through various fiery hoops to make that work. (The first version of TeX had been written in a different language altogether.)

As a matter of fact, also in the 1980s Niklaus Wirth, the inventor of Pascal, came up with an improved version of Pascal called Modula-2, which corrected some of the more egregious errors and oversights of the original and was in fact intended to be a language for serious development. However, by that time everybody was becoming sold on the idea of OOP, which Modula-2 quite intentionally didn't support, so that language didn't really go anywhere, either.

Why use C?

Posted Jul 10, 2012 11:59 UTC (Tue) by dgm (subscriber, #49227) [Link]

From my days of Modula2 and Pascal, I vividly remember the feeling that the languages went through leaps and bounds to -limit- you. In hindsight, it probably is a good idea if what you want to do is code from 9 to 5, and then go home to play with your family. But the fact is that the feeling pissed me off then, and was one of the reasons I preferred C and later C++.

Why use C?

Posted Jul 10, 2012 12:52 UTC (Tue) by Cyberax (✭ supporter ✭, #52523) [Link]

For me, in hindsight, almost none of Pascal's or Modula's limitations make sense. They just seem petty and trivial.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds