That modern languages are way more efficient than C is a mantra that keeps getting repeating over and over but is it really true? I have worked on many different projects and regardless of it beeing gui, no-gui, web site, web services etc I have yet to encounter a situation where C gave my a disadvantage against my colleagues using Java, C# or PHP.
My belief is that what the new programmers see as the main advantage in the modern languages is the framework. But as a experienced C programmer I have a vast framework in the form of shared libraries, both my own but mostly the vast array of libraries in a modern Linux distribution.
Posted Jun 28, 2012 19:49 UTC (Thu) by Otus (guest, #67685)
[Link]
I've yet to see a GUI toolkit/interface that wasn't a pain to use in C.
Other than that, I definitely agree.
Why learn C? (O'Reilly Radar)
Posted Jun 28, 2012 21:22 UTC (Thu) by dskoll (subscriber, #1630)
[Link]
I've yet to see a GUI toolkit/interface that wasn't a pain to use in C.
Sadly, Tcl and Tk are no longer in vogue, but at the turn of the millennium I developed a couple of large C++ (ok, not C, but close) projects that embedded Tcl and used Tk as the toolkit.
Tcl proved to be a terrific embeddable language; its C API is simply wonderful compared to some monstrosities such as embedded Perl. Whipping up GUIs in Tcl/Tk is pure joy.
I don't think we should be afraid to use C where it's appropriate and non-C where it isn't.
Why learn C? (O'Reilly Radar)
Posted Jun 28, 2012 21:43 UTC (Thu) by dashesy (subscriber, #74652)
[Link]
If C++ is in the list, I think Qt beats any other GUI toolkit/interface. I think the OP is talking about plain C.
Why learn C? (O'Reilly Radar)
Posted Jun 28, 2012 22:44 UTC (Thu) by wahern (subscriber, #37304)
[Link]
Tk is written in Tcl, but the C API is a first class citizen, which is how you end up with things like Perl/Tk. And Tk is widely considered a breeze to work with, and (correct me if I'm wrong) may have been one of the first toolkits to handle widget nesting, placement, and resizing automagically.
So, it's a GUI toolkit for C which may be the easiest to use of all of them, even accounting for Qt, Silverlight, etc.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 0:14 UTC (Fri) by dskoll (subscriber, #1630)
[Link]
...handle widget nesting, placement, and resizing automagically
That's correct. Tk's geometry managers (the packer, the placer and the gridder) are still better than any other toolkit geometry managers I've seen.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 8:19 UTC (Fri) by rwst (guest, #84121)
[Link]
And when I recently had a look at Qt I specifically sought
such a placement function, and was quite disappointed that
it wasn't included.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 12:14 UTC (Fri) by richmoore (subscriber, #53133)
[Link]
Qt has quite a decent set of layout managers, which one were you missing?
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 0:13 UTC (Fri) by dskoll (subscriber, #1630)
[Link]
Tk is plain C. I just happened to use it from a C++ project, but it's equally useful in C projects.
Why learn C? (O'Reilly Radar)
Posted Jul 2, 2012 19:43 UTC (Mon) by landley (guest, #6789)
[Link]
C++ is not C. Saying "C++ contains C, therefore C++ is a good language" is like saying "a mud pie contains water, therefore mud pies are a good beverage".
C is in a sweet spot of minimal abstraction: enough to get portability between arm and x86 and such. Not enough to obscure what the hardware is actually doing, but enough that the "how" can get washed through somebody else's optimizer. (The old joke "C combines the flexibility of assembly language with the power of assembly language" is funny because it's true.) C lets you program close to the bare metal without having to care whether a branch delay slot is allowed to cross a cache line boundary this week, or starting all over every time somebody invents a new architecture (of which linux supports dozens: arm for phones, x86 for desktops, ppc for game consoles, mips for routers, sparc and itanic for bureaucrats, ...).
The _other_ sweet spot is up with ruby and python and lua (oh my!), scripting languages which provide thick but reliable abstractions so you can program to those abstractions without knowing how it was implementated. These languages replace pointers with references and on top of that build dynamic memory (garbage collection), and dynamic typing (duck typing), and combine them to put flexible container types into the base language. You don't have to care whether a python dictionary is a hash table or a tree under the covers; it doesn't matter, it just _works_. Scripting languages are so dynamic they do away with compilation entirely and set the executable bit on the source code: maybe they do a just-in-time bytecode compiler pass under the covers, but you can ignore it.
Between "dynamic everything with thick, opaque, reliable abstraction" and "static everything with as little abstraction as possible", you have a middle ground of "mixed static and dynamic with leaky abstractions". The worst of both worlds. Languages like C++ provide all sorts of abstractions like templates and std string types, but the implementation details intrude into the language so you have to understand how they're _implemented_ to use or debug any of them.
There's a difference between having to understand how a language feature is _used_ (ala C code) and regularly having to reverse engineer your compiler and standard template library to debug your problem (C++). In C++ you learn piles of caveats to the base language, "don't throw an exception from a constructor", "don't think you can avoid having to manually initialize every data member in your constructor with memset(this, 0, sizeof(*this));", and of course how a piece of data can live on the stack, on the heap, in the globals, in the process environment space, or be mmaped, each with its own lifetime rules. Good luck if you mix two of them in the same container through an abstraction that did an unexpected call by reference instead of call by value. For exatra fun: templates are turing complete _at_compile_time_, which means there's no way to prove a C++ compilation using templates will ever finish.
C++ never got rid of pointers, and then attempted to build elaborate abstractions on top of them. Since pointers inherently cut through abstraction (it's what they're _for_), these abstractions leak. Making the abstractions _thicker_ is painting over dry rot with more and more coats. The C++ guys keep acting like they can escape a poor foundation by building higher, that adding enough stories will render the sand underneath the whole thing irrelevant. With each new standard the language gets larger the problems with it get harder to understand and explain, which is considered progress.
C is a local peak. Scripting languages are a local peak. C++ is in the valley of suck between the peaks. Sure you can strap rocket engines to a turtle and make it work; people wrote and deployed millions of lines of Cobol and Ada code too. That doesn't make it a scalable, robust, or efficient language.
Every time a discussion of C wanders blithely into C++ as if it's the same thing, somebody doesn't know what they're talking about. And _that_ is what the above interview was about. We are teaching C++ as if that's a replacement for teaching C. It is not.
Why learn C? (O'Reilly Radar)
Posted Jul 2, 2012 21:52 UTC (Mon) by nix (subscriber, #2304)
[Link]
As someone who likes C++ for its expressiveness and power, and who wrote C++ before he wrote C, and OO code before he wrote procedural code, I tried very hard to disagree with you... but...
... dammit, you're right. And I don't just say that because I'm working in the very small subset of code that pretty much *must* be implemented in C for reasons unrelated to efficiency. (You're also working in that subset, which is probably why we agree on this point.)
You *can* write C++ that avoids the traps you describe (and with C++11, it gets easier) -- but you do need to be an expert to do it, everyone who works on the project needs to be an expert too, and you need to avoid almost every piece of example code that exists out there on the Internet as well. Which means that hardly anyone writes good C++. (By comparison, a far higher percentage of C developers can write good C, though the percentage is still small and it still takes the usual time for any human field of roughly ten years to become anything close to expert.)
Why learn C? (O'Reilly Radar)
Posted Jul 3, 2012 15:10 UTC (Tue) by dashesy (subscriber, #74652)
[Link]
everyone who works on the project needs to be an expert too
And everyone who will be working on the project! Forever without a gap, if there is one person in between that does not know most of those traps, it soon becomes unreadable and hard to understand and debug (the worst that can happen to a project). Specially the corner cases (that C++11 promises to solve with first class language idioms) tend to accumulate more cruft. I really appreciate that kernel, and what landley is doing is using C. Whenever code beauty matters (and not time to market), if code does not work it should be replaced, not sub-classed.
Why learn C? (O'Reilly Radar)
Posted Jul 4, 2012 12:13 UTC (Wed) by jwakely (subscriber, #60262)
[Link]
> In C++ you learn piles of caveats to the base language, "don't throw an exception from a constructor",
Nonsense.
> there's no way to prove a C++ compilation using templates will ever finish
Erm, except to maybe try compiling it. Templates are Turing complete, but in practice compilers have limits on how many levels of instantiations they'll handle and rapidly run out of memory (and crash) if you do something stupid/crazy.
And so what? Is the fact that there's no way to prove a C program will ever finish a practical problem? If not, why is the Turing completeness of templates a practical problem?
Why learn C? (O'Reilly Radar)
Posted Jul 4, 2012 12:39 UTC (Wed) by dgm (subscriber, #49227)
[Link]
>> In C++ you learn piles of caveats to the base language, "don't throw an exception from a constructor",
>Nonsense.
Maybe just misinformed. In fact, exceptions are the only way to propagate errors in constructors, you cannot return an error code. But I guess what he really meant was "destructor".
Why learn C? (O'Reilly Radar)
Posted Jul 10, 2012 4:11 UTC (Tue) by smitty_one_each (subscriber, #28989)
[Link]
"For exatra fun: templates are turing complete _at_compile_time_, which means there's no way to prove a C++ compilation using templates will ever finish."
I mostly think you're making a good, substantial case in this article, but the hyperbole blunts the argument.
How much theorem proving have you _ever_ done with code, irrespective of language?
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 10:19 UTC (Fri) by Otus (guest, #67685)
[Link]
I haven't used tcl/tk, so apparently I've missed the one that works.
Trying to read the source of any "modern" GUI app written in C is painful.
(Not that those written in other languages are necessarily much better.)
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 21:48 UTC (Fri) by man_ls (subscriber, #15091)
[Link]
The source of a well structured, OOCSS web page can be surprisingly nice to read. Even adding some javascript it remains good enough to understand on the fly. That is perhaps why web applications are taking the world by storm.
Speaking of javascript, I think that C is good to learn, object oriented is fine, but any developer that doesn't know a functional language is missing out a lot (and doesn't know what is missing). I come from a Java background and I know a bunch of people like that.
Why learn C? (O'Reilly Radar)
Posted Jun 28, 2012 23:24 UTC (Thu) by nix (subscriber, #2304)
[Link]
I wouldn't describe Java, C# or PHP as modern languages in that sense. LuaJIT is at least as fast as compiled C, and Lua as a language knocks the socks off C. (The same is true of a number of other languages.)
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 21:05 UTC (Fri) by tjc (subscriber, #137)
[Link]
> Lua as a language knocks the socks off C.
C doesn't have socks, so I think you must be mistaken.
Why learn C? (O'Reilly Radar)
Posted Jun 30, 2012 5:22 UTC (Sat) by dirtyepic (subscriber, #30178)
[Link]
Socks were added in C11. Unfortunately they were almost immediately knocked off again, which explains why they can't be found in the spec.
Why learn C? (O'Reilly Radar)
Posted Jun 30, 2012 17:25 UTC (Sat) by tjc (subscriber, #137)
[Link]
Yes, and all those C11 socks start with an underscore (_Alignas, _Alignof, _Atomic, &c.) like some of their C99 predecessors. I realize there are reasons for this, but it's turning C into kind of an ugly language.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 5:04 UTC (Sun) by wahern (subscriber, #37304)
[Link]
Most of those have non-prefixed versions which you can get by including a header. For example _Bool has <stdbool.h>, which does #define bool _Bool. For _Alignof include <stdalign.h> and get alignof. <stdatomic.h> does the same for many other types.
In C anything that starts with an underscore followed by a capital letter, or anything which begins with two underscores, is reserved for future language extensions. That way, new extensions won't break existing code. If you want the intuitive names, include the headers.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 1:23 UTC (Fri) by HelloWorld (guest, #56129)
[Link]
> That modern languages are way more efficient than C is a mantra that keeps getting repeating over and over but is it really true?
Yes it is.
> I have worked on many different projects and regardless of it beeing gui, no-gui, web site, web services etc I have yet to encounter a situation where C gave my a disadvantage against my colleagues using Java, C# or PHP.
You're just denying reality here. C lacks tons of features that make everyday things much easier to write and debug and less error prone, such as automatic memory management, bounds checking, a sane type system, a module system, exception handling, closures etc.. These things were invented for a reason.
> My belief is that what the new programmers see as the main advantage in the modern languages is the framework. But as a experienced C programmer I have a vast framework in the form of shared libraries, both my own but mostly the vast array of libraries in a modern Linux distribution.
It's hard to implement good libraries if you're hampered by a language like C. It's pretty much impossible to do string handling in a sane way in C due to the lack of automatic memory management. You can't write a sane (generic, type-safe, fast, no preprocessor nonsense) container library in C. Arbitrary-precision arithmetic is horrible due to the lack of operator overloading.
So sorry, but you're utterly wrong. C is a millstone around the developers' necks for almost every project nowadays.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 7:09 UTC (Fri) by kleptog (subscriber, #1183)
[Link]
You're just denying reality here. C lacks tons of features that make everyday things much easier to write and debug and less error prone, such as automatic memory management, bounds checking, a sane type system, a module system, exception handling, closures etc.. These things were invented for a reason.
I think that's kind of the point. All those things you refer to simply do not exist at a hardware level. If your goal is to teach people how computers actually work then you need to use a language whose model resembles that of the underlying machine, and C does that.
FWIW, I think it would be instructive for new programmers to think about, for example, how you would actually implement things like exception handling in C, just so they appreciate what is being done for them. Many of the things you describe are decidedly non-trivial.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 7:51 UTC (Fri) by HelloWorld (guest, #56129)
[Link]
> I think that's kind of the point. All those things you refer to simply do not exist at a hardware level. If your goal is to teach people how computers actually work then you need to use a language whose model resembles that of the underlying machine, and C does that.
My comment was a response to HenrikH's comment where he questioned the productivity advantages of modern high-level languages. I didn't mean to debate the utility of C as a teaching tool.
Besides, I don't think your comment is very convincing either. The problem is that C sucks even as a low-level language. There's no reason for not having a sane module or type system in a low-level language, not to mention the various C antifeatures like the stupid syntax and the preprocessor.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 8:42 UTC (Fri) by dgm (subscriber, #49227)
[Link]
A list of features is not very convincing, either. Real World measurements, _that_ would be convincing! Show me measurements of effort put into creating real applications in different languages and we can start to talk.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 21:10 UTC (Fri) by tjc (subscriber, #137)
[Link]
I know I shouldn't be feeding trolls, but why do you think C's syntax is "stupid?" The only thing that really stands out to me as not-so-hot is the prefix pointer declarator.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 23:38 UTC (Fri) by HelloWorld (guest, #56129)
[Link]
I don't know why I'm even responding to this, given that you're calling me a troll without any justification.
Anyway, the main problem with C is its declaration syntax. Let's take a declaration like
void (*signal(int, void(*)(int)))(int);
This is ugly for two reasons. One is that parenthesis are required to distinguish void *(int) from void(*)(int). The other is that the tokens that describe the function's return type are split up: the void(* in the beginning and the )(int) in the end belong together, which is harder to figure out than necessary. Using a postfix pointer declarator fixes the first problem but not the second:
void signal(int, void*(int))*(int);
This is fixable by using another syntax for simple declarations: i int; instead of int i;. That would lead to a declaration like this:
signal(int, *(int)void) *(int) void
This also offers the advantage that the name being declared is always at the beginning of the line. It also makes preprocessor macros that involve declarations easier to write.
Another problem with the C syntax is that in function parameter declarations, foo[] means the same as foo*, while everywhere else it doesn't. People keep confusing arrays and pointer due to this (though the implicit conversion from foo[] to foo* also promotes this).
I also find it ugly that while both :? and if/else exist, there is only a switch statement and not a switch expression.
And just to be clear: the C syntax is actually not the greatest of C's problems. The lack of sane type and module systems is a much greater burden.
Why learn C? (O'Reilly Radar)
Posted Jun 30, 2012 17:48 UTC (Sat) by tjc (subscriber, #137)
[Link]
> signal(int, *(int)void) *(int) void
That's going to be hard to parse. The lexer is going to return a token with the value "signal", but the parser is going to be sitting at the top level without any context in which to evaluate it. If it's not a reserved word then it must be an identifier, but nothing else is known about it without peeking further ahead into the input stream.
Something like this would be more practical:
func signal(int, *(int)void) *(int)void
I personally don't mind that C (and C-like languages) anchor declarations with the type. It's still a left-to-right parse, with the single exception of the type. The "var|func ... <complete return type>" syntax is probably better, but I've been looking at "<type> ..." for so long that it seems natural to me. It also allows you to do things like this:
int i, j;
As far as the foo * vs. foo[] thing, that's not really a syntax problem, it's a semantic problem, given that arrays decay to a pointer to the first element in most (but not all; e.g. sizeof(foo)) cases. I agree that that could be changed, and it would be better in most cases. I usually write array parameters as foo *, just because that's what they really are inside the function.
I'm not aware of the difference between a switch statement and a switch expression, so I can't comment on that. I guess I better start "googling."
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 11:15 UTC (Sun) by HelloWorld (guest, #56129)
[Link]
> That's going to be hard to parse. The lexer is going to return a token with the value "signal", but the parser is going to be sitting at the top level without any context in which to evaluate it. If it's not a reserved word then it must be an identifier, but nothing else is known about it without peeking further ahead into the input stream.
I fail to see how conventional C syntax is any better. In a declaration such as foo *bar;, you have exactly the same situation, and to be honest, I don't even see why it's supposed to be a problem.
The only problem I see with this syntax is that it's ambiguous: there's no way to know whether foo(bar)*baz; is a declaration or a statement without looking at the preceding declarations in order to find out whether bar and baz are variables or typedef-names. Alas, conventional C syntax has the same problem in declarations such as the above-mentioned foo *bar;.
It's also fairly easy to disambiguate it using a syntax such as
foo: (bar)*baz;
That would clash with the goto label syntax, as foo: bar; is valid C code. However, this is not a problem: bar; is a statement without effect, so there's no point in writing code like that, therefore a line like that should be parsed as a declaration.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 22:30 UTC (Sun) by tjc (subscriber, #137)
[Link]
I can't think of a situation in C where an expression that begins with a type specifier is anything other than a declaration. I could be overlooking something, but I'm looking at the grammar in appendix A13 of K&R, and I don't see anything.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 22:49 UTC (Sun) by HelloWorld (guest, #56129)
[Link]
> I can't think of a situation in C where an expression that begins with a type specifier is anything other than a declaration.
Uh, so? In a declaration like foo *bar, you still have to know whether foo is a typedef or a variable name in order to know whether it's a multiplication or a declaration.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 23:02 UTC (Sun) by tjc (subscriber, #137)
[Link]
The compiler has this information available to it in the symbol table.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 23:20 UTC (Sun) by HelloWorld (guest, #56129)
[Link]
I know, this is the well-known lexer hack. I still don't see how the syntax I've proposed is supposed to be any more problematic than conventional C syntax.
Why learn C? (O'Reilly Radar)
Posted Jul 2, 2012 20:36 UTC (Mon) by tjc (subscriber, #137)
[Link]
The type specifier in a C declaration acts as a surrogate for the missing 'var' or 'func' keyword, which makes it possible for the parser to recognize the declaration without requesting a lot of extra tokens from the lexer.
Why learn C? (O'Reilly Radar)
Posted Jul 2, 2012 21:23 UTC (Mon) by HelloWorld (guest, #56129)
[Link]
I think that this is only a problem when using top-down parsers. A bottom-up parser should handle this situation easily.
Why learn C? (O'Reilly Radar)
Posted Jul 3, 2012 15:46 UTC (Tue) by tjc (subscriber, #137)
[Link]
Yes, it could be made to work, but it would have to be a backtracking parser, and it would probably be slow. Most bottom-up parsers (such as those produced by bison) are LALR(1), which only has one token look-ahead.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 22:59 UTC (Sun) by tjc (subscriber, #137)
[Link]
I wish there were some way to edit posts after posting so that my mistakes are not permanently recorded for posterity. :)
I should have said, "I can't think of a situation in C where a statement that begins with a type specifier...."
Why learn C? (O'Reilly Radar)
Posted Jul 2, 2012 9:30 UTC (Mon) by nix (subscriber, #2304)
[Link]
Quite, thus ruling out sizeof().
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 11:28 UTC (Fri) by adobriyan (guest, #30858)
[Link]
> Many of the things you describe are decidedly non-trivial.
Seeing small, unknown C project called Linux the kernel from inside,
I agree that C sucks even as low-level language.
Multiple return values.
Less stupid preprocessor, more compile-time evaluation!.
Precise alignment and offsets of structure fields (constant and "modulo by" constraines).
Less "undefined behaviour" beartraps!
All of this could be added without sacrificing "close to machine" property some love so very much.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 2:55 UTC (Sun) by vonbrand (subscriber, #4458)
[Link]
Multiple return values.
You can return a struct... and multiple return values would require handling multiple values all over the place (asignments, comparisons, ...). Nifty, but definitely not "low level." C is OK as is here.
Less stupid preprocessor, more compile-time evaluation!
The preprocessor is decidedly not stellar. Better use m4(1) perhaps? Or just chuck it altogether (given const and enums, and inline functions, its use is mostly for #include nowadays). Plus current compilers are quite smart at getting rid of useless code and constant folding, so "compile time evaluation" is given much more than you perhaps realize.
Precise alignment and offsets of structure fields (constant and "modulo by" constraines).
Sorry, but if you want a low-level, efficient language, you have to give the compiler leeway to accomodate the architecture's quirks.
Less "undefined behaviour" beartraps!
Ditto.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 12:22 UTC (Sun) by HelloWorld (guest, #56129)
[Link]
> You can return a struct... and multiple return values would require handling multiple values all over the place (asignments, comparisons, ...). Nifty, but definitely not "low level." C is OK as is here.
This has nothing to do with high-level-ness. It doesn't require dynamic memory allocation or other fancy run-time implementation techniques and you don't lose any control over your code.
> Sorry, but if you want a low-level, efficient language, you have to give the compiler leeway to accomodate the architecture's quirks.
If one doesn't specify the data structure layout, the compiler would still be able to decide by himself what to do.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 20:42 UTC (Sun) by kleptog (subscriber, #1183)
[Link]
>> You can return a struct... and multiple return values would require handling multiple values all over the place (asignments, comparisons, ...). Nifty, but definitely not "low level." C is OK as is here.
> This has nothing to do with high-level-ness. It doesn't require dynamic memory allocation or other fancy run-time implementation techniques and you don't lose any control over your code.
Returning multiple values is something that would require ABI support. Only values in the registers can be returned because the callee stack frame vanishes at return. Returning structs is already something poorly supported, because if it doesn't fit in registers, where do you store it then? The reason C++ can do it is because new/delete are part of the language, as are (copy) constructors. (I believe it's done with hidden extra parameters).
It would also require making a list a first class object in the language.
You could possibly come up with an ABI that supported returning multiple values in a way that was an improvement over what you can do already and make it work on all the myriad of processors in existence, but I just don't see the demand.
I think C's simple, predictable ABI is key to its continuing popularity. Any language, including dynamic languages, can call into a C library. If you have a C++ library it's already much harder, and calling from (for example) a python script into a perl library is essentially impossible.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 20:56 UTC (Sun) by HelloWorld (guest, #56129)
[Link]
> Returning multiple values is something that would require ABI support.
Uh, so what?
> Only values in the registers can be returned because the callee stack frame vanishes at return. Returning structs is already something poorly supported, because if it doesn't fit in registers, where do you store it then?
On the stack of course. Also, returning structs isn't "poortly supported" at all, it works just fine.
> The reason C++ can do it is because new/delete are part of the language,
That's simply nonsense.
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 22:24 UTC (Sun) by nix (subscriber, #2304)
[Link]
To be fair, returning structures was poorly supported once. Of course that was in about 1980, but it explains some properties of the standard C and POSIX library (e.g. why stat() does not return a 'struct stat').
Why learn C? (O'Reilly Radar)
Posted Jul 1, 2012 22:38 UTC (Sun) by juliank (subscriber, #45896)
[Link]
I'd say another reason is that it's much easier to check for errors if your function returns some integer and passes the real result via a pointer.
Why learn C? (O'Reilly Radar)
Posted Jul 2, 2012 9:29 UTC (Mon) by nix (subscriber, #2304)
[Link]
Well, if there's only one error, it's easier to use the function if you just return the pointer or NULL.
Structures and results
Posted Jul 12, 2012 16:44 UTC (Thu) by alex (subscriber, #1355)
[Link]
Or to emulate old-school styles return NULL and set the global errno ;-)
Why learn C? (O'Reilly Radar)
Posted Jul 12, 2012 20:04 UTC (Thu) by hummassa (subscriber, #307)
[Link]
HRESULT feelings.
Why learn C? (O'Reilly Radar)
Posted Jul 12, 2012 20:08 UTC (Thu) by hummassa (subscriber, #307)
[Link]
> On the stack of course.
Actually, IIRC, if the struct/class instance fits in one or two registers, those are used to return it; if it does not fit, the caller reserves the space just below the return address/stack frame on the stack (so the callee knows where to put it).
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 18:22 UTC (Fri) by HenrikH (guest, #31152)
[Link]
I am not denying reality one bit, I actually only made a reflection upon the real reality, i.e I constantly compare my work, the time it takes me to write certain things and when I compare that with the people who use those so called modern languages I can see no advantage for them, i,e they do not finish their project faster than me and their software is not more capable than mine. The only reason that I stopped writing dynamic web sites in C is that installing new code is such a hassle compared with say PHP since it has to be compiled to the correct architecture.
Impossible to do string handling? I do string handling all the time and I'm sure a lot of other C programmers do the same. Excuse me but what are you smoking?
Arbitrary-precision arithmetic? I just use GMP for that if we are talking about serious need for precision.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 18:44 UTC (Fri) by HelloWorld (guest, #56129)
[Link]
> I am not denying reality one bit, I actually only made a reflection upon the real reality, i.e I constantly compare my work, the time it takes me to write certain things and when I compare that with the people who use those so called modern languages I can see no advantage for them, i,e they do not finish their project faster than me and their software is not more capable than mine.
Even if what you say is true, it's just an anecdote with no representativeness whatsoever.
> Impossible to do string handling?
I didn't say that. I said it's impossible to do it in a sane way, and anything that requires you to worry about memory management is *not* sane.
> Arbitrary-precision arithmetic? I just use GMP for that if we are talking about serious need for precision.
GMP-based code is utterly unreadable due to the lack of automatic memory management and operator overloading. Anybody who denies this probably has no idea what good, elegant and simple code looks like.
Why learn C? (O'Reilly Radar)
Posted Jun 30, 2012 17:13 UTC (Sat) by HenrikH (guest, #31152)
[Link]
>Even if what you say is true, it's just an anecdote with no representativeness whatsoever.
Of course but I have yet to meet anyone that can write a similar application in another language quicker than I do in C, and I have worked on thousands of projects.
>I didn't say that. I said it's impossible to do it in a sane way, and anything that requires you to worry about memory management is *not* sane.
No you say that You have the right to define what is sane or not and thus it doesn't matter what the rest of us say, it will always be defined as not sane by you.
>GMP-based code is utterly unreadable due to the lack of automatic memory management and operator overloading. Anybody who denies this probably has no idea what good, elegant and simple code looks like.
GMP has automatic memory management under the hood, there is no need to resize the integers etc, and while there is no operator overloading I hardly think that "mpz_add (a, b, c);" is that much harder to read than "a = b + c;".
Actually I think that the first is easier to read since I know by reading the code that the function that performs the action is mpz_add() while I in the other case has to hunt down what class is used, what the overloads are etc.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 18:54 UTC (Fri) by metan (guest, #74107)
[Link]
> Impossible to do string handling? I do string handling all the time and I'm sure a lot of other C programmers do the same. Excuse me but what are you smoking?
I must agree with this statement. Most of the rants about C string handling comes from people who couldn't do string handling correctly in any language.
The standard C library sucks, but you could easily do better and even create wonders with reasonable usage of alloca() and some of the C99 features or GNU extensions.
Why learn C? (O'Reilly Radar)
Posted Jun 30, 2012 0:51 UTC (Sat) by alankila (subscriber, #47141)
[Link]
Most of us who have written scripting languages do not even think about the concept of "string handling". It doesn't even occur to us that there could be a problem with it.
Why learn C? (O'Reilly Radar)
Posted Jun 30, 2012 3:23 UTC (Sat) by jamesh (guest, #1159)
[Link]
You can easily run into problems with languages with automatic string handling if you don't know how the language handles things under the covers.
If you have an algorithm that iteratively cuts off and processes the head of a string until the string is consumed, the way to get the tail of the string can affect the performance.
If making a new string containing the tail of the original string just references the original string data, the obvious solution may work fine. If instead it involves a memory allocation and string copy, then the algorithm may exhibit quadratic performance w.r.t. the string length.
And this isn't just a case of one string handling method being better than the other: with the first strategy, taking a 100 character slice of a 100K string might prevent the large string from being released.
For all its faults, at least these sort of trade-offs are usually visible in C string handling.
Why learn C? (O'Reilly Radar)
Posted Jun 30, 2012 17:03 UTC (Sat) by HenrikH (guest, #31152)
[Link]
Exactly, I have encountered numerous people who are baffled when += "some string" all of the sudden gets slow as molasses.
Why learn C? (O'Reilly Radar)
Posted Jul 3, 2012 13:36 UTC (Tue) by alankila (subscriber, #47141)
[Link]
*sigh* I should have predicted that my simple comment would end up in this sort of discussion and worse yet would make me look foolish. ;-)
Let's just say that your point is valid and correct. It still does not make me look C-style "string" handling favorably. The few instances where you have to take a copy to detach a substring from a longer string, or to avoid quadratic performance of string concatenation loops are imho not materially significant compared to the benefits of making strings easy and nice to use.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 7:15 UTC (Fri) by ekj (guest, #1524)
[Link]
Yes it's really true. At least to the best of our knowledge, it's really true.
I'm not talking "new programmers" here, even the Grand Old Man of large-software projects, Brooks, clearly agrees that high-level languages do allow people to get more done with less hours spent.
It's not a new revelation either, the Mythical Man Month is over 20 years old.
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 21:00 UTC (Fri) by PaulMcKenney (subscriber, #9624)
[Link]
Of course, when Brooks wrote "The Mythical Man Month", C was considered to be a high-level language. ;-)
Why learn C? (O'Reilly Radar)
Posted Jun 29, 2012 23:02 UTC (Fri) by dgm (subscriber, #49227)
[Link]
By who? Certainly not by K&R. In the preface of "The C Programming Language" (an absolutely must for anybody wanting to start with C and programming in general) you can read this:
"C is a general purpose programming language which features economy of expression, modern flow control and data structures, and a rich set of operators. C is not a ``very high level'' language, nor a ``big'' one [...]"
Why learn C? (O'Reilly Radar)
Posted Jun 30, 2012 15:10 UTC (Sat) by engla (guest, #47454)
[Link]
“very high level” language and “high level” language are different labels. The former obviously created to differentiate from the latter.
Why learn C? (O'Reilly Radar)
Posted Jul 2, 2012 0:24 UTC (Mon) by rodgerd (guest, #58896)
[Link]
So if someone tells you the movie they just watched was "not very good", you'd assume it was a good movie, rather than a bad one?
Why learn C? (O'Reilly Radar)
Posted Jul 4, 2012 12:25 UTC (Wed) by jwakely (subscriber, #60262)
[Link]
There's a difference between not very high level and not "very high level"