What's To Love About C? 793
First time accepted submitter edA-qa writes "Antiquated, clunky, and unsafe. Though beloved to some, C is a language that many choose to hate. The mass opinion is indeed so negative it's hard to believe that anybody would program anything in C. Yet they do. In fact a lot of things, even new things, are programmed in C. The standard was recently updated and the tools continue to evolve. While many are quick to dismiss the language it is in no danger of disappearing."
Re:because (Score:5, Informative)
Re:Good habits (Score:5, Informative)
Re:because - (Score:5, Informative)
That's because there is no difference! I think you meant:
char* const foo;
for the second one. const modifies the item to the left, unless it occurs at the beginning of the line, in which case it modifies the item to the right.
Re:Maybe because it compiles down to the metal... (Score:5, Informative)
I mean, unless you want to, you know, use pascal or fortran or something.
Although "C" compiles down really very close to the metal, so does "C++" (and a host of other more modern languages). However, it's not that easy to take that next step to the metal. In between is a machine that virtualizes the registers (using register renaming techinques), virtualizes the memory (using difficult to predict caching, translation, and ordering operations), and reorders the instructions (speculation, branch target hiding, etc), and runs in a sandbox (under the os which is timeslicing between tasks and faulting and swapping in memory translations and maybe even simulating some instructions).
Knowing what the machine is actually doing is often a mythical quest down the rabbit hole. Although I'm a big fan of "C+" (what I call the c++ subset that doesn't have all the crapola that I don't use**), I'm under no illusion that all you are really doing with C is using a more predictable translation strategy (e.g., static compile time translation) rather than some magical "metal" commanding language.
** +objects, +const, +templates, +-stl (some are okay), -overloading, -virtual inheritance, -rtti, -boost, etc...
What a crock.... (Score:2, Informative)
Who are these people that hate C? A tool is a tool and some are better suited to a job then others. Even COBOL has it's place. If you want something to compile to true binary code that runs very fast on the target hardware, then C is a great tool. If you want to slam out some code that will sort of run and look okay to the user, how about Visual Basic? Pascal has it's place as does C++ and even ASP.
Re:because - (Score:5, Informative)
And the funny thing is that most people who write const char* foo really want char const * const foo. You don't want either the pointer or the data pointed at to change. However, almost nobody knows that, so even those who do just use the weaker const char* so people understand the code.
Re:because - (Score:5, Informative)
Re:Fortran is better. (Score:4, Informative)
The main problem with Fortran is Fortran programmers. Or, more specifically, those who started off with F77 or earlier and carried on doing things the same way. For numerical stuff, I prefer F90 or later to C - far fewer ways to shoot yourself in the foot with memory management.
Re:One good reason... (Score:4, Informative)
Re:Maybe because it compiles down to the metal... (Score:4, Informative)
> How do you add two ints? How about two floats? Why is overloading bad?
Add the string "0.5" to the string "1.0." with the '+' operator. Is the result "1.5" or "1.00.5" ?
Is there an easy way to find out based on the syntax of the language, or do you have to dig into the class libraries you're including which may contradict each other?
Always "1.00.5" unless you or one of your includes declared some extremely evil operators.
Because they can lead to surprising behavior, c++ operators are usually defined with care so that they won't be accidentally invoked and don't do strange things when they are used (such as interpret strings as floating point values).
Re:One good reason... (Score:0, Informative)
http://www.mikeash.com/pyblog/performance-comparisons-of-common-operations-leopard-edition.html
C++ virtual method call 1.1 ns
Objective C message send 4.9 ns
4.9/1.1 ~ 4.5 times slower.
I don't know where you're getting 10 times slower from.
Re:because (Score:5, Informative)
Don't know much about C, do we? ;-)
He does, you not so much.
If I saw the above declaration, I wouldn't be at all surprised to see a later command like:
post = "second"; // Or perhaps some computed value
If the declaration had contained a const, this would be a syntax error, since the variable post has been declared a constant.
Nopz, what was declared as constant is the memory the pointer is pointing to. Example:
1: void f() {
2: const char *a="bla";
3: a = "blu";
4: a[0] = 0;
5: }
>gcc -c t.c
t.c: In function ‘f’:
t.c:4:2: error: assignment of read-only location ‘*a’
Tip: avoid using snarky comments. When you're wrong it makes you look specially stupid.