6 ms·
Divided by a common syntax
- umanwizard 10y agoTangent, but why is it inaccurate that C is described as "portable assembler" ? That's a bit of an exaggeration, at worst -- yes, C is a bit more high-level than a typical assembly language, but most C constructs can be mapped to a typical processor's assembly language in an obvious way, something that isn't true of any other language I know of.
- 21 10y agoMaybe because by the time the compiler is finished optimizing the code it doesn't match the source code anymore line by line, but only in the intended effect of execution.
- pdw 10y agoRight. There might be an obvious mapping for any C construct, but there's absolutely no guarantee that a C compiler will do the obvious thing. The C standard leaves much behavior undefined (wolfgke listed some of the more notorious items), and modern compilers exploit that. People who view C as "portable assembler" end up being very disappointed.
- wolfgke 10y ago> There might be an obvious mapping for any C construct I don't know much about the PDP-11, the machine for which C was originally designed, but for modern processors this "obvious" mapping is typically not how things are done in assembly. To give an example: int x = 42; x = x+1; foo(&x); The way how it was originally conceived in C was that we reserve, say, 4 bytes on the stack and pass the adress of x on the stack to foo. On the other hand, any assembler programmer would try to store integer variables in a processor register (where we can't take an adress) and synchronize it with the x on the stack before we pass x's adress to foo. In other words: x now is now "two variables": 4 bytes on the stack and a register, which are synced if necessary. Summarizing: In assembly we have "two kinds of local variables": - Registers [one cannot take adress of it] - Adresses in stack frame [on RISC cores (load-store architecture) these can only be accesses by an explicit load/store and must be loaded into register before anything useful can be done with them; on x86-32/x86-64 the story is a little more complicated - I don't want to go into the details here] As I have shown these two completely different concepts are intertwined in C into the conept of local variables. I hope this convinces you how the concepts in C don't map directly to modern assembly code (i.e. a direct mapping yields bad assembly code). Ironically C has the concept of "register" variables, of which one cannot take the adress. This feature is hardly used in modern C, but maps IMHO much better to the "register" concept in assembly (but I'm not a C expert).
- wolfgke 10y ago> Tangent, but why is it inaccurate that C is described as "portable assembler" ? - Strict aliasing rules (most important reason why C should not be considered as "portable assembler") - Overflow for adding/subtracting signed numbers is undefined behavior - Implicit casting of integer types in expressions; OK, there are rules for this, but under assembly you have all this under direct control without having to learn lots of arcane rules - There are things that can easily be done in assmbly that cannot/can hardly be done in C (for example taking adress of a label (very useful for implementing coroutines) is only an GCC extension but not standard C: http://stackoverflow.com/q/1777990/497193 http://stackoverflow.com/q/1777990/497193). Or how do you control the amount of stack space that a function is allowed to use (would be very useful for embedded applications where you strictly want to avoid stack overflow and you want to allocate exactly as much stack space as necessary, but not more)
- junke 10y agoC was very close to the metal in the earliest versions (PDP-11). Since then, CPU architectures evolved and even though the language evolved too, C compilers somehow have more work to do to map the programmer's intent to the actual hardware.
- groovy2shoes 10y ago"C is a portable assembler for the PDP-11."
- nwatson 10y agoAny C programmer working at large scale will need to rely on third-party libraries. That library code is an abstraction. When I write C++ the language magic is "just another library". If I'm uncomfortable I just step through those magic layers in a debugger and I'm then happy, just like a C library user would step through compiled-from-source third party stuff. "But a C coder can read the third-party code!" But a C program already hides the magic of function calls and stack frames ... C++ is just a few more layers of abstractions just like that one!
- wolfgke 10y ago> When I write C++ the language magic is "just another library". The difference is: For a library you can (in principle) write your own implementation and replace the given one by it. Or even remove the library completely if it seems a good idea. For language features this is not possible (without writing a new compiler that implements something different than C/C++). Thus the language features of C++ clearly cannot be compared to a library - they change the language (if left away), while a library is no different language, but a text written in the language.
- lmm 10y ago> But a C program already hides the magic of function calls and stack frames ... C++ is just a few more layers of abstractions just like that one! In principle yes, but there are good and bad abstractions (bitshifts that print probably being in the latter category), and facilities that break important symmetries of their surrounding code (exceptions).
- gpderetta 10y agoExactly, I mean, which language designer would reuse the the very common stream input/output operators for such an uncommon bit manipulation operation! Slightly less tongue-in-cheek, what sort of symmetries are broken by exceptions?
- lmm 10y ago> Slightly less tongue-in-cheek, what sort of symmetries are broken by exceptions? They make it much harder to maintain single entry/exit in functions where you want that - and, crucially, harder to notice when a function isn't single entry/exit. They also violate referential transparency, which means e.g. caching the result of a repeated function call is no longer necessarily safe.
- userbinator 10y agoIn my experience, the C++ programmers I've worked with did tend to have a bit of an obsession with abstraction. They're far more likely to build complex systems to solve simple problems. I suspect it's a combination of OOP hype (not as prevalent today, but still noticeable) and all the "magic" features that attract such programmers, as it gives them enjoyment to work with complex things. On the other hand, C doesn't seem to attract the same type of programmers because the language itself is relatively spartan. I have a theory that simpler languages naturally encourage programmers to find simpler solutions both for themselves and the machine (thus possibly being more efficient), because more complex ones would involve more work. Asm is an extreme example, where you have to basically write every instruction the machine will execute --- and in such a scenario, even if it's mostly library calls, you're not going to want to construct and destruct a dozen objects unless you absolutely, unconditionally need to.
- deleted 10y ago[deleted]
- frozenport 10y agoI feel this is a criticism of the past. In the last decade a lot of places were abstraction was required (threads, etc), we've seen good libraries appear. This has greatly simplified code bases, especially if you measure by lines of code.
- cgvgffyv 10y agoC++? Preposterous. AbstractSingletonProxyFactoryBean QED.
- deleted 10y ago[deleted]
- paulddraper 10y ago> simpler languages naturally encourage programmers to find simpler solutions both for themselves and the machine And simpler solutions for their users. Maybe a good thing; maybe not. But it's unlikely you will see a modern web browser in C.
- svisser 10y ago"Divided by a common syntax" also describes the relationship between Python 2 and Python 3.
- themckman 10y agoI was kind of hoping it would be an article about all the Lisps and Schemes.
- GFK_of_xmaspast 10y agoThat would bring out of the woodwork all the people who disagree that the lisps and schemes have a common syntax as well as all the people who disagree that lisps have a syntax at all.
- TazeTSchnitzel 10y agoAs a C programmer, I think I find C++'s dizzying abstractions scary because C, and thus C++, is an unsafe language. In C, I have to maintain careful discipline in memory management. In C++, the same operators used for this in C will now sometimes do this for you, except when they don't. And so I look at C++ code and I have no idea what it's doing, and I don't know if it's safe. In Python or whatever I needn't worry because there aren't any actual pointers or heap corruptions or segfaults. Mind you, I have very little experience writing C++ code, so maybe I'd get used to it.
- clappski 10y agoI think nowadays it's a bit of a misconception that C++ is unsafe regarding memory; RAII essentially takes care of it for you when you talking about IO or other resources, and under the circumstances that you really need a pointer you can either use a reference or a smart pointer, which essentially just delegates the management of RAII to the smart pointer object.
- TazeTSchnitzel 10y agoSo it's safe so long as you're using the safety features. So, unsafe.
- pjmlp 10y agoC++ is a car with safety belts. It is up to the person to buckle it, or not. C is a car made in the days when having a safety belt wasn't even a concept, so there is no option to buckle it.
- Spivak 10y agoMaybe a better metaphor would be C is a car with a knife pointing out of the steering wheel. The fact that it's dangerous makes the person drive safer and more cautiously.
- 10y ago
- donatj 10y agoI know HN gets a lot of crap for having a ton of Go fanboys but the reasons given here for liking C are the reasons I am in love with Go. No magic, no overloading, very clear when I do this this happens, and very easy to read code including in the standard library. It's a more friendly C.
- shakna 10y agoI want to like Go. But its insistence on $GOPATH is infuriating. I have many, many projects in dozens of languages, organised in a way that reflects both my development and deployment practices. But Go wants to be different. And it wants to be separate. It's a tiny knitpick, and there is much I like about Go, even the way it handles exceptions. But it inconveniences me. And so, I guess I'll pout.
- patates 10y agoI just set the $GOPATH per project and it stops being a big problem, and turns into just an inconvenience. If you have a ProjectX with the path $pathx, set $GOTPATH to $pathx and place your source files in $pathx/src/projectx I also hate that I have to do this though and I totally understand what you mean. Just wanted to mention my favorite work-around to see if I get some feedback on it being stupid/clever/ordinary.
- shakna 10y agoIt's what I've been recommended, but it really seems like a terrible hack that shouldn't have to exist.
- laumars 10y agoIt's not really any different to supplying the source paths in C / C++ build scripts. Or setting the Java variables in your preferred IDE. At the of the day the compiler isn't going to know where to import external source files from if you don't specify it somewhere. The GOPATH envvar just shortcuts the need to use build flags but if you want to do away with system wide Go variables then you can always write build scripts more akin to those we see in some other languages.
- dap 10y ago> A C++ programmer, meanwhile, comes at things from a different perspective: code written by humans is buggy; [1] instead, as much as possible of the complexity should be shuffled to the compiler. As though compilers aren't themselves written by humans. When abstractions move into the language itself, it's harder to observe what's going on when they do something strange (even if it's correct) than if they were just some other library code in the same program.
- mbfg 10y agoI generally agree with the article's main point, however c still has macros which kind of breaks those rules. I remember back in 98 when first picking up Java, thinking, WHAT NO MACROS?, but now i believe that was an incredibly good idea not having them. Of course Java now has things like aspects, et. al, so you never can get away from things if you really want them.
- user5994461 10y ago> A C programmer might perhaps uncharitaby suggest that the number of bugs might be reduced by using a less complicated langauge, of course. The only thing to know about C and C++. Don't use any of them unless there is no choice.