14 ms·
Ah, C, the love of my life... Want minimal syntactic sugar and a language that's not prone to feature-of-the-week hipsterism? C Want to make tiny binaries for
by Nursie 10y ago
Ah, C, the love of my life...
Want minimal syntactic sugar and a language that's not prone to feature-of-the-week hipsterism? C
Want to make tiny binaries for some obscure system with virtually no storage or RAM? C.
Want to know where every last cycle goes and explicitly control everything? C
Want to trivially import any of the huge array of existing libraries? You guessed it...
Want to do something you really shouldn't, and any 'sensible' language might attempt to stop, but is called for right here and now...
It's not the safest language, it's not the most expressive, but the syntax is small enough to hold in your head and the body of existing, available work is HUGE. I've been working in C for 15 years and, while I enjoy forays into other languages, it will always be my love.
- jbb555 10y agoAgree entirely with all this. You know what you get with C and it just works as you expect always. I always feel refreshed after writing C. There are perhaps a few very minor enhancements I'd suggest, but I'd be very reluctant to open the floodgates and ruin it.
- zigzigzag 10y agoIt always works as you expect? Really? C is one of the few languages, along with C++, that revels in undefined behaviour. It can be very hard to reliably know what a C program will do if it's not written very carefully because there are so many constructs that look benign but which are technically wrong, and the compiler will mercilessly exploit in order to optimise your program into nonsense.
- deagler 10y agoThis, While C and C++ are both very powerful languages and could still be considered the "industry standard"(loosely used) It is definitely not a language that will make your applications work as expected, I would say that C#/Java or higher level scripting languages like Lua or Javascript are languages that will let you make "code it and it works" applications
- MaulingMonkey 10y agoC and C++ are the only languages I regularly use wherein I need to resort to disassembly to debug the damn thing.
- retro64 10y agoI find this statement hard to imagine. I've been coding professionally in C++ for just shy of 20 years now (and in C for a few years before that) and have only resorted to assembly when I needed to actually use assembly for performance reasons. Actually - not completely true. I do like to look at the assembly from time to time for other reasons, but this is rare.
- MaulingMonkey 10y agoEven when I have performance to worry about, intrinsics are usually an option - possibly even a superior option - when writing performance sensitive code. But here are some of the cases where I resort to looking at program disassembly - I'll note a theme here of debugging optimized code (because who doesn't love heisenbugs): 1) Diagnosing a crash which turned out to be the result of a 1 byte vtable pointer corruption bug in an older codebase, which turned out to be a bad static_cast in relatively removed code (a good case for boost::polymorphic_downcast!). Simply understanding which pointer was bad in the first place required looking at the disassembly - when you can't rely on your debugger's results thanks to optimization. 2) Figuring out the actual values of variables in crashes and crash dumps of optimized builds to properly root cause a bug, when the debugger gets confused - or simply aggressively inlined and reordered everything so aggressively that there's no sensible values to even display (so, most crash dumps.) 3) Noticing when the optimizer has reordered code "unexpectedly", alerting me to the fact that supposedly thread safe code is in fact nowhere nearly remotely safe and is in fact missing many memory barriers (possibly because their portable macros "helpfully" defaulted to a noop on whatever new and previously unrecognized platform I'm porting to.) 4) Noticing when the optimizer has removed or rewritten code in an "incorrect" manner, helping me debug code that would've worked if it hadn't technically invoked undefined behavior, so I can a) fix it, b) attempt to explain to my coworker that, yes, it's really undefined behavior, and yes, it's actually a problem (typically with a combination of citing the standard and linking INVALID WONTFIX-ed "bugs" in some compiler's bug database), c) be reasonably certain I've actually found the real root cause of a bug and fixed it. Now, yes, I'll admit this isn't 100% of my debugging sessions. And perhaps I'm an outlier. My coworkers generally learn that I can (eventually) tackle pretty much any weird bug they might be struggling with and that I'm happy to help. All the porting I've done reopens a whole codebase's worth of wounds - latent undefined behavior that another compiler's optimizer didn't take advantage of. But on the other hand, I've been lucky enough to never encounter a codegen bug in all the compiler and linker bugs I've found. So far. That I know of. And while "rare" by incidence, these are the debugging sessions that can eat weeks at a time for a single bug, when sufficiently nasty and novel.
- tarancato 10y agoUndefined behaviour is not as prevalent in real world code as reading articles from HN might make you think.
- rwmj 10y agoThis is a very naive statement. Sure there are a handful of good companies that enable every single compiler warning, and fix those warnings, and then run the code through Coverity, and fix all those problems too. Almost no one else does. The amount of terrible C in the real world is enormous.
- kbart 10y ago"The amount of terrible C in the real world is enormous." I'm sure you could say that about pretty much any programming language: "The amount of terrible X in the real world is enormous". There are also plenty of clean, nice, safe C code around (and any other language), there's no need to over-generalize ("Almost no one else does").
- netheril96 10y ago> I'm sure you could say that about pretty much any programming language: "The amount of terrible X in the real world is enormous". But the damage is far greater in C. In other languages you won't have arbitrary code execution or privilege escalation just because the programmer is not careful. Nor will there be, in other languages, so many nondeterministic bugs that show up once in a blue moon.
- kbart 10y ago"It always works as you expect? Really?" Well, it works as defined in standards. It's just not every programmer knows what to expect. C is simple, yet powerful language and with powers comes responsibility. It's not like you can throw some libraries/modules/objects (or whatever it is in other, safe languages) together, upload to server and call it a day -- static testing, debugging, unit testing is a vital part of any semi serious C project.
- 0xfeba 10y agoC++ is far more prone to undefined behaviour and even compilation across systems with the STL.
- JoeAltmaier 10y agoI hear this, but I've rarely experienced it. Maybe I unconsciously avoid such cases with long use. I started with assembler, and debug C/C++ with disassembly turned on, so maybe that's why.
- ArkyBeagle 10y agoIt's just really not that hard to avoid undefined behavior. Really. The worst is arguably integer overflow, and that's not even all that challenging.
- radmuzom 10y agoOne of the best things about C is it is not upgraded frequently like other languages. So there are no frequent <language> <x.y> released posts here like we see for other languages.
- creshal 10y agoOn the downside, when it does get updated, compilers take ages to implement the new features, and in the meantime make up busywork like "let's break OS kernels or crypto code to get faster in some random benchmark nobody cares about!"
- dom0 10y agoDon't need C updates for that, a GCC upgrade is quite sufficient! :(
- joosters 10y agogcc is generally used as a testing ground for new C/C++ features. So in most cases, the compiler supports new features before they are 'officially released' into the language. It's the complete opposite of waiting for a feature to appear in the compiler.
- creshal 10y agoGCC didn't get sort-of-complete C11 features until 4.9 (2014) and still omits (largely useless) optional features.
- bkjsbkjdnf 10y ago> You know what you get with C and it just works as you expect always. Every language works just as you expect if you have the right expectations.
- henrik_w 10y ago"minimal syntactic sugar" made me think of this quote: "C is memory with syntactic sugar." Dennis Kubes
- vilya 10y ago> Want to know where every last cycle goes and explicitly control everything? C So... you compile with optimisation switched off?
- Nursie 10y agoIn some cases you may wish to, yes. Particularly where it comes to things like explicitly zeroing memory before freeing it, something that has recently been addrezsed with memset_s
- vilya 10y agoThat's exactly my point: when the compiler can decide not to do something you told it to you don't have full explicit control over everything. That's not a bad thing though - the compiler usually makes good decisions on your behalf. Usually.
- lightcatcher 10y agoThere's something to be said to be familiar enough with the optimization that you can predict assembly that is not a direct translation of what you're writing. For instance, when writing a scalar-processing loop over a fixed length array with no dependency between iterations you can reasonably expect the compiler to convert to SIMD instructions and do some unrolling. It's not too difficult to check the binary that this happened, and if it doesn't happen you just write it yourself with intrinsics. I often think of C + compiler's optimization passes as a "scripting language for assembly".
- atilaneves 10y ago> Want to make tiny binaries for some obscure system with virtually no storage or RAM? C++, D, Rust, ... > Want to trivially import any of the huge array of existing libraries? You guessed it... If C can talk to it, so can anything else. Granted, having the preprocessor means no bindings, but C++ has that too. > Want to do something you really shouldn't, and any 'sensible' language might attempt to stop, but is called for right here and now... C++, D. Want undefined behaviour? C. Want segfaults? C. Want memory leaks? C. Want memory corruption? C. Want to write insecure code? C. Want to write your own containers? C. Want Go's approach to generics? C. Want to spend 3x as much time writing code as a comparable C++ solution (not to mention more productive languages)? C. I've known C for 21 years now. I just quit a job as a C programmer for embedded systems. If I can help it, never again.
- S_I 10y ago>Want undefined behaviour? C. Want segfaults? C. Want memory leaks? C. Want memory corruption? C. Want to write insecure code? C. Want all of that and a whole bag of inconsistent design choices? C++. Because, yes, one day you will meet a C++ codebase which is a mixture of "forbidden C++" and "modern C++".
- existencebox 10y agoFor the last decade or so writing physics engines has been a hobby project of mine; I shamefully read your comment and realized that absolutely describes my current toy. It's been in various stages of dev since c++11 was newfangled, and combined with being the "learning experience" of a programmer moving over from C; let's just say it shows... (mentally revising my plans to ever apply to C++ jobs and not just go hide in a forest)
- Nursie 10y agoWriting C++ is three times as fast? Perhaps so, but reading it back takes 5 times as long.
- ArkyBeagle 10y ago
- Keyframe 10y agoAs if it were yesterday. Late 89 or early 90, I was learning my first programming language properly (after Logo, BASIC and other stuff). It was C. Common wisdom was that C was bloat and you'd do everything in assembly anyways. I stuck to it (did asm as well). Then, a few years later everything changed with real bloat that was C++ of ye olde and C was back in full force and never quit being there. I've dabbled with C++ before STL craze and never went into it as some of my friends did, but it wasn't quite different. Just a different mental model that it evangelised. These days, I can't even tell what's going on when looking at modern C++. I'm not sure I even want to. C just works for me, and has for a long time. I've worked in numerous languages and platforms, even changed careers from programming (graphics) to film and television. But I'll always be a C programmer.
- retro64 10y agoI have been coding in straight C lately on an embedded personal project and have loved it immensely. I know what is going on and have a good shot at predicting the resulting assembly. However, for me, C falls down on large projects. I know there a lot of hate on objects lately, but the complexity of a project decreases while the maintainability increases when you use properly designed interfaces. You “force” the new guy to do it right because the interface makes it obvious. I know there are ways to enforce encapsulation with C, but I’ve never worked on a (large) codebase where this has been successful.
- Keyframe 10y agoObjects are in C as well, with structures. You can do abstraction like that in different ways in comparison to classes. For example, components are common. Difference being that objects are components, but not all components are objects. Also function objects, aka functors. I try to organize into specific components and piece them together into a sequential or threaded or both model, depending on what I do. Of course, it is specific to what I do. But there are large C projects that are successful - for example, Linux kernel.
- pjmlp 10y ago
- KKKKkkkk1 10y agoI love C with all my heart, but minimal in the syntactic sugar department it ain't. Other languages of its day like Fortran or Pascal would not let you write an atrocity like while (*++targ = *++src) ;
- Nursie 10y agoWell, that's up to you whether you write that way or not. Kinda like with everything else related to C!
- imagist 10y ago> Well, that's up to you whether you write that way or not. The problem is, it's also up to the programmers who worked on my codebase before I did. I would never write that atrocity, but I've come across that atrocity quite a few times in code I needed to modify.
- Nursie 10y agoI'll.never claim there aren't a ton of terrible coders out there. They exist in all languages, but perhapd C gives them the tools to be really, extra awful..
- imagist 10y agoIt also doesn't give you very good tools for refactoring to clean up the awfulness. Unit testing tools and the type system in C are such that it's fairly difficult to ensure that your refactor hasn't broken anything.
- jstimpfle 10y agoperfectly readable :) (but usually generates compiler warning without an additional set of parentheses)
- jjaredsimpson 10y agoNot really an atrocity. More like an idiom.
- tonyjstark 10y agoup to 'trivially import...' I thought about FORTH, not that I have any experience with it besides some tutorials.
- solidsnack9000 10y agoI think the strongest thing in C's favor is that it is an extremely simple language, so it's easy to port and easy to write (semi-broken) compilers for. A language like C++, D or Rust, being more complicated, can take longer to provide ports for.
- Manishearth 10y agoAll three of those languages have at least one compiler with an llvm backend, so writing ports should be as hard as C. Perhaps easier, since you don't actually have to write a parser for anything, just an IR->asm pass.