10 ms·
What’s to love about C?
- gouranga 14y agoThe thing I love about C is that it the most obedient of all languages and never assumes you are an idiot. Never does it tell me I'm about to blow my own foot off - it just does it and I've learned from the experience. Due to this it taught me how to think about things first and know of all the consequences of doing everything. *NIX is the same.
- bornhuetter 14y agoThere is something wonderful about low level programming langauges. I enjoy writing code and knowing exactly what the ASM is going to look like. Python is fun, but I've got no idea how it is doing what it does.
- domlebo70 14y agoHow often do you need to know what it's doing?
- gouranga 14y agoUsually when something doesn't go as quick as you were hoping, profiling is unhelpful and you have to take the python bytecode to bits to find out what the hell it's doing inside. That happens quite a bit these days for me.
- uhhyeahdude 14y agoOh, and this. For sure. Why is everything so slow?!
- gouranga 14y ago> Why is everything so slow?! Urf tell me about it. Does my flipping head in.
- bornhuetter 14y agoI used to play around with 3D graphics in real-time on the 486. I needed to know exactly what was going on to even get 30 fps. I managed to get one of Michael Abrash's (ID software)loops to run 0.5 clock cycles faster, which I was pretty proud of.
- uhhyeahdude 14y agoWhen you are attempting to do a very specific thing. Not going to go into what that might be, unless you're curious, but sometimes you do need to know exactly or as close to exactly how your code is operating.
- uhhyeahdude 14y agoThis is exactly why I'm learning the various ASM right now. I am not content (depending on what I'm trying to do, of course) with a program just doing "something" that works.
- simias 14y agoThis is very true but in my experience it's not always a good thing. It often leads me to premature optimization for instance.
- techdmn 14y agoI've been working on some ideas about how different languages lead to different sorts of premature optimization problems. I see a lot of OOP problems where developers try to avoid re-initializing or re-instantiating an object, and end up with weird partial-initialization or wrong initialization edge cases. I think language structure encourages this, and have been thinking about what other sorts of premature-optimization problems languages encourage.
- ajanuary 14y agoDoing exactly what you told it is fine until you hit one of the many undefined behaviours and then it does exactly what you thought you told it only some of the time. There's a lot of undefined behaviours to know about, and not all of them are obvious. You can be merrily compiling your programs fine on GCC and Clang and your intended behaviour seem entirely obvious, only to have it blow up on another compiler or with some different compiler flags. That said, the undefined behaviours do allow the compilers to do some very tight optimisations, so swings and roundabouts.
- gouranga 14y agoThat tends to matter less if you have a decent test harness and plenty of assertions!
- ajanuary 14y agoHadn't thought about that. I haven't done much C since I discovered proper automated tests.
- beagle3 14y agoNot everywhere C's undefinededness lies; e.g. shifting more sizof(int) bits - in case the shift parameter is dependent on your input. "Decent test harness" is like "sufficiently smart compiler". Everyone assumes it will be there when planning, but in practice it is only available in very specific and not-generally-useful cases.
- silentbicycle 14y agoHere's a one-file, ISC-licensed test framework I wrote for ANSI C: "greatest" (https://github.com/silentbicycle/greatest https://github.com/silentbicycle/greatest). It doesn't depend on any dynamic allocation, or anything beyond C89, and compiles with zero warnings under -Wall -pedantic.
- rpearl 14y agoThe following program compiles without warnings using gcc 4.7.0. The compiler uses the undefined behavior of accessing a variable that may not have been initialized to turn the conditional into one that's always taken, and then folds it away. https://gist.github.com/2910012 https://gist.github.com/2910012
- qznc 14y agoInteresting. Our compiler catches it, though: $ cparser -O3 -Wall -Wextra -pedantic test2.c -o uninit warning: ignoring gcc option '-pedantic' test2.c:3:6: warning: no previous declaration for 'void foo(int)' [-Wmissing-declarations] 1 warning(s) test2.c:4:6: warning: 'variable lol' might be used uninitialized [-Wuninitialized] Looking at the GCC bugtracker, there seem to be various warnings missing. http://gcc.gnu.org/bugzilla/buglist.cgi?quicksearch=uninitialized http://gcc.gnu.org/bugzilla/buglist.cgi?quicksearch=uninitia...
- plorkyeran 14y agoclang does catch this: clang -Weverything uninit.c uninit.c:3:6: warning: no previous prototype for function 'foo' [-Wmissing-prototypes] void foo(int bar) { ^ uninit.c:10:6: warning: variable 'lol' may be uninitialized when used here [-Wconditional-uninitialized] if (lol == 7) { ^~~ uninit.c:4:9: note: initialize the variable 'lol' to silence this warning int lol; ^ = 0 uninit.c:37:3: warning: no newline at end of file [-pedantic,-Wnewline-eof] */ ^ 3 warnings generated.
- rpearl 14y agoGlad to hear it--I've been looking into using clang more... This might just tip me towards it.
- antirez 14y agovalgrind: ==46592== Conditional jump or move depends on uninitialised value(s) ==46592== at 0x100000E90: foo (in ./a.out) ==46592== by 0x100000ED3: main (in ./a.out) The matter is, valgrind exists. Everything is a C deficiency that valgrind is able to overcome decently is no longer a C deficiency practically speaking.
- tezza 14y ago* Lots of programmers you can hire today (contrast to D, ML, Haskell ) * Excellent introductory texts (Kernighan & Ritchie) * Backed by giants like Microsoft, IBM * Everything else always has some mechanism to link against it * Thorough API documentation (if quite large) MSDN, man 3 sprintf
- unwind 14y agoI wouldn't quite agree that Microsoft are a good example of "backers" of C. Unless, of course, you consider backing for C++ to be enough. Microsoft's compiler doesn't support C99, and likely won't do so any time soon (http://connect.microsoft.com/VisualStudio/feedback/details/526116/c99-support http://connect.microsoft.com/VisualStudio/feedback/details/5...).
- tezza 14y agoI don't mind the way MS does C . Whacking variable declarations at the top of the scope becomes second nature quickly. I came from assembly language, where the process is very similar and not too demanding
- apaprocki 14y agoMicrosoft not supporting C99 is a huge issue because the world of open-source C projects that are supported on both Unix flavors and Windows can not integrate C99 features into their core because Windows support means building with MSVC. It might not be a big deal for one individual project, but you're literally talking about the entire world of open-source C projects being restricted by their decision.
- alexchamberlain 14y agoThere's an old adage that a computer only does what you tell it to do. What I love about C? It really does only do what you ask it to; nothing more, nothing less.
- JohannL 14y agoI was just typing up a reply saying just that in more words when I saw yours; so for the sake of efficiency I'll just make my post a pointer to yours ^^
- derleth 14y ago> It really does only do what you ask it to; nothing more, nothing less. Not even assembly language has this property anymore, especially in the presence of an OS. Which leads me to what I think is C's greatest weakness: It's no longer a thin layer over assembly language. It is so far removed from the current trends hardware design it doesn't even have functions that implement memory barriers as part of the core language, let alone any language features to help you take advantage of whatever SIMD hardware the machine possesses. Part of this is C becoming a victim of its own success: People expect the same language to compile down to ARM and x86-64 and IBM mainframe (zSeries) and so on and so forth machine code, and do it in an efficient manner. Since low-end ARM chips don't have SIMD, C can't have SIMD, the thinking goes (even though low-end hardware doesn't have FP opcodes, either, but floating-point math is still part of the C standard). They rely on compilers to make it fast, which is reasonable to some extent but it's also the same thing you rely on when you write in Haskell. It's contrary to the spirit of things. I'm not advocating a return to assembly language. That would be massively stupid. However, it would be nice to have a language with concise syntax that also allows you access to the processor's status word (the carry flag, for instance).
- uhhyeahdude 14y agoI agree with all of these comments. But... Unless you are an old-school coder who grew up with C, it is just so much easier to get stuff done with Python/Ruby or, hell, Bash/Awk/Perl. Granted, that last was for very specific tasks, but still. Want to write security code? C is is your friend, but you better be good at it, very good.
- exDM69 14y ago> Unless you are an old-school coder who grew up with C, it is just so much easier to get stuff done with Python/Ruby or, hell, Bash/Awk/Perl. True, but who is going to write your Python/Ruby interpreters then? And using what language?
- uhhyeahdude 14y agoOh, you are certainly on point. But how deep down the rabbit hole do you want to go? I mean, after reading "Trusting Trust", you really can't be sure of anything. Who writes your compilers? Assuming you are not specifically referring to security stuff, and more talking about a responsible community; well, I don't know. I do know that I need to get better at C. And that that was w/r/t interpreter comment, in case that implication was not obvious.
- exDM69 14y agoThis is why (good) computer science studies generally involve everything from CPU fundamentals to operating system programming and compiler design. I'm not afraid to look inside my interpreter, operating system kernel or my C compiler.
- shiven 14y ago> True, but who is going to write your Python/Ruby interpreters then? Pythonistas will. > And using what language? Using python. Case in point, Pypy. <\ :P>
- uhhyeahdude 14y ago
- dkhenry 14y agoI am going to make the bold claim that everyone should know C. Even in a world where we can do 90%+ of our coding in a higher level language we still aren't at the place were we can get past writing out performance loops in C. Case in point my company recently needed to speed up a 128bit AND on two MySQL binary fields. While this can be done in pure SQL implementing the function in C saved an order of magnitude of time. Everyone is going to run into these performance problems and until we have run times that can match hand coded C it will in my option still be a required skill to have. ( Yes I know you can s/C/ASM , however this is a time issue eventually we may get past needing C , but we are not there yet )
- exDM69 14y ago> I am going to make the bold claim that everyone should know C. I agree. Anyone who's going to be working at a non-entry-level job in the computer industry should know what the foundations are built on. C still dominates in the kernel space and other low-level facilities everyone uses. If you don't know it, you are at a disadvantage compared to those who do.
- mattmanser 14y agoPah, 99.9% of programmers will never, ever, ever need to drop down to that level. Where do these delusions even come from? I think a lot of people here would benefit from 6 months in a 'normal' company, the kind that employs the vast majority of programmers. Just for a bit of perspective.
- CodeMage 14y agoThese delusions come from experience with watching programmers flop around like fish out of water whenever some layer of abstraction they took for granted starts "leaking". I honestly don't get this attitude in our industry. Nobody tells a general practitioner, "Nah, you don't really need to know what comprises a living cell. You most likely won't even need to drop down to that level." Just because you don't need to do any low level work, doesn't mean you shouldn't learn anything about it.
- markessien 14y agoC is what you use when you should actually be using assembler, but you are not insane enough to actually do that.
- alexchamberlain 14y agoReally? Optimiser's are pretty damn good, so C has pretty much replaced assembler.
- exDM69 14y agoThe statement "C is portable assembly" may have been true some time in the 1970's but compilers have improved since then and the statement is no longer valid.
- deleted 14y ago[deleted]
- exDM69 14y ago> Believe me, there are a lot of cases where a human beats the compiler. Oh, I do believe you. I do analyze and re-write assembly code from the C compiler every now and then. SIMD optimizations are especially tricky. But the compiler still wins at least 80% of the cases. And when you add a single line of assembly code (inline or not) to your C code, you inhibit all optimizations that could take place. So for your innermost loop it might make sense to hand write assembly, but for most optimizations just re-think your C code and/or add compiler intrinsics to get SIMD or other CPU facilities put to better use.
- stephencanon 14y ago> when you add a single line of assembly code (inline or not) to your C code, you inhibit all optimizations that could take place. That's a significant exaggeration with most compilers. You may inhibit some optimization which would require the compiler be able to reason about side-effects across the asm block, but that's a far cry from "all optimizations".
- exDM69 14y agoAfter using C++ for years (in addition to Python, Haskell, etc) I've recently converted back to C for my low level tasks. You just gotta love the simplicity of C programming. My jobs consists of reading blobs of binary and throwing it around different buffers in memory (mmap's and dma buffers, etc), so C is a natural choice. I like how C is fast and dangerous, there's no silly security layers (like Java) and it lets the operating system do it's job. Fast stack allocs for temporary data are great but deadly if you fuck up. Coding in C is like carrying a loaded gun pointed at your feet. You have to handle it with care and responsibility but when you need it, you can shoot away any extra limbs that get in the way and show 'em who's the boss.
- uhhyeahdude 14y ago<Coding in C is like carrying a loaded gun pointed at your feet.> Could not agree more. Except I would say pointed at someones head, or, perhaps, your own.
- TazeTSchnitzel 14y agoCoding in C is like being presented with a grid of buttons of various shades of blue and green. You know to press a specific colour, but it doesn't always do quite what you expect, and half the buttons segfault.
- jonsen 14y agoIt's always hard to C a loved one Go.
- fmkamchatka 14y agoOne great advantage of C (which was kind of mentioned in the article) is predictability of the generated code. Compared to a C++ compilers, a C compiler is much simpler. When you write a for loop, even doing something on a struct or some fairly "complicated" object, you have a fairly good idea of the machine code generated. It's also easier to go between the assembly code and the C code generally. In some applications like email or image processing, this can be valuable.
- derleth 14y ago> predictability of the generated code To some extent, I hope the code that gets generated isn't predictable in the way you mean: I want the compiler to know all of the obscure ways to make string operations go faster by using SIMD hardware, for example. I want that kind of deep low-level knowledge to be encoded into the optimization passes because I don't know it and I likely never will. Isn't that why we have optimization passes to begin with?
- michaelochurch 14y agoI've never heard anyone informed bash C. C++ and Java, yes, but not C. Even people who've never used it tend to know that C has a domain at which (a) it excels, and (b) it's really the only credible option. There's actually no such thing as a bad language. I enjoy language wars as much as anyone else, but languages are only good or bad relative to the type of problem they're being used for. For example, what makes C++ and Java odious is that people try to use them for inappropriate purposes. They aren't platonically "evil" languages, and they weren't designed by stupid people; they're just inappropriate for over 90 percent of what modern programmers have to do in their professional lives. When people need high-level features and don't have them, they tend to roll their own-- badly. This is Greenspun's Tenth Rule and the heart of those god-awful "design patterns". C++ and Java, for all that, also have domains in which they're the appropriate languages to use. They're just very small.
- swah 14y agoThe conclusion to this is that in bigger projects you should use multi-language solutions... which many people don't like.
- loup-vaillant 14y agoDoes anybody here have an idea why? I have heard reasons ("interfacing languages is a mess" and "you'd have to learn several languages" being the main ones), but none I found compelling (especially when the alternative is C++). If it's irrational to reject multi-language solutions (I believe it is), why do people keep doing it?
- to3m 14y agoTool support is usually poor, particularly for debugging. Interfacing languages is a mess, whether obviously (if you do it by hand) or hidden away (if you use SWIG). More code to deal with and step through. Higher chance of really ugly, hard-to-diagnose side effects from making a mistake on the C side. People working in both languages will regularly introduce bugs or perf problems because they get the languages mixed up as they're working. If you have less-technical people working in the higher-level languages, they'll will work around bugs in the C side, so you never hear about them until you fix them and break everything. I've worked on a number of mixed-language projects, and these same issues keep cropping up. Perhaps it depends on the team, but that is not something not everybody gets the chance to choose. My current mixed-language program (tcl/C++) is working out OK, with just me working on it, but tool support is poor, and hand-writing the language interfaces is annoying. (Notable exception: Visual Studio 2010 looks to offer pretty decent mixed native/managed debugging, and the CLR has good C++ interop support.)
- va_coder 14y agoPeople who love C love it, in part, because they are often solving interesting problems with it. Many problems that require C are interesting: embedded software, automated robots, new databases etc. Most problems that C is not good at - Web development - are not that interesting to many people that love C.
- puivert 14y agoNone of those require C, and are frequently done using high level languages.
- Jach 14y agoYour comment may have gone over better if you included specific examples. For one, here's a story about Lisping at the Jet Propulsion Lab: http://www.flownet.com/gat/jpl-lisp.html http://www.flownet.com/gat/jpl-lisp.html
- tosseraccount 14y agoC is not good at web development? Most of the "web" stack is written in C: OS/NGINX/apache/php/java/perl. If you do write the top of the stack in C, it will run faster than anything else and be smaller. This means lower turnaround time which is VERY IMPORTANT in web apps. C, in many cases, is the best Web language.
- sgk284 14y agoIn what world does runtime speed and compiled size have anything to do with turnaround (or programmer productivity, as I read it)? C is a great language for many reasons, but let's not conflate benefits.
- tosseraccount 14y agoPerhaps there's a misunderstanding in the term "turnaround". I meant it in this sense t = x + y + z. where t = total time, x = send time , y = processing time, z = send back to user time. If you can decrease y, you decrease "t". Responsiveness is important to users. With the advent of large and extremely fast memory cache, small program size will dramatically increase program execution speed by have small code and small data already in cache. Smaller program executable will enable many more instances on a real physical node. Size matters. Speed matters.
- cema 14y agoC is well placed in that sweet spot close to the hardware where the abstractions are good enough to hide the hardware specifics from the programmer and good enough to build more advanced abstractions with sufficient efficiency. Because it is so close to the lower levels of abstractions, it ended up a compact and concise language, easy to keep in mind. This (among other things) led to C being the quintessential language for some segments of "computer science": maybe not so much directly but as a reference model.
- gurkendoktor 14y agoI think the most dangerous part of C is that many people assume C is perfectly efficient. It's as if C was the "100%" of performance and other languages are converging towards it as they get better. But I don't see how this is true. Pointer aliasing is the biggest example probably: http://www.futurechips.org/tips-for-power-coders/how-to-trick-cc-compilers-into-generating-terrible-code.html http://www.futurechips.org/tips-for-power-coders/how-to-tric... And then there is the huge abstraction penalty for things like `qsort()` where C++ is actually way ahead of C.
- argv_empty 14y agoGuaranteed simple flow And then there's setjmp/longjmp. Clear and unambiguous Aside from all the undefined and implementation-defined behavior. A macro preprocessor Which just does plain, dumb text substitution with no understanding of program syntax (or the environment where the expanded macro will appear!). CPP's macro system is a pretty leaky abstraction. The other points are why I use C (when I use C), but I wouldn't make these three arguments in favor of C.
- wmil 14y agoWhat I don't like about C: it's 2012, strings should be easy. Instead they are a source of bugs and security vulnerabilities.
- mccoyst 14y agoThe preprocessor, and conditional compilation especially, are not something to love about C. That is probably one of C's most hateful, code-goop enabling capabilities.
- regularfry 14y agoUgh. Just today I've had to clean up a problem where a function was called without enough parameters, but because the compile-and-link step included just the right components, there was neither a warning nor an error. Not only that, but because of a coincidence on the stack (the missing parameter was a NULL, which just so happened to be lying around), the relevant unit tests actually passed. The problem with C is that, for all its simplicity, there's a ton of voodoo which you've got to "just know", and the best practices are not at all clear from what the language makes easy. In fact, the language makes it easy to make mistakes and difficult to find them. I wish Go had made GC optional, I really do.
- cheatercheater 14y ago> Access to the hardware Yeah, C is close to the metal. But only on a PDP-11, the assembly language of which C was conceived to integrate with.
- deleted 14y ago[deleted]
- orthecreedence 14y agoOne thing I LOVE about C is that it's so easy to create APIs that other languages can use. For instance, if a C library exposes a good API, you can call it from lisp quite easily with CFFI. This gives you a large advantage: the low level, raw speed of a C library paired with the high-level forms of whatever language you choose. I'm all for people writing programs/apps in C++, but I kind of cringe when I see a library in C++ because it's a harder language to wrap around.
- farinasa 14y agoNo. ...wait
- wtf2012a 14y agoIn embedded product development, C and C++ is King.