Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×
Programming

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."
This discussion has been archived. No new comments can be posted.

What's To Love About C?

Comments Filter:
  • Re:because (Score:5, Informative)

    by localman57 ( 1340533 ) on Monday July 02, 2012 @01:50PM (#40518947)
    No, he's right. On systems where your constants exist in a different medium than your variables (such as microcontrollers where variables are in RAM but constants are in flash), declaring a string as const or not const can have a big impact on what resources you eat up. Typcially, there's often a #pragma or non-standard keyword such as ROM that goes along with this.
  • Re:Good habits (Score:5, Informative)

    by localman57 ( 1340533 ) on Monday July 02, 2012 @01:53PM (#40518967)
    Exactly. My company does a lot of different things from embedded systems to web interfaces, and, generally speaking, the C guys write better Java code than the Java guys write C code.
  • Re:because - (Score:5, Informative)

    by 19thNervousBreakdown ( 768619 ) <davec-slashdot@@@lepertheory...net> on Monday July 02, 2012 @02:01PM (#40519059) Homepage

    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.

  • by slew ( 2918 ) on Monday July 02, 2012 @02:01PM (#40519065)

    ...and not some VM? Most of the popular languages these days are all dynamic. And they are very convenient and nice. But if you actually want to know what the machine is actually doing, and want to have a say in such things, C is the way to go.

    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)

    by Anonymous Coward on Monday July 02, 2012 @02:04PM (#40519107)

    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)

    by AuMatar ( 183847 ) on Monday July 02, 2012 @02:10PM (#40519171)

    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)

    by DanTheStone ( 1212500 ) on Monday July 02, 2012 @02:12PM (#40519199)
    Thank you. "const" modifies the thing that preceded it. Iff nothing precedes it, it modifies the first thing that follows it. It's not terribly complicated.
  • by Rising Ape ( 1620461 ) on Monday July 02, 2012 @02:31PM (#40519413)

    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.

  • by stanlyb ( 1839382 ) on Monday July 02, 2012 @03:03PM (#40519793)
    And don't forget, the only reason for Objective-C to have these features at run-time is also the only reason why Obj-C is 10 times slower than any other language.
  • by hxnwix ( 652290 ) on Monday July 02, 2012 @03:15PM (#40519971) Journal

    > 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).

  • by Anonymous Coward on Monday July 02, 2012 @04:14PM (#40520743)

    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)

    by dmbasso ( 1052166 ) on Tuesday July 03, 2012 @03:56AM (#40524863)

    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.

One man's constant is another man's variable. -- A.J. Perlis

Working...