I'm looking for arguments for a discussion on why someone would want to still use C nowadays. This question will inevitably come up at my bachelor's exam and I would like to answer with something other than just "because the project was started in C 10 years ago".
I know the kernel guys like C for it's explicity which makes it simpler to predict the code generated by the compiler and thus it's performance implications.
Posted Jun 29, 2012 8:57 UTC (Fri) by metan (guest, #74107)
[Link]
Well perhaps some of us are comfortable with plain C. Don't get me wrong I could handle variety of languages and each has its place, but when I'm about to write fast algorithm that reads data, does some calculations and writes the result, C is most of the time the choice (because there is nothing that C++ could do better).
And also note that C is not dead language, the standard is evolving C99 is much better than C89 and C11 even has 'type generic expressions' heck the syntax is wrong but its there.
Not to mention that combination of C and inline assembler is best choice for small MCUs where space and timing really matters.
Why use C?
Posted Jun 29, 2012 11:39 UTC (Fri) by alankila (subscriber, #47141)
[Link]
What C11 brings to table might be interesting 10 years from now, seeing that C99 still isn't acceptable for many cross-platform projects because the compilers do not implement it.
Why use C?
Posted Jun 29, 2012 11:55 UTC (Fri) by niner (subscriber, #26151)
[Link]
Well compilers do. Microsoft's Visual Studio C compiler doesn't. And as they've pretty much officially confirmed, that they won't support C99, chances are, they will never support C11 either. So in 10 years, the situation will be pretty much the same as today: either you want to support this compiler and adhere to an ancient C standard or you don't and have access to a language that's quite ok.
With gcc being available for Windows for free and the possibility to compile as C++ in VC, I really don't see the point of restricting oneself to the stone age.
Why use C?
Posted Jun 29, 2012 16:31 UTC (Fri) by tshow (subscriber, #6411)
[Link]
There is to the best of my knowledge no gcc for Microsoft's hardware platforms. If you need to build on XBox 360 or WinPhone, gcc isn't there to help.
Why use C?
Posted Jun 29, 2012 13:45 UTC (Fri) by tshow (subscriber, #6411)
[Link]
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.
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.
Why use C?
Posted Jun 29, 2012 15:10 UTC (Fri) by etienne (subscriber, #25256)
[Link]
C is also abused more easily than C++, and it is maybe what you need (for a kernel).
I mean, to run a C application you should have a linear memory which is always accessible, you should have a predictable flow of execution, and few more "obvious" things (like having the same assembler instruction set during the execution of the whole program - no switch from ia32 to amd64).
Unfortunately for kernels, there are a lot of types of memory (virtual,physical,kernel,user,not-present,...), there are non-predicatble flow of execution (interrupts, processor exceptions which are completely different to C++ exceptions), you cannot rely that your stack is "as assumed" by the language - so most of the time you cannot use the cleverness of a language more advanced than C.
For instance, it would be very difficult to handle a C++ exception happening inside a processor exception or a processor interrupt, the stack rollback will not go past. Also do you deviver the C++ exception to the kernel or to the user mode application?
Obviously you do not want to garbage collect the virtual memory which is not yet mapped because it does not exist.
Also, there is a lot of different pointers (some of them 48 bits for high memory), you do not want your class to do a memory access without being sure that the right memory segment is mapped - and C++ may hide some access.
Accessing memory with DMAs (and chained DMA) may also have strange behavior not miximg well with the "detail hiding" of C++.
Was my £0.02...
Why use C?
Posted Jun 29, 2012 22:00 UTC (Fri) by daniel (subscriber, #3181)
[Link]
If you don't know exactly what code your compiler produces, whether C or C++, maybe you should seek some more forgiving project than the kernel.
Why use C?
Posted Jun 30, 2012 8:44 UTC (Sat) by HelloWorld (guest, #56129)
[Link]
> If you don't know exactly what code your compiler produces, whether C or C++, maybe you should seek some more forgiving project than the kernel.
That's ridiculous. Nobody knows exactly what code exactly a modern compiler will produce for any non-trivial piece of code, including the compiler's authors.
Why use C?
Posted Jun 29, 2012 22:03 UTC (Fri) by daniel (subscriber, #3181)
[Link]
"it would be very difficult to handle a C++ exception happening inside a processor exception or a processor interrupt, the stack rollback will not go past. Also do you deviver the C++ exception to the kernel or to the user mode application"
None of the above. You compile with exception handling disabled.
Why use C?
Posted Jun 29, 2012 22:10 UTC (Fri) by daniel (subscriber, #3181)
[Link]
"you do not want your class to do a memory access without being sure that the right memory segment is mapped - and C++ may hide some access."
C++ does not have a monopoly on hiding memory accesses, or bugs for that matter. I have even heard of such things cropping up in C code, in the kernel even. Improved programming discipline usually seems to be the cure.
Why use C?
Posted Jun 29, 2012 16:12 UTC (Fri) by cmorgan (guest, #71980)
[Link]
I ask myself the same question all of the time. Lots of programmer time is being dumped into backfilling a language like C that doesn't exactly help much with string management, memory management etc.
There are a TON of people that haven't bothered to learn about other languages, including C++.
The other issue I see (from embedded software development) is that some companies are using some really constrained systems that can't support virtual machines or interpreted languages.
Why use C?
Posted Jun 30, 2012 8:56 UTC (Sat) by khim (subscriber, #9252)
[Link]
I ask myself the same question all of the time. Lots of programmer time is being dumped into backfilling a language like C that doesn't exactly help much with string management, memory management etc.
Joel answers it best, as usual: I am actually physically disgusted that so many computer science programs think that Java is a good introductory language, because it's "easy" and you don't get confused with all that boring string/malloc stuff but you can learn cool OOP stuff which will make your big programs ever so modular. This is a pedagogical disaster waiting to happen. Generations of graduates are descending on us and creating Shlemiel The Painter algorithms right and left and they don't even realize it, since they fundamentally have no idea that strings are, at a very deep level, difficult, even if you can't quite see that in your perl script.
There are a TON of people that haven't bothered to learn about other languages, including C++.
That's separate issue. People often forget that efficiency or the program in not the target function. Target function is "efficiency of programmers PLUS efficiency of user of said program". If there are millions of users then C makes perfect sense, but when there are only dozens of users (e.g. when said users are programmers involved in the same project) then efficiency of programmer becomes more important and there are plethora or languages which try to improve it (at the expense of resulting program efficiency and thus efficiency of user).
The other issue I see (from embedded software development) is that some companies are using some really constrained systems that can't support virtual machines or interpreted languages.
This, again, shows the same basic misunderstanding. It may be cheaper to buy dozen of high-end appliances and save on programming time. But if you want to produce millions of them then additional month spending on C programming can easily save millions of dollars in the end.
Why use C?
Posted Jun 29, 2012 18:00 UTC (Fri) by apoelstra (subscriber, #75205)
[Link]
You can fit the entire memory model in your head. You can fit the machine model in your head. It's a very simple, easy-to-use language. (Some languages *cough*PHP*cough* do not even have a consistent machine language.)
It's also complete enough to use any programming paradigm you want, without preferring one version over the other. Many "better" languages make you use class-based OO, if you want OO (and sometimes even when you don't!).
The standard library is sorely lacking, but there are more libraries available for it than for any other language. It has compilers for more platforms than any other language. It can link with any other language since it doesn't require name-mangling, hidden structures, GC tagging, etc.
The type system is pretty irritating, but it has 'const', which many languages lack, which makes reasoning about code a million times easier than without it.
Why use C?
Posted Jun 29, 2012 21:31 UTC (Fri) by tjc (subscriber, #137)
[Link]
Reducing this to a single sentence, C is currently the best available software abstraction of the Von Neumann architecture.
Why use C?
Posted Jun 29, 2012 22:13 UTC (Fri) by daniel (subscriber, #3181)
[Link]
"Reducing this to a single sentence, C is currently the best available software abstraction of the Von Neumann architecture."
According to who?
Why use C?
Posted Jun 30, 2012 18:03 UTC (Sat) by tjc (subscriber, #137)
[Link]
I thought that would be obvious, since I posted it. ;)
Why use C?
Posted Jul 9, 2012 5:21 UTC (Mon) by daniel (subscriber, #3181)
[Link]
Well then I must compliment you on your skillful trolling style.
Why use C?
Posted Jul 1, 2012 6:06 UTC (Sun) by wahern (subscriber, #37304)
[Link]
FWIW, C is carefully defined to work just as well on Harvard architectures. That's why there's no conversions allowed between object and function pointers.
Also, you can't compare pointers which aren't derived from the same object. Which means the address space isn't necessarily flat, either.
Why use C?
Posted Jul 1, 2012 22:44 UTC (Sun) by tjc (subscriber, #137)
[Link]
Yes, I believe you are right. I was actually thinking of the broader definition of Von Neumann vs. Turing Machine rather than the more specific case of Von Neumann vs. Harvard. In other words, a system with a CPU, registers, and random-access memory.
Somewhere in this video he claims that there are only two foundational languages, C and Lisp, and I think that nails it pretty well. I'm sure there are people who would take umbrage with that claim.