27 ms·
I love coding in C
- app4soft 7y agoQ: “Do you prefer C or C++?” A: “C tbh”[0] [0] https://twitter.com/elonmusk/status/1211786326670036992 https://twitter.com/elonmusk/status/1211786326670036992
- pjmlp 7y ago> C is closest to the real Nope, plenty of alternatives have existed throughout the history of computing https://en.m.wikipedia.org/wiki/System_programming_language https://en.m.wikipedia.org/wiki/System_programming_language > C as the Ur-language Not really, ESPOL came up in 1961, while C only became a thing in 1969.
- saagarjha 7y agoThey said is, not was.
- smitty1e 7y ago> "...as our dicipline[sic] hurtles away from the bare-metal physicality of lower-level computing" "Physicality" was setting CPU register bits directly via the I/O panel of my UYK-7 back in the day. Happy not to be cruising at that altitude these days (to use an equally inappropriate metaphor).
- thedaemon 7y ago> I hope that it is clear that what I am describing is fundamentally a feeling, and is as such highly subjective... The article clearly states this is a subjective statement and isn't meant to be taken literally.
- haunter 7y agoOne of my early Youtube memory is the "Write In C" song altho I think I saw it written on some Usenet channels in the mid-90s https://youtu.be/XHosLhPEN3k https://youtu.be/XHosLhPEN3k
- spion 7y agohttps://imgur.com/gallery/2O5zM https://imgur.com/gallery/2O5zM
- dleslie 7y agoThe Ur-Language is probably verilog, or at least the raw instructions of the platform; and C isn't much closer to those instructions than many of its alternatives, thanks to optimizing compilers and the differences between the true form of the computational model of the platform and the imaginary one that the C standard reflects.
- linuxlizard 7y agoI've been writing a large app in C again after a few years away. Originally wrote the app in C++ then again in Python and now in straight C. I enjoy the faster compile times of C (over C++) and the better type checking of C (over Python). However, when I'm carefully using C++, I don't have to fiddle around with memory management and still get fast performance and better type checking. Yesterday I wrote a Python script to generate C code test cases. String manipulation in Python is very easy. The right tool for the right job, I suppose.
- swiley 7y agoI honestly don’t like C++ at all as a programmer, “C” is way better for hacking. For example: I understand why the standard was written the way it was but syntactic closures in C++ are way worse than GNU C.
- saagarjha 7y agoWhat are syntactic closures in GNU C?
- swiley 7y agoThis [1] explains how to create them using nested functions. Near the end is a quick blurb about the implementation which is probably why c++ doesn’t do it that way. [1] https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
- jacquesm 7y agoIn August this year I started a new project. In C. Why? Because I'm quite familiar with it and because it allowed me to focus on the problem rather than on the language that I was writing in (the problem is - for me at least - a very hard one). The codebase has steadily grown, at some point it was 7K lines, then I cleaned it up and refactored it back down to about 5500 right now. At some future date it will be - hopefully - finished and I may use it to power applications and/or services. It's super efficient both memory and speed wise compared to what the competition is doing but that's just a side effect. That side effect does allow for some applications that would otherwise be impossible. The code takes one file and outputs another, it's a classical 'unix filter' and it relies on a couple of very high performance libraries that others provided, battle tested and ready to be plugged in. As the project matures I come across the occasional wart in the language, something that I know I could have done more elegantly in a language with richer constructs (lists, list comprehensions, that sort of thing). What surprised me most after working on this project for a couple of months though is how well the language fits the problem, that's something that I did not really think would be the case when I started out with it. But as time passes and things 'find their place' it is indeed just like the author writes, I - still - love coding in C. Even if I know there are better alternatives out there and I should probably get invested in one of them one of these days at the 75% mark or so of my career I'm still very happy that I picked C at the start and never once did I imagine that it would still serve me this well 37 years later. Most of the other 'hot' languages of the days that went by in the meantime have all been lost, and yet, C is still here, and likely will still be here for years to come. To me it's like an old knife. Not pretty, known to hurt you if you abuse it but still plenty sharp and well capable of doing the job if you treat it well.
- swiley 7y agoI’ve been messing with yasm lately and I think I might like that more. I’ve written a lot of C (including a small browser) and definitely enjoy it (especially parsing, which is probably not great.) The more I’ve written and read though the more I’ve realized how much it really does abstract and that kind of sucks the joy out of it for me. For actually getting things done there’s go and python of course.
- raverbashing 7y agoC is simple. But not ergonomic. Or safe. It's bad to the point of being almost broken. "Oh but it's powerful", yes, a chainsaw is powerful but even that has an emergency brake. C has none of that. (Not to mention compilers that take "undefined behaviour" as a code word for "bamboozle time")
- jacquesm 7y agoThe C emergency brake is called valgrind.
- saagarjha 7y agoOr some of the newer sanitizers.
- pjmlp 7y agoExcept it doesn't break on all platforms. So I have to hope for the best when some piece of hardware, coded in C, gets plugged into the network. Morris Worm is 30+ year old, and yet best we can do is having platforms like CHERI, Solaris SPARC ADI, ARM MTE, which also happen to be quite specific.
- jacquesm 7y agoC has a bad rep, and deservedly so in the context of anything related to security. We have not been able to sanitize the language because the ability to surprise the writer of some bit of code is almost a core concept in the language. This sucks. But at the same time the way newer languages are held up as the manna from heaven is rather tiresome as well. By the time they will be deployed in the quantities that C code is today there will be exploits in those languages because it is mostly the programmers that are fallible. Now, some languages - again, C - make it easier to shoot yourself in the foot than others. But I've seen plenty of code in Java that was exploitable even though memory safety in Java is arguably at a higher level than say Rust. So you'll be hoping for the best for some time to come, no matter what the stuff you install was written in.
- adamnemecek 7y agoThere is little content in this article.
- saagarjha 7y agoIt’s reasonably short and it gets its point across. What where you looking for, specifically?
- unlinked_dll 7y agoC has aged liked fine wine with the cork out.
- diego_moita 7y agoPick the right tool for each job. C is fantastic for small and very optimized components. If you want it portable, from microcontrolers to GPUs, it is the best choice too. But if you need rapid prototyping, large scale projects within an organization or some libraries and tools to make the perfect, animated, templated and themed user interface you should think twice before choosing C. Java and .Net are freaking good at database integration and memory allocation/dealocation. Erlang is fantastic for multitasking and multithreading. Ruby on Rails or Python on Django are much better to prototype a website nannying a database. You already know the saying "if your only tool is an hammer..."
- Delmania 7y agoCame to make a similar point. There's lot of love for C and belief it's some superior language to modern ones. The reality is that C hasn't aged well as hardware has evolved. It's not that good as parallel programming, which is more prevalent considering most CPUs are multi-core. The ACM did a great write-up on this, https://queue.acm.org/detail.cfm?id=3212479 https://queue.acm.org/detail.cfm?id=3212479 The bottom line is to echo your first statement, C is a tool, good for some tasks, not so good for others. Choose accordingly.
- Koshkin 7y agoThat's one of the good points about using C today. Unlike, say, Fortran (or any other language with a high-level semantics) C may be hard for a compiler to optimize for a modern CPU.
- sys_64738 7y agoIf it had native support for JSON and supported GO like co-routines then a lot of its utility would be applicable to other scenarios that use fancy languages. This is what I always find surprising, C’ syntax is robust so add these missing gaps to the current language and weall win.
- CDSlice 7y agoIsn't that basically Go's whole thing? It has syntax that is really close to C, Go style co-routines (obviously), and I believe it has good JSON support. You do have to use a GC and you lose some of the really low level bits but I would wager most C programs don't need to have no GC and be able to do the really low level stuff. The ones that do probably don't need the goroutines and JSON and stuff.
- SlowRobotAhead 7y agoI have an embedded project with an RTOS for things you would do with coroutines, CBOR and JSON handling. I would not appreciate a garbage collector when it came time to debug a dumb pointer or overflow error. Doing it in C obviously. I can’t really consider Go embedded because I’m maxing the chip out now in C without including a Go VM or whatever you call it. This isn’t all as rare as you might think.
- petee 7y agoThey're not standard, but have you checked out libmill.org or libdill?
- krapp 7y ago>C’ syntax is robust so add these missing gaps to the current language and weall win. If every feature everyone wanted was added to C, it would just turn into a bloated mess like C++, or like PHP, with tons of bad decisions baked into the language that can't really be avoided. Features like this belong in libraries - the stdlib should remain as minimalistic as possible.
- saalweachter 7y ago
- TazeTSchnitzel 7y agoC is low-level enough to hang yourself and high-level enough to make it hard to tell when you've done so. It was a great language in the 1970's.
- bachmeier 7y agoAnd is now obsolete, for the most part. Unless you want to do something on hardware that only has C support, there are generally better options. The language has not evolved. There are now several good alternatives in this space. Edit: Rather than responding to all the comments individually: There's obviously a lot of code already written in C. That doesn't mean it makes sense to write new code in C. Sometimes C is your only option, which I already said in my original comment. None of that means there is any reason to start a new project in C if it's going to run on Windows or Linux, and it's certainly not the case that you have to write your program in C if you want to call it from another language. Sorry if this disappoints anyone, but it's true. For the traditional uses of C, such as writing a text editor or a library for numerical computing that you're going to call from another language, there's no longer a sensible argument for using C.
- Koshkin 7y agoJust do not expect Linux (the kernel) to be rewritten in some other language any time soon.
- marcosdumay 7y agoWe will have to move to microkernels at some point. Maybe this is the correct point.
- ncmncm 7y agoHypervisors, most likely. Linux (like the BSDs) being C is an organizational phenomenon. There is no plausible technical reason for it not to be, or include parts in, C++. History is path-dependent; how we got here matters more than what is around today. There is really no excuse for systemd being C. It is really a drag on its development.
- dcchambers 7y agoWe used C99 in some of my classes at college, despite attending University from 2010~2017. I liked C a lot - working that 'close' to the hardware and the OS really gives you an appreciation for how things work. These days I've moved on to things like Ruby, Python, and Go...but I'd like to give it another try with C18 to see what modern C looks like.
- nlh 7y agoThis is timely! I never really learned C deeply (other than from a class in high school where pointers were considered 'too advanced'...so we didn't really get too far). I decided to work on this year's Advent of Code in C and it's been a blast so far. I finally "get" pointers, malloc/free, etc. (and I have a newfound appreciation for Ruby things like CSV.each). Since most of my coding work is at a much much higher level (web backend & frontend stuff), I probably won't spend too much time in C in the regular course of business, but it's massively helped me better have an intuitive understanding of previously vague things like "allocate on the stack vs. heap" and "pass by reference vs. value" -- I knew what those words all meant but didn't really have a feel for what they actually did. C helps with that.
- nabla9 7y agoIf you want to write a library that can be used from many programming languages in many operating systems, exotic processors and application plugins, use C. If you want to write a new programming language that does the same from the get-go, make C as one of the compilation targets.
- cosmic_quanta 7y agoOr export a C-API! As other commenters have pointed out, use the right language for your use-case. Then, I agree that exporting a C interface is best.
- ncmncm 7y agoExporting a C interface preserves some flexibility. That is often best.
- mechhacker 7y agoI really like programming in Rust, when I do. Got as far as playing around with a web site that used Rustler NIFs on a Phoenix website. Why? It was just fun to figure out how to get something from the web frontend through Elixir and into some Rust numerical code. I don't think it had much practical use and knowing me, would be ridiculous to go back and touch it again (likely). I've been thinking about doing Rust to WASM but I'm still not sure if it's just throwing a bunch of complexity at something just because. It holds my interest a lot more than just doing everything in JS, though. C was also more enjoyable to me than other languages. Something about compiled languages just feels really nice. But, it's also great to have a REPL available right there. Like with Elixir.
- dnautics 7y agoIf you like low level nifs in elixir, check out my project: https://hexdocs.pm/zigler/Zigler.html https://hexdocs.pm/zigler/Zigler.html
- TallGuyShort 7y agoI used to enjoy C. If you enjoy C for these reasons, you may enjoy Go and the benefits of living in this millenium. It feels very minimal with a lot of control. Similar heritage. Definitely feels like C a lot of the time. And you get to use the & operator if that's your thing. It's got cleverness to infer types if you want, but you can specify types and a lot of the cleverness is at compile-time: still very light-weight to run. And it's got a better standard library which makes it a lot more fun to throw something together quickly.
- cyphar 7y agoWhen it comes to simplicity, Go is definitely quite similar to C in many respects (and not all of them good). After I started working with Rust for a little bit, I think it better matches the feeling I get with C when it comes to being confident what your code will do when compiled. To be fair, there is still a fair amount of magic you have to get used to. But the main issues that I've had with Go is when the Go runtime is causing your code to very much not act like a C program. To be fair, I work on container runtimes and similarly "lower level" tooling so this is probably not an experience that everyone else has.
- saagarjha 7y agoI haven’t used it much, but I really dislike Go despite liking C. It adds new things but just barely, to the point that it makes me want to not want the watered-down features it has.
- frou_dh 7y agoOne of these decades we'll get over compulsively raking the embers of Bell Labs
- actionowl 7y agoI DO TOO! Warts and all, the simplicity is really refreshing, especially in an age of: "Make sure you have homebrew installed and then `curl http://goodluckbro.io/doomed | sh` then go read the Medium blog post to write your first hello world" And don't get me wrong, I love and use whatever-shiny-lang-v2 too, I just really like the simplicity of C (and yes I acknowledge that comes with the ambiguity and undefined behavior too).
- MadWombat 7y ago> Make sure you have homebrew installed and then `curl http://goodluckbro.io/doomed http://goodluckbro.io/doomed | sh` So... what is the equivalent of doing this in C? * install C compiler (no, there is no install script, go google and install yourself and yes, the process is different on different platforms) * read a blog post (possibly on medium) and write hello.c * read another blog post to figure out compiler flags * ./a.out A bit less convenient, but not that different
- augustk 7y agoFor those who appreciate radical simplicity I can recommend the Oberon programming language. As opposed to C it is a real high-level language whose language specification contains no reference to any computing mechanism: https://www.miasap.se/obnc/oberon-report.html https://www.miasap.se/obnc/oberon-report.html
- fortyrod 7y agoI'm finishing up an embedded side project over the holidays. It's C, FreeRTOS and the STM USB host stack. I spend most of my time with the likes of React JS, Swift and Kotlin so it does give me a good, honest, fresh feeling. Also the feeling that my code could rise up and smite me at any moment! Great read, thanks for posting!
- tyingq 7y agoI wish C had gone back and done a better implementation of strings that wasn't null terminated. Or at least an implementation of plain buffers with a richer libc for manipulating them. I know you can find bolt ons, but they aren't terribly useful since no 3rd party libraries use them. You have to resort to stuff like antirez's sds.
- Koshkin 7y agoThe choice is natural for C, due to the fact that it is the only way to implement the string simply as an array of characters. (Doing anything else would go against C's view of the world.)
- tyingq 7y agoI suppose, but watching everyone mess about with a struct that has length and a buffer, and a dearth of built-in functions to manipulate it, is...ugh.
- tyingq 7y agoAn article with a better, and more dramatic explanation of the same opinion I have: https://queue.acm.org/detail.cfm?id=2010365 https://queue.acm.org/detail.cfm?id=2010365
- 59nadir 7y agohttps://github.com/antirez/sds https://github.com/antirez/sds is heaps better than just using raw strings. Any language with a fat pointer variant out of the box is going to do it better than C as well. That's not to say that the rest of C is bad, but there's nothing to be gained from defending C strings; they're bad and you should probably use an alternative and that alternative should probably have a length and a buffer, even better if you can find this out of the box in your language that otherwise works a lot like C.
- saagarjha 7y agoI’m not sure I would call typedef struct { size_t size; char *buffer; } string_t; “un-C”.
- pgruenbacher 7y agoShout out to [zig](https://github.com/ziglang/zig https://github.com/ziglang/zig) as a modern alternative to C.
- TheRealPomax 7y agoThe arguments sound like they should be about Rust at this point in history.
- decasia 7y agoI thought the OP was a little bit self-undermining, because as the author points out, C feels "close to the metal" but at this point it's actually virtual metal. But I think "getting close to the lower levels" is still great. The only time I write C-like code is for an Arduino. Turns out that even if your electronics is rusty, you can do some very useful things in the real world by automating a simple motor and a few LEDs. And whenever you have to interface with individually numbered output pins, it feels pretty low level. tldr: if you want to get close to the metal, it's really useful to start by switching to hardware that facilitates this.
- saagarjha 7y agoEven assembly goes through a number of layers before it’s run. With microcode, virtual memory, and privilege rings there’s a number of things between the code and what it’s running on.
- decasia 7y agoYeah, that's why I used expressions like "closer to the lower levels" — you can descend in orders of abstraction/layers of reality, but it's more asymptotic or directional than anything else. It's not like playing with an Arduino is getting me closer to understanding the underlying chemistry and physics. Which are also interesting to learn about... But personally I think it's for the best that reality is inexhaustibly complex (from the perspective of human individuals).
- almindor 7y agoWhen you said rusty i thought you meant Rust for a moment :)
- klingonopera 7y ago> C feels "close to the metal" but at this point it's actually virtual metal. Since you mention the Arduino, when you start out with it, you're most likely turning on digital pin e.g. D7 with digitalWrite(7, true), it's easy to understand and quick to implement. But spend enough time on the Arduino (or C or Assembly or embedded in general) and you'll soon realize that all that function call is doing is basically the same as doing PORTD |= 0x80. That's a bit tougher to understand, but even quicker to implement, and once you've understood how that all works together, a lot of everything about computers and digital electronics becomes so much easier to understand. Here's a more detailed guide for the Arduino example: https://www.instructables.com/id/Fast-digitalRead-digitalWrite-for-Arduino/ https://www.instructables.com/id/Fast-digitalRead-digitalWri...
- SandPhoenix 7y agoI also took Jae’s cs3157, and I loved every second of it! So glad to see a fellow AP alum on HN ;)
- caseymarquis 7y agoCoding in C is like camping. It's fun for a while, but eventually you really miss things like flushing toilets and grocery stores. I like using C, but I get frustrated every time I hit a wall trying to build features from other languages. C is a very WET language. C is basically it for embedded development though. I've gotten so tired of recompiling and waiting to flash a chip with C, that I've started learning ANTLR and making my own language. The idea is to have a language which runs in a VM written in C, and allows you to easily access code which has to be in C. Sort of like uPython, except it can easily be used on any board with a C compiler with a small amount of setup, and C FFI is a first class citizen. Also coroutines masquerading as first class actors with message passing, since I always end up building that in C anyway.
- rumanator 7y ago> Coding in C is like camping. It's fun for a while, but eventually you really miss things like flushing toilets and grocery stores. The only issue I have with C is that there are no good reasons for some of those missing features to be missing. Take, for example, namespaces. Would it be a problem to implement them as, say, implicit prefixes?
- AllanHoustonSt 7y agoWould it be possible to do this without changing how name mangling works?
- edwinyzh 7y agoI LOVE coding in Delphi, really. And I start my new project (document-focused website generator @ https://docxmanager.com https://docxmanager.com) in Delphi, while some people think it's dead :) And I like C (although I don't code in it), because Delphi/Lazarus can interface with C very easily, you know, there is a lot of C libraries around the world :)
- BadThink6655321 7y agoI love coding.
- Koshkin 7y agoIndeed, there is something very satisfying in constructing things that work (no matter the language).
- FpUser 7y agoI like C myself. I wish though they would extend it with some features similar those available in Delphi/FreePascal language. This will still leave complete low level control and have amazing compile speed and eliminate much need for C++. C++ is heavy beast and while I a have to use it every once in a while I do not particularly enjoy compile time and some other things. Zig made some moves to that direction but not yet enough and is not really ready for production anyways. Also I did some experiments and initial impression was good but it failed to compile used libs written in C even though it claims to be a C compiler as well.
- dnautics 7y agoZig is fantastic, but it's still early days. If it doesn't compile all c code ever I wouldn't mind taking c code, wrapping it into a .a and dropping it into a zig program until you can untangle it.
- anderspitman 7y agoComing back to C always feel very refreshing, until I have to manipulate strings or open a socket. Currently golang is striking the right balance for me. Rust has a place as well, but it still slows me down too much. When writing Rust there's sort of this constant background anxiety that I won't be able to get the compiler to accept the thing I'm trying to do. I'm excited about async/await though, and when you must have safety+performance it's awesome.
- MiroF 7y agoI think it really depends on what you are writing. I built up a column-store database in C recently (not for production, just as a project) and while I can imagine rebuilding it in C++/Rust, I don't think it would be possible to do it in a performant way in Go. Go is nice, but tends to be very verbose and also is garbage collected. It's for a different usecase.
- anderspitman 7y agoYeah I could have been more clear. Vs C, I use Go for productivity tradeoffs (strings, networking, etc). For performance I would use Rust.
- boramalper 7y ago> C is closest to the real I think you can argue for both sides of the argument, but what is the obsession for "the real"? Is the JavaScript running in your browser "less real" than the x86 Assembly that is compiled once again to Intel Microcode?[1] > When I saw a memory address outputted to stdout, I felt that I was as close to the hardware that I could be without literally opening up my machine and stroking my ram modules like a crazy person. Sorry to shatter your enthusiasm but the memory address you are seeing is very likely to be "virtual" and not physical thanks to paging: yet another damn abstraction![2] ----- > "If I have seen further, it is by standing on the shoulders of giants." --Isaac Newton The Magic of Software: How I Learned to stop Worrying and Love Abstractions http://www.thecaucus.net/#/content/caucus/tech_blog/373 http://www.thecaucus.net/#/content/caucus/tech_blog/373 ---- [1]: https://en.wikipedia.org/wiki/Intel_Microcode https://en.wikipedia.org/wiki/Intel_Microcode [2]: https://wiki.osdev.org/Paging https://wiki.osdev.org/Paging
- ThrowawayR2 7y ago> "I think you can argue for both sides of the argument, but what is the obsession for "the real"? Is the JavaScript running in your browser "less real" than the x86 Assembly that is compiled once again to Intel Microcode?" Because when the shiny abstractions and bloated frameworks layered like a Guinness World Record stack of pancakes† that attempt to hide all the underlying complexity of the hardware break down and leave you with a problem, be it CPU, memory, or I/O, then anyone without an understanding of what's going on at least at the assembly and architecture level will be up the proverbial creek without a paddle. I've made good money bailing people out of these kind of problems. † About 60 cm or 2.5 feet apparently. https://www.eater.com/2012/2/21/6611971/this-is-officially-the-worlds-largest-stack-of-pancakes https://www.eater.com/2012/2/21/6611971/this-is-officially-t...
- deleted 7y ago[deleted]
- reza_n 7y agoI write C full time and I love it (Varnish Cache). In our team of about 10 full time C engineers, we spend less than an hour a month dealing with things like “memory safety”. When you are writing C at a professional level, your time is spent on things like performance, algorithms, accuracy, hitting requirements, and delivering software. We have numerous safety and fuzzing systems humming in the background checking our work 24/7. The tooling ecosystem (Linux) is top notch. (If you want to write C full time professionally too, contact me!)
- helixes 7y agoWhat does it take to become a professional C engineer? How did you practice all that. I currently started looking into it, but I'm kind of just getting started (Books/PDF, Exercises).
- reza_n 7y agoTime and experience. Learning the syntax and wrapping your head around pointers and memory is the first step. After that, just try and write as much C as possible. Help out with projects, start projects, write tools and APIs. Most importantly, study existing code bases to learn real world techniques. Best case, get a C job where you have to write a diverse amount of C code day after day.
- GeorgeTirebiter 7y agoI can't recommend this book enough: Code Reading: The Open Source Perspective by Diomidis Spinellis. See also https://news.ycombinator.com/item?id=21006995 https://news.ycombinator.com/item?id=21006995 for several others.
- kstenerud 7y agoC was my first love, and I still find myself using it often, especially in library code when I want versatility. I've gotten into the habit of pushing memory allocations as far back into the user program as possible (where the user asks for the size of the internal struct, allocates and casts, and deals with ownership himself) to allow more flexibility to the users of my libraries, and also to remove entire classes of memory issues from the libraries themselves: https://github.com/kstenerud/c-cbe/blob/master/tests/src/readme_examples.c#L229 https://github.com/kstenerud/c-cbe/blob/master/tests/src/rea... As a bonus, it avoids the headaches of mixing multiple allocators (malloc, new, [NSObject alloc], JNI, etc) when used in cross-language codebases. It does, unfortunately, complicate the API a little bit, but I find the tradeoff to be worth it in terms of safety.
- codr7 7y agoI've spent roughly 20 years going back and Forth between C and C++, trying to decide whether to add the missing pieces my way to C or strip away what I don't need from C++. I'm currently in a C phase after having spent quite some time writing interpreters in C++. The most common issue I see in C codebases is excessive memory allocation. In most other languages, allocating a chunk per instance is the only way, though the implementation likely does some kind of pooling/slab allocation behind the scenes. In C you have the freedom to arrange your allocations any way you feel like, and treat any segment of N bytes as a value of size N. As an example, most collections (vectors, sets, maps etc); tend to only work with pointers; which is a huge missed opportunity. My two favorite kinds of collections in C are embedded linked lists [0] and deques. One reason is they both provide stable pointers (which is the main disadvantage of vectors), the other that they minimize the number of memory allocations. I recently implemented a pool allocated deque library [1] in C that uses embedded linked lists to keep track of memory blocks and provides value semantics, which I hope will make my interpreters easier to implement as well as increase performance. Time will tell. [0] https://github.com/codr7/libceque/blob/master/source/libceque/list.h https://github.com/codr7/libceque/blob/master/source/libcequ... [1] https://github.com/codr7/libceque https://github.com/codr7/libceque
- GuB-42 7y agoThat song sums it up nicely https://youtu.be/tas0O586t80 https://youtu.be/tas0O586t80
- continuational 7y agoHardware has evolved a lot since C was made, but is there a way to actually make use of this that isn't available in C? It seems like modern CPUs do an awful lot to support programming like it's still the 1970'ies.
- m0zg 7y agoI love coding in a "C+" subset of C++, meaning minimal use of OOP, and maximal use of RAII, smart pointers, and STL. It's hard to beat C++ when it's scoped down like this.
- saagarjha 7y agoThere’s still a couple finicky memory issues, unfortunately.
- skocznymroczny 7y agoI really like C too, although I wish it was slightly higher level. Proper strings, built-in hashmaps, few other small improvements.
- memn0nis 7y agoI too, love coding in C. I love how you can control almost everything. There's nothing "mysterious" or abstracted away. Maybe I'm OCD, but I love understanding exactly what is going on in my programs at all times
- klingonopera 7y ago#include <stdio.h> int main() { char a = 3; char b = 8; char result = a * b; printf("%u %u %u %u", sizeof(a), sizeof(b), sizeof(result), sizeof(a * b)); return 0; } PRINTOUT/x86-64: 1 1 1 4 Surprising results on x86, probably not on Arduino or embedded platforms. The phenomenon is integer promotion in C. EDIT: "Char isn't an arithmetic type!" The same is true for short, too. EDIT2: The consequences are: Might need to manually %= 0x100 instead of relying on data type's inherent modulation in multi-operational arithmetic, making rotation a necessarily pre-scan operation for LE/BE conversion instead of a process-transparent operation and I had a third one... hm. Also, maybe speed loss?
- yogthos 7y agoA lot of people have a misconception that C is close to the way hardware works. The reality is that hardware engineers jump through lots of hoops so that C programmers can keep pretending that they're coding against a really fast PDP-11. Not only that, but these contortions directly lead to vulnerabilities like Meltdown and Spectre because memory representation in C doesn't map well to modern hardware. C family of languages is basically holding us back from utilizing current day hardware safely and efficiently. This [1] is an excellent essay on the topic. [1] https://queue.acm.org/detail.cfm?id=3212479 https://queue.acm.org/detail.cfm?id=3212479
- x3ro 7y agoThanks! Comments like these, especially backed by good sources, are why I keep coming back to the often pretty messy comment sections of HN :D
- pmarin 7y agoMeltdown and Spectre are hardware bugs.
- aduitsis 7y agoGood comment and article, thanks! One thing only, the phrase "The quest for high ILP was the direct cause of Spectre and Meltdown" is slightly exaggerated. One might say ILP was rather a remote indirect cause, not the direct cause. Blaming it on C and ILP maybe is kind of far fetched. That isn't to say that the article doesn't have a point though, far from it.
- codebolt 7y agoAny idea what a low level language that was designed for today's hardware would actually look like?
- yogthos 7y agoGPU shaders are a good example.
- dodobirdlord 7y ago
- yogthos 7y agoRTFA
- russtrotter 7y agoFor as much as it seems comical, I enjoy languages like Java and C# so much because while I cut my teeth on lots of C (then C++) from the early 90's onward, once the web began to drive so much development, you needed to be able to sling strings (with first-class Unicode support) around. When I was learning C++, a popular idiom to employ was to use it as "a better C". Don't make your own classes, but leverage good "string" impls, templates, namespaces, declaration semantics, RAII, etc, basically stuff that just your C... better. I can appreciate good nostalgia, but I can't help but think i'd be pining for some of those betternesses that came along later.
- master_yoda_1 7y agoMe too
- bitwize 7y agoI love coding in C, too, but the writing is on the wall. C is being put out to pasture in favor of Rust.
- pxi 7y agoC with valgrind and other analysis tools (clang scan-build/cppcheck etc) make for a powerful combination. There should be a dinky acronym. But, never gonna happen in my workplace.
- mobilemidget 7y ago"I hope I am able to explain some of the psychological appeal of lower level programming in C." Felt like I finished reading the intro here, only to realise I scrolled to the bottom with only 2 paragraphs to read more. Guess I was expecting a bigger read, perhaps with some code references, as it was interesting :)
- roca 7y agoC has not really been close to the metal for a long time. The C abstract machine is actually very complicated, especially the rules around undefined behavior. John Regehr's blog explains this well, e.g. https://blog.regehr.org/archives/213 https://blog.regehr.org/archives/213. It's easy to write code that looks like it will compile to something simple and obvious but that current compilers will turn into code that does something else entirely, or worse, current compilers compile as you expect but future compilers will mangle. If you want to be reliably close to the metal, I think the best options are either to actually write assembly, or use a language with a simpler abstract machine (preferably, no undefined behavior), that statically compiles to code that maps closely to what you wrote, and lets you generate "idiomatic machine code" (e.g. unboxed integers). Safe Rust fits the bill, but there are probably other more obscure options.
- userbinator 7y agoI do too, mainly C + Win32. Besides Asm (which I use a little bit of too), it's the only way I've found to write applications which are a single tiny executable with no dependencies nor installation, and will work from Win95 upwards (and likely WINE as well). I'm a little surprised that the author didn't mention efficiency.