LWN.net Logo

Why learn C? (O'Reilly Radar)

Why learn C? (O'Reilly Radar)

Posted Jun 29, 2012 11:28 UTC (Fri) by adobriyan (guest, #30858)
In reply to: Why learn C? (O'Reilly Radar) by kleptog
Parent article: Why learn C? (O'Reilly Radar)

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


(Log in to post comments)

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

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