17 ms·
The Unreasonable Effectiveness of C (2013)
- nkurz 12y agoDiscussed a couple years ago here: https://news.ycombinator.com/item?id=5037089 https://news.ycombinator.com/item?id=5037089 And the followup discussed here: https://news.ycombinator.com/item?id=5075370 https://news.ycombinator.com/item?id=5075370 I repost because I'm becoming more and more frustrated with C, and wondered if others feel the same. Not because it's too low-level, rather because it seems to be trying (with limited success) to move to being a higher level language. There are times when I really do want a "thin layer over assembly", and C seems to have evolved to a place where that is no longer possible. I explain my distress more completely in the comments of Yann's article here: http://fastcompression.blogspot.com/2014/11/portability-woes-endianess-and.html http://fastcompression.blogspot.com/2014/11/portability-woes...
- pgeorgi 12y agoBefore reading the comments on that article I knew that it's about C compiler authors (ab)using properties of the C language for optimizations (and other code-breaking behavior). Properties that some language lawyers cooked up back in the stone age to keep all compilers on all architectures compliant without further effort for developers, which is typical design by committee cruft to get people to buy in on the standard.
- kazinator 12y agoIf C was ever a thin layer over assembly, it stopped being that sometime in the 1980's, not recently. C has not radically changed since the 1989 ANSI C standard. The library is bigger and there are some bells and whistles in there like designated initializers, structure literals, complex numbers and (now optional again) variable-length local arrays. The trick of abusing C as a higher level assembler that are undefined behavior today were already undefined behavior already in 1980-something, and before that there was no standard at all, other than the K&R1 book.
- alayne 12y agoThe best optimizing compilers are C and C++ compilers (as far as I know), perhaps Fortran but even that is getting more esoteric. I think that's why the assembly idea persists.
- andrewchambers 12y agoLanguages which don't have pointer aliasing, but use gcc back ends are probably the fastest around. So I think gcc based fortran. C can work around this using restrict keyword though.
- kazinator 12y agoRestrict is a big hammer. You can work around aliasing suspicions simply by imagining you are programming a load-store machine whereby local variables are registers. You have some algorithm that works with several different objects, such as structures and arrays. You can help the compiler (and the reader!) by writing code that isn't repeatedly dereferencing pointers, but which loads values into local variables. Concrete example, doubly-linked list deletion, bad: victim->prev->next = victim->next; victim->next->prev = victim->prev; Here, the compiler doesn't know whether the assignment to victim->prev->next clobbers victim->next, which is evaluated again in the second line. If you write it like this, you eliminate that concern: node *prev = victim->prev, *next = victim->next; next->prev = prev; prev->next = next; The assignments here do not interfere; the first line assigns to a "prev" structure member, and the next line doesn't access any such thing; it uses the "prev" local variable which isn't a structure member. Caching in local variables is preferrable to using some C99 "restrict" voodoo that introduces undefined behavior. In the case of the above linked list, where are you even going to stick "restrict"? There is only one incoming pointer, victim. Making that "restrict" doesn't help. You need it in the actual structure declaration on the next and prev pointers. Yuck, what?
- dagw 12y agoThe gcc Fortran compiler is unfortunately pretty far behind the state of the art (or at least was a couple of years ago). To really realize the true potential of Fortran you unfortunately need to get a commercial compiler.
- twelvechairs 12y agoNot so long ago C was the 'high level language' to assembly. Now few people use assembly and C is 'low level'. Its a weird transition to make. There's still plenty of people around who want to use C for high level tasks.
- userbinator 12y agoYou might be interested in this: http://blog.regehr.org/archives/1180 http://blog.regehr.org/archives/1180 (discussed here: https://news.ycombinator.com/item?id=8233484 https://news.ycombinator.com/item?id=8233484 )
- deleted 12y ago[deleted]
- throwawayaway 12y agoIf I follow exactly what you are saying, you should say it's C coders who rely on undefined behaviour and compiler writers you are frustrated with, not the language itself.
- Scramblejams 12y agoIf I didn't know a little about Damien, I would swear this piece was an audition for The Onion.
- Scramblejams 12y agoTo you downvoters, consider that the top-voted comment in an earlier submission (https://news.ycombinator.com/item?id=5037089 https://news.ycombinator.com/item?id=5037089) agrees with me in principle, if not in form. If nothing else, the endless onslaught of critical security bugs we see throughout the software world suggests that we should be striving mightily to deprecate memory unsafe languages, not writing lovefests about rediscovering the worst of the offenders.
- maaaats 12y agoWhy not post this valid comment instead of the garbage you first wrote, then?
- Scramblejams 12y agoBecause I don't expect to have to hold the hand of every reader of my comments. Before you criticize what I wrote, do a little work to understand it. I suggest you re-read Damien's piece, this time as an example of satire, and see how well it works. I can't do it all for you.
- deleted 12y ago[deleted]
- kyberias 12y agoWhat a horrible pissing contest that thread contains.
- Scramblejams 12y agoLanguage wars in a nutshell. However, there is a significant contingent of C practitioners who delude themselves into thinking that they are good at writing secure code, when all the world's evidence militates against them. Hopefully one day "C Considered Harmful" is a meme and we only use C, and its memory-unsafe friends, in hopefully increasingly rare places where absolutely nothing else will do. We need to be realistic and admit C/C++ is part of the problem. The sooner everyone accepts that, the better.
- rubiquity 12y agoIt almost seems cruel to have a blog post written by someone from more than a month ago on the top of HN on Thanksgiving Day. I can picture the turkey and stuffing slowly tumbling out of Damien's mouth as he learns a blog post he wrote from early 2013 is at the top of HN right now.
- maaaats 12y agoIt claims C has the fastest build/run cycle. Is this true? Many non-compiled languages I can redefine my functions at runtime, and with Java I can hotswap.
- foxhill 12y ago> build/run cycle > non-compiled language C isn't trying to fit in that space. regardless, C code takes far less time to parse, optimise, and convert into machine code than all the languages i know of.
- astrodust 12y agoNope. Pascal is still king here. The speed it compiles at is simply ridiculous. Largely this is because the way the modules are defined makes it a snap to pre-compile aggressively. You're simply not allowed to do things that would change the way a module is understood by the compiler based on context. The biggest hold-up in the C world is rampant abuse of #include files.
- sika_grr 12y agoYea, change a certain .h file and you can go get a cup of coffee while waiting for the build to end. This can't ever happen with Java/C#.
- foxhill 12y ago> The biggest hold-up in the C world is rampant abuse of #include files. true. > Nope. Pascal is still king here. for some values of "king" perhaps - perhaps it's faster to compile, but no arrays of varying lengths in a function call? and if i'm not mistake, a function must be defined before use..? pascal is incredibly restrictive, and perhaps that's why it's so fast.
- astrodust 12y agoIt's very restrictive, but the compiler is unbelievably fast because of that. I'm not going to argue that Pascal is somehow superior to C, it's far more limited, but there's lessons here to be learned on how to make a compiler rip through code.
- gfodor 12y agoSo, is Rust a better C? I haven't dug into it too much but that's been my general impression of how it's been presented.
- dllthomas 12y agoI doubt it's a uniformly better C, just because the space has so many dimensions. I hope it's a better C, on balance, for the things I want to use C for. I've not yet dug in, though.
- femngi 12y agoRust is aiming more to be a better C++. However that doesn't preclude it from being used in the same space as C and can become impressively lightweight if you forgo things like the standard library. I would argue that it is better than C for its safety features alone. I hope in the future we look back at this decade with its never ending stream of buffer overflow vulnerabilities with disbelief that we could use something so insecure for something so vital.
- reikonomusha 12y agoShouldn't we already be suspended in disbelief given the languages we have had for 30+ years?
- pjmlp 12y agoI have been complaining about it since the MS-DOS days and many of us used Turbo Pascal, when Assembly wasn't required. Somehow along the way UNIX spread into the enterprise and brought C along.
- byuu 12y agoIt's somewhat higher level than C. I've always liked to think of myself as a programming polyglot, able to pick up and reason about nearly any standard language. I'm not sure how Rust is doing it, but the more I read and learn about it, the more confused I am. The syntax is just so bizarre and counter-intuitive to me. And yet I seem to be the only person in the world who doesn't "get" it. It's as if the language is being explicitly developed to be my kryptonite.
- hbex5 12y agoIt's basically impossible to write safe code in C. Surely that makes it pretty damn poor at what it does? Especially since system code is the place where safety matters most.
- Scramblejams 12y agoI have no idea why you're being downvoted. Probably by a bunch of people who think they can write C and C++ without writing any security bugs. Which, unless they belong to a vanishingly small group of security experts who write code in an organization with the same discipline, budget and glacial schedule of the team writing software for the Space Shuttle, they can't.
- foxhill 12y agoexcept that literally every piece of software you have ever written or used goes through some level of C, and lets face it, most of the C projects out there are actually pretty well tested. and it's not like higher level languages are significantly better in this regard.
- Scramblejams 12y agoIt's not like higher level languages are significantly better? Seriously? The last time I accidentally executed injected code due to a free error or buggy bounds check in the non-C parts of Python, or Ruby, or Lisp, or Erlang was ... well, never. And no, the fact that C underlies all these languages isn't a testimony to the adequacy of its security features.
- foxhill 12y agothe python interpreter, CPython, is uh, written in C. CRuby, too. if C was so insecure, you would expect that interpreting python would be just as "insecure" as writing C.
- Scramblejams 12y ago
- adamnemecek 12y ago"When someone says: 'I want a programming language in which I need only say what I wish done', give him a lollipop." What a shitty, shitty quote.
- Double_Cast 12y agoThis quote confused me. What does it mean?
- ArtB 12y agoThat dream is so child-like that you should treat that person as a child.
- deleted 12y ago[deleted]
- htor 12y ago"C has the fastest development interactivity of any mainstream statically typed language." This is not true. Using debuggers and crash dumps to reason about your running program is not fast or interactive at all. It doesn't matter how good the tools are. They interrupt the workflow. Working in a REPL on the other hand, is real interaction with your program. You speak the language to the program and the program responds in the same language. This is fast.
- dap 12y ago> Using debuggers and crash dumps to reason about your running program is not fast or interactive at all. Says you. Using core dumps is a different kind of interactive. Rather than testing out what a program would do or could do (as in a REPL), you're iterating on theories of what it did do. It definitely takes getting used to, but I'm often much faster debugging (even non-fatal problems) from a core file than iterating with a step-through debugger or rerunning the program. It also helps a lot when the tools within the core dump debugger are good -- you should be able to quickly dump what's relevant to your program (not just threads and stacks and individual objects).
- sampo 12y agoBut he said "mainstream statically typed language". Is there one such with a REPL? Would Scala be mainstream? OCaml or Haskell probably not.
- vezzy-fnord 12y agoHonestly, sometimes I think that the best solution for fixing the security quagmire we're in would have been not by creating a new system programming language like Go, Nim, Rust, D or even reviving one of the Wirthian languages (though having them on the table is very much a good thing), but by creating a memory safe dialect of ANSI C, and perhaps standardize on a better string handling library. In fact, such an effort was started at AT&T back in 2001. The language was named Cyclone [1]. It met its 1.0 release in 2006. It has since been abandoned. Which is a damn shame, because it looked like it had real potential. Is C a primitive language? Yes. In fact, it's almost stupidly simple compared to most modern languages. But its greatest weakness also proves to be its greatest strength. The main cognitive overhead from using C is in its manual memory management model and certain gotchas in standard libraries. It being bare bones as it is (but not too much) also makes it surprisingly pleasant at times. I get the feeling that plenty of modern languages try too hard to be novel and, for all the freedom you get from not having to worry about anything below program logic itself, you still end up expending a lot of effort into complicated high-level patterns that are frequently interspersed with arcane PL theory. But that's not even the main reason why something like Cyclone is much needed. No. It's that we've pretty much perfected a ton of our APIs, like POSIX, to work with C. It simply feels most natural to work with POSIX in plain C. FFIs and bindings vary, some of them like Nim's actually feel quite clumsy (otherwise Nim makes it absolutely trivial to create FFIs, which is great). Finally, refactoring 30 years worth of code into Cyclone sounds far more realistic than rewriting the world in Rust. [1] https://en.wikipedia.org/wiki/Cyclone_%28programming_language%29 https://en.wikipedia.org/wiki/Cyclone_%28programming_languag...
- Scramblejams 12y agoYes, I too had high hopes for Cyclone when I saw it back then. Sad it seems to have stagnated.
- Araq 12y agoThanks for mentioning Nim! :-) I can't say I agree with anything you say though... So only memory safety is C's problem? Not its type system that's very weak in every aspect (not just memory safety)? Not its missing module system? Not its ambiguous syntax? You also confuse "simple" with "primitive" and perhaps "familiar". C surely is primitive, but how is it simple? Simple to use? No. Simple to implement? Not really. Has a simple mapping to the underlying hardware? That stopped being true three decades ago.
- andrewchambers 12y agoC is a great language, for fun I'm slowly working on my own highly C like langage - https://github.com/g-programming/glang https://github.com/g-programming/glang. Its basically C with Go like syntax, and a package system instead of headers. I aim to fully interop with existing C code and have no runtime requirements beyond what C has. I'm not trying to go crazy with extra features over what C has though. An important part of what will make it work, is perfect interop with existing C code. Yes I understand memory safety issues, but there is always a need for machine independent assembly languages. which C essentially is.
- p4bl0 12y agoYour project sounds very cool. Will it have the Go methods and interfaces system too?
- andrewchambers 12y agoI think its important only add those things after the language can run the same sorts of programs C can currently. For the future, some of my ideas include: a form of combined data types that require a type-switch to disambiguate (and is checked by the compiler), like algebraic data types. (essentially sugar over tagged unions) It may also have a form of defer blocks similar to Go defer statements. (sugar over the C idiom goto cleanup;) A final thing I considered was a built in reference counted pointer type. Just to partially alleviate memory management, but without resorting to a garbage collected run time.
- robmccoll 12y agoThis is really cool, but what about porting the go standard library to C instead? Will this make that easier? Does the ABI of g meet C's?
- andrewchambers 12y agoG will use llvm as its primary backend, and my intention follow the C ABI exactly so interop requires no work. I haven't considered the standard library much beyond the fact that I would G to have its own that is inspired by the good parts of other languages. At first it would have to rely on C libraries. G won't be able to support every way that the Go standard library works. But if you need everything Go has, then you probably want to use Go to begin with.
- Aldo_MX 12y agoEvery time I see someone praising C I curse Microsoft for not having proper support for C99 in Visual Studio.
- AlexeyBrin 12y agoVisual Studio 2013 has better support for C99, here is an article from MSDN about it http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-librar... . But it is not a complete C99 implementation. The good news is that with VS 2015 you will be able to use Clang for compiling C code, so hopefully a complete C99/C11 implementation on Windows at some point.
- pjmlp 12y agoMicrosoft reiterated at Connect 2014 that only the parts required by the C++ standard are to be included, as well as, smaller parts required by well known FOSS projects that they see value in supporting on Windows. Clang support is only planned as compile target for Android and eventually iOS. Check going native videos from Connect.
- AlexeyBrin 12y ago> Clang support is only planned as compile target for Android and eventually iOS. I know they plan to officially support only Android and iOS. However, even today I can use Clang to create Windows executables from the command line. Once the support for Clang will be included in VS, it is only a question of time until someone will swap the Clang from the Android NDK with a Clang compiled for Windows.
- Aldo_MX 12y ago> smaller parts required by well known FOSS projects that they see value in supporting on Windows Sadly, Microsoft don't see value in projects like FFmpeg, at least not to the point of making the compilation process with Visual Studio less painful.
- 12y ago
- dothebart 12y agoone of the best things C has to offer was omitted herein - the preprocessor ;-)
- 101914 12y ago"You wanted a banana but what you got was a gorilla holding the banana and the entire jungle." - Joe Armstrong One could argue, easily, this is true for many of the popular garbage collected languages. It is certainly why I do not care for them. It is also why I do not care for some of the popular OS's, or most of the popular "software solutions". It is the unwanted delivery of the kitchen sink when all I want is a glass of water. The OP says he's frustrated with C "because it seems to be trying (with limited success) to move to being a higher level language. There are times when I really do want a "thin layer over assembly", and C seems to have evolved to a place where that is no longer possible." "seems to have evolved" Some of my favorite programs that continue to work reliably, year after year, are written in what I guess some would call "primitive," K&R-like C and with little need for "the" standard libraries. I am not an expert C programmer, but I have no complaints when reading and editing this "primitive" C. I have seen others complain about it in forums. One of my favorite source code comments is in the netcat source where the author says he's going to write the program as if he's writing in assembly. In my mind C should be just a thin layer over assembly. Essentially, to me, it should be a kind of "shorthand" for writing assembly. (That implies the author should think as if he's writing assembly. And C just takes away the drudgery of actually typing out all those lines of assembly instructions.) Now, that is just my opinion. I am not an expert C programmer. I know nothing.
- Kenji 12y ago"One could argue, easily, this is true for many of the popular garbage collected languages [...] It is the unwanted delivery of the kitchen sink when all I want is a glass of water." Exactly. It's basically like forcing people to throw all their garbage on the ground and then forcing them to pay for the garbage men who collect said garbage. It would be easier for everyone to just go to a bin and dispose of the trash themselves.
- djur 12y agoThe problem is that there's a great deal of data structures that are difficult or impossible to write in a provably safe way without garbage collection (or reference counting). Pick up _Purely Functional Data Structures_ and take a crack at implementing them in Rust without unsafe{} and you'll see what I mean.
- Elrac 12y ago"C has the fastest development interactivity of any mainstream statically typed language." I wonder if this statement is true since the invention of Go. A very fast compile was one of the design goals of Go, and its authors could afford luxuries (esp. In terms of memory use and object file size) that were unthinkable in the days of C. For example, IIRC object files contain the compiled code of their source plus the top hierarchy of modules called. People have mentioned that changing a .h file in a large C project could trigger a lengthy recompile. AFAIK, Go minimizes this problem, so if the hype is correct there should be lots of projects where similar situations will be handled faster in Go. My own empirical experience is very limited. I can confirm that Go compiles much faster than Java for my code, but that will surprise no one and it doesn't help us compare with C. Does anybody have more tangible data regarding Go's alleged speed demon nature esp. as compared to C when compiled with e.g. VS or gcc?
- bkeroack 12y ago"I always have it in the back of my head that I want to make a slightly better C. Just to clean up some of the rough edges and fix some of the more egregious problems." This is called Go. Brought to you by the same people who gave you C.
- imanaccount247 12y agoNo it is not. Go is only "like C" in the sense of the most superficial syntactic similarities and that it is primitive. It isn't even in the same world as C, it is just another high level garbage collected language that compares poorly with the rest of the languages in that group. Go was not brought to us by the people who made C. It was brought to us by Rob Pike and Russ Cox. Dennis Ritchie gave us C.
- jbergens 12y agoKen Thomson was (and is) also involved with creating Go and he was also one of the writers of Unix. He actually invented the B language which preceeded C, so it was close :-) I think he wrote a lot of C in Unix and was therefore part of making C popular.
- imanaccount247 12y agoKen had nearly as little involvement in Go as he had in C, they just asked him stuff. Did he write any of it at all? "From the same people as C" is a factual claim, and it is factually incorrect. Ken absolutely did not bring us C, and it is very sketchy to claim he brought us go. Go is brought to us by the guy who came to bell labs after it was all over and decided to try to pass himself off as "the bell labs guy!" for the rest of eternity.
- myg204 12y agoJust putting this from the official FAQ, for the record. Robert Griesemer, Rob Pike and Ken Thompson started sketching the goals for a new language on the white board on September 21, 2007. Within a few days the goals had settled into a plan to do something and a fair idea of what it would be. Design continued part-time in parallel with unrelated work. By January 2008, Ken had started work on a compiler with which to explore ideas; it generated C code as its output. By mid-year the language had become a full-time project and had settled enough to attempt a production compiler.
- aosmith 12y agoC is a great language but have you tried go? Feels very c like with a lot of very nice conveniences.
- jeffreyrogers 12y agoGo is garbage collected though, and a lot of people using C don't want a GC'd language. (It's up for debate whether GC actually matters for most of them, but that's the feeling nonetheless). Edit: also, not sure why you were downvoted, since your question at least deserves a reasonable answer.
- Scramblejams 12y agoIt's because this place has turned into Reddit. :-(
- Dylan16807 12y agoI don't think tossing out the name of a popular alternative really contributes anything. I especially don't see what's 'reddit' about downvoting such a comment.
- Scramblejams 12y agoHis comment was one made in good faith and included a sincere suggestion. Responses to his comment can educate him, the participants and the lurkers, which is where the contribution appears. Just because a comment doesn't instantly add to the conversation doesn't mean that it won't, given the good-natured participation of others. Drive-by downvoting is what doesn't contribute anything in this case.
- Dylan16807 12y agoThe thing is, there wasn't really an insightful question either. Just "Go. Discuss." Perhaps if someone actually makes a detailed post on that subject then the post can be undownvoted. But at the moment there is no contribution, and the drive-by downvoting sorts it to the bottom, which is a good thing.
- arh68 12y ago(2013) First, I think the speed of C relies heavily on the compiler. C is not fast, not if I wrote the compiler. But GCC is very fast, and ICC at least as much. I was a little confused by the Erlang wariness, but the reasoning seemed sound > At Couchbase we recently spent easily 2+ man/months dealing with a crash in the Erlang VM. ... ... it was a race condition bug in core Erlang. We only found the problem via code inspection of Erlang. That does not sound like a fun time, but once the bug is fixed, what's the big deal? Is there good reason to believe more bugs will come? It wasn't my ass that got bitten but I'd forgive Linux even if it had a race condition bug. (or if it crashes touching 1000 files in /etc)
- andrewchambers 12y agoNo, C is fast, even tcc which does essentially no optimization except for constant folding is about half as fast as gcc -O1 I think. Basically no special work is needed to be 50 times as fast as python.
- ris 12y ago"No special work" except the massively more laborious effort of writing the whole thing in C.
- andrewchambers 12y agoI was referring to no special work on the part of the compiler writer, not the application writer.
- jerven 12y agoWhich python?
- andrewchambers 12y agoCPython 2.6 last I tried benchmarking tcc and python. Obviously pypy or pyston might have an edge for certain snippets after a long jit warmup.
- neutronicus 12y agoHmmm. I work on a pretty large scientific computing project written in C++. Complex numbers in C++ are much, much better than they are in C. In general, I'm very happy it isn't in C.
- panzi 12y agoAre you comparing C's built in complex type with a C++ library or a C++ library with a C library?
- neutronicus 12y agoIn both cases it's libraries defined by their respective standards, I think. The important thing is that the C++ class overloads operators and mathematical functions, and the C class doesn't.
- panzi 12y agoC99 has a builtin complex type. "double complex" (similar to "unsigned int"). Of course all arithmetic operators are defined for that type. Example: http://coliru.stacked-crooked.com/view?id=0d295064bd367eca http://coliru.stacked-crooked.com/view?id=0d295064bd367eca Source: http://en.cppreference.com/w/c/numeric/complex http://en.cppreference.com/w/c/numeric/complex Have you tried that? Of course Microsoft refuses to implement C99, so you could only try it on non-Microsoft C implementations.
- byuu 12y agoThis last bit is so true: > I always have it in the back of my head that I want to make a slightly better C. Just to clean up some of the rough edges and fix some of the more egregious problems. I'm always mulling over that. "Maybe just namespaces, better macros, function overloading and member functions (eg this->) would work." Yet I'm sure Bjarne Stroustrup was thinking the same thing when he started on "C with Classes." The power to add features must be so overwhelmingly corruptive. So justifiable in each and every case, in the moment. And then you just can't stop. "Closures would be helpful. I'd love to write generic containers. Inheritance would save so much typing.", and the next thing you know, you're working on virtual inheritance, concepts and user-defined literals. :/ In my opinion, C really does need just a bit more to be great, but it would need someone with a will of steel to stop and leave things be after that.
- andrewchambers 12y agoI agree totally, my top level comment in this thread has my version of what a great C replacement would be. I'm slowly working on it.
- byuu 12y agoLooks like a decent start, but not enough there to really judge it yet. Best of luck, I'll keep an eye on it! If you'd like feedback (ignore this post if not): Caseless switch is a great idea, especially no-fallthrough by default. Consider extending it with substituion, eg "case > 7:", "case >= 13 && < 16:", or "switch longvariablename as N { case N > 13 && N < 16:". Return types after function parameters are nice, consider saying "function foo(params) returns type" instead. Tuples always feel important but in practice I tend to not like them. Variable definitions are a bit clunky, and inconsistent with struct variable definitions. Consider ctors/dtors and/or inline member initialization. I would recommend not to compress keywords: func -> function, const -> constant, etc. "type name struct" would feel better and more consistent as "class name". The C. prefix feels like it'd be great if you only use a very small number of C APIs, and very annoying if you used a lot of them. The boldest and coolest part is building this directly on LLVM. My thought was always to transform the IL into C (trying my best to preserve line numbers and variable names), and then let GCC or Clang build that. My ideas were unified function call syntax (never any functions inside classes), subclasses (declare a class inside another class, but the child class has direct access to the parent class' scope), namespace-aware multi-line macros (trailing \ is grotesque), and if implementing a backend instead of a translator, zero undefined behaviors in the language (do what people expect on eg signed integer overflow instead of GCC doing whatever is fastest yet obviously wrong. Aim for hardware that is actually used today instead of worrying about hypothetical PDP-11 programs, so no 9-bit chars.)
- lordnacho 12y agoThe boring response would be that different languages have different strengths. C is indeed capable of being very fast, mainly by virtue of letting the dev have fine control over memory. But execution speed isn't the only metric. Sometimes you just need something to be done quickly and you know you aren't going to hit the constraints (time, space) of the system, so you might as well go with something like .NET to reduce your development time. The point about the tooling I'm not so sure about. There's a lot of languages out there with well developed tools. And the tools tend to address the pain points: you have GC profiling in .NET, and you have Valgrind-type tools in c++.
- imanaccount247 12y ago>Every time there is a claim of "near C" performance from a higher level language like Java or Haskell, it becomes a sick joke when you see the details. They have to do awkward backflips of syntax, use special knowledge of "smart" compilers and VM internals to get that performance, to the point that the simple expressive nature of the language is lost to strange optimizations that are version specific, and usually only stand up in micro-benchmarks. I see this kind of statement a lot, and it simply does not hold up to even a cursory look. Where are the awkward backflips of syntax, special compiler or VM knowledge, or version specific optimizations here? http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=mandelbrot&lang=java&lang2=gcc&data=u32 http://benchmarksgame.alioth.debian.org/u32/benchmark.php?te... http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=fasta&lang=java&lang2=gcc&data=u32 http://benchmarksgame.alioth.debian.org/u32/benchmark.php?te... Why are there so many java web frameworks/libraries/servers that offer performance close to C? http://www.techempower.com/benchmarks/#section=data-r9 http://www.techempower.com/benchmarks/#section=data-r9 >Critically important to developer efficiency and productivity is the "build, run, debug" cycle. This is a pretty bizarre claim. Sure, having an equally archaic and primitive process but 10 times slower is obviously worse (C++), but that doesn't mean having an archaic and primitive process is good if it is fast. I rarely ever run my code, much less debug it. Freeing myself from that horrible way of working was one of the greatest things about working in a language with a useful type system.
- applecore 12y agoWhat's horrible about running your own code?
- sliverstorm 12y agoI think he is saying in a round about way his code always works just right on the first compile, so he never even needs to run it before releasing it.
- Rapzid 12y agoI think he's saying he works on the code for long periods of time before worrying about how running it works out. Then he just tweaks the bits that are off and goes about his business.
- mrcslws 12y agoOne nitpick: > That some of the most heavily used and reliable software in the world is built on C is proof that the flaws are overblown, and easy to detect and fix. Easy to detect? Multiple times per year we find out about a security bug in Linux or Windows that has existed since the 90s.
- TwoBit 12y agoC is not the fastest language. C++ is faster than C. For example, the only way you can hope to match C++ inline template algorithms in C is with a horrific macro scheme.
- sharpneli 12y agohttp://benchmarksgame.alioth.debian.org/u64q/c.php http://benchmarksgame.alioth.debian.org/u64q/c.php Gives the indication that C++ is faster in practice only when your algorithms are so complex that writing them in C starts to become tedious. And frankly C++ can never match C until they introduce restrict. Only with C99 C was able to match Fortran. C++ does not have that luxury yet.
- TwoBit 12y agoWhat you say about restrict is hard to consider because every C++ compiler supports restrict. Also, I frequently see those types of algorithms that benefit C++. It's not just an STL thing.
- sharpneli 12y agoYeah it's true that restrict is well supported, mostly because all C++ compilers are also C compilers. Maybe the biggest reason why C++ is slower in practice is because the programmers tend to feel that using features unique to C++ almost mandatory. Even though technically the pure C program also compiles fine in C++.
- danenania 12y agoWhat's a good practical way to get my feet wet with C? I want to learn it, but I'm the sort of person that can only learn a language by actually trying to build something useful in it. Sometimes it seems like a language that everyone thinks is important to know, but that no one thinks should actually be used in a real world project unless you're working on embedded systems or building an OS. What are some ideal (and beginner-friendly) use cases?
- rubiquity 12y agoA lot of people tend to recommend Zed Shaw's "Learn C The Hard Way"[0] but I would love to hear what others have to say. 0 - http://c.learncodethehardway.org/book/ http://c.learncodethehardway.org/book/
- danenania 12y agoThanks, I'm actually going through Learn C The Hard Way now, but it's the sort of thing that doesn't really work for me. I just need to work on some cool little app idea that C is well suited for and learn/bang my head against the wall until I figure it out. The problem is that I can't seem to come up with anything small and practical to build that anyone actually thinks should be done in C. It's like there are beginner tutorials on one end, linux kernel hacking/database internals/NASA robots on the other end, and nothing in the middle. My experience with other languages and areas of interest are fairly wide, so it seems like I should be able to come up with something, but I'm feeling sort of at a loss.
- hlt 12y agoI think writing a C compiler in C is probably the most effective way of learning pure C, although probably not the most efficient. You don't even need to generate assembly code, you can just compile to C itself or some other language.
- rogerbraun 12y agoMaybe just reimplement some piece of existing software you wrote with a C version? I want to port a small node-based API to C in the future, just to see what it takes.
- plow 12y agoIt’s as if people make asymptotic progress towards the goal of the ‘ultimate programming language’, the newer innovations which may well have improvements are too marginal to be beneficial, there is too much existing inertia, that is why the more prominent modern ones incorporate interoperability with C often. It would seem more sensible to say as the author of this post seems to propose, that C is the de facto ultimate.
- otikik 12y agoRemember that where there's C, there can be Lua. Just saying.
- SCdF 12y agoThere is currently a really cool project I suggest (if you're interested in learning / seeing C in action) called Handmade Hero[1]. He[2] is making a game, from scratch, in (mostly) C (but with a little bit of C++ niceties thrown in), and streaming[3] the entire process for an hourish, Monday -> Friday. It's intended to be a tutorial in both learning C and learning from-scratch game development (where from scratch means using GDI and like, parsing your own wav files). It's really cool to watch, he's an entertaining guy, afaict is a pretty decent teacher (I know zero C, but C-style languages, and I _think_ I'm keeping up), so I recommend checking it out. [1] http://handmadehero.org/ http://handmadehero.org/ [2] https://twitter.com/cmuratori https://twitter.com/cmuratori [3] http://www.twitch.tv/handmade_hero http://www.twitch.tv/handmade_hero
- blt 12y agoC was built as a replacement for assembly programming. It seems low-level today but it does a huge amount of work for you. Call stack manipulation, computing offsets for nested struct members, converting arbitrarily complex conditionals into the right branching instructions, coalescing a sequence of expressively named intermediate values into the same machine register... these are the things C does for you. I've never written a serious assembly program but I can imagine how annoying it is to do all that stuff manually. It would be easy to screw up. I think C has been so successful because it made really good decisions about what to do for you. It eliminated a lot of those busy-work programming tasks without taking away your fundamental control of the machine. Automatic array bounds checking, garbage collection, ... they would have been unthinkable for the original goals of C. The stupid warts are annoying but I think C made fundamentally sound choices of what to do automatically and what to leave up to the programmer. We all talk about modular, abstract code, but back in the day "modular" meant "use the same calling convention for every int(int, int) function", not "dependency injection". From that perspective, C must have felt like a huge leap in modularity and abstraction from assembler.
- facorreia 12y ago> C is the fastest language out there. I thought it was Fortran.
- codygman 12y agoFortran is fastest for matrix manipulation and other maths stuff IIRC.
- Animats 12y agoSome of the unhappiness stems from C++, which was supposed to "fix C". Instead, it created a big mess. C++ is the only major language to support hiding ("abstraction") without memory safety. This turned out to be a poor feature combination, never again repeated. C has neither hiding nor memory safety, so if it's broken, at least it's visible at the user program level. Java/Pascal/Modula/Ada/Common LISP/Go/Rust, and the interpretive "scripting languages", all have both hiding and memory safety. We've at least come to an agreement on that feature set. Because of the existence of C++, mainstream C language development more or less stopped. ANSI C, ISO C, C99, and C11 don't differ by much. (At one time, I was promoting an approach to make C memory-safe in a way that allowed mixing safe and unsafe modules to allow a gradual transition and rewriting of old code. See (http://animats.com/papers/languages/safearraysforc43.pdf http://animats.com/papers/languages/safearraysforc43.pdf). It's technically feasible but politically hopeless. I'm now pinning my hopes on Rust and hoping they don't screw up.)
- demarq 12y agocall us when the honeymoon is over.
- fithisux 12y agoIn this thread I saw no mention of Freebasic. It has some good features and is comparable to C. TerraLang is another interesting approach (for system programming languages). But the fundamental flaw is that in many cases a DSl that compiles to something lower level is the way to Go.
- yason 12y agoThe reason I love C——I wouldn't like to love it but I am dragged down back to C each time so I think I've just given up——is that it costs a bit more to write. I used to prototype things with Python by the method of sheer experimentation. When I found a suitable design, I'd rewrite it "properly" in Python and if that wasn't fast enough I'd think a bit and rewrite it in C. But that's happening less and less often these days. Because C costs a bit more to write it makes me think ahead. And I've (once again) found that while prototyping in Python is fast it's even faster to think and then just write C. I write some boilerplate C and start adding the data structures... this could take a "long" time but once I've figured it out, writing the actual C code is a breeze. All the mental work has already been done and once the data structures are right, the implementation just follows. Curiously, this doesn't exactly happen with Python. Because you can make a Python program run almost immediately, there's nothing forcing you to the productive idleness. In C, you'll have to write a bit of code first until you can get something meaningful up and running: this is the hatching time for what you really want to write. I'm not sure why I haven't observed this with other languages. I suppose I would need to write hefty amounts of Java until I could get a program running somewhat, but this process of forced productive idleness mostly happens in C. Maybe it's because there are no layer beneath C and assembly and you intuitively know the tradeoffs and only a negligible delta of what you write and what the machine will actually do. Maybe it's because C feels so physical. I don't know. But so far C is the only language that gives me that process in a native, intuitive way, constantly telling me that "you must not rush, because you can't rush anyway".