5 ms·
> Not when you take the arcane undefined behavior rules into account. C semantics are anything but simple. Maybe, but note that simplicity should be viewed in
by gajjanag 10y ago
> Not when you take the arcane undefined behavior rules into account. C semantics are anything but simple.
Maybe, but note that simplicity should be viewed in a relative sense.
I am curious as to which language you think has simpler semantics than C. For example, I have not found Python, Julia, MATLAB, C++, Verilog, or shell script simpler than C. Same goes for my initial explorations of Rust.
Even if one includes the arcane corners, the spec is < 200 pages (excluding the stdlib).
- pcwalton 10y agoIf you don't count the unsafe sublanguage, Rust, SML, OCaml, Scheme, Java, and Lua all have simpler semantics than C, and that's just off the top of my head. It's debatable, but I would even argue that Haskell and Swift do. Undefined behavior is really subtle, and there are parts of the spec that compiler developers haven't even come to consensus regarding the meaning of.
- tikhonj 10y agoStandard Haskell (ie Haskell 2010[1]) certainly is. Most of its features are just syntax sugar over a tiny core language with simple (although not 100% formalized) denotational semantics. The language itself isn't very big and it's pretty well-specified. GHC Haskell with all extensions... euh, I don't know. Many extensions are either syntactic sugar or straightforward changes to the base language, sometimes even making it simpler. But I'm not confident about all of them, and I'm not sure how the semantics of some low-level libraries (ie for concurrency) work out. I've written code in essentially just the standard subset and it's a pleasant language—there aren't any extensions that you absolutely must use in real projects. That said, people use lots of them anyhow because most make the language nicer without adding too much complexity. Since the extensions aren't organized in a unified way and have to be split up (since they can generally be enabled or disabled independently), it's hard to figure out how complex they make the semantics of the language as a whole. [1]: https://www.haskell.org/onlinereport/haskell2010/ https://www.haskell.org/onlinereport/haskell2010/
- gajjanag 10y agoI have not implemented a compiler so I can't really judge on that aspect. Isn't it also true that undefined behavior can stem from varying hardware/platform considerations? In particular, leaving some things judiciously undefined allows flexibility in implementations and thus can give good performance across a variety of platforms. Sure, one could force some precise semantics, but that might result in unnecessary "emulation" code on some platforms. So at a programming language level, by having such emulation/fixed behavior, the semantics are simpler. But at a low level, the semantics are more complicated as the machine instructions may differ significantly, thus losing transparency. This is a concern in quite a few resource constrained applications. For a simple example, see http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-... and the section on oversized shift amounts.
- nickpsecurity 10y agoIts main competition to start with: http://modula-2.info/m2info/ http://modula-2.info/m2info/ Then, Modula-3 was the C++ alternative and its semantics are probably still easier than C due to simplicity plus well-specified. https://en.wikipedia.org/wiki/Modula-3 https://en.wikipedia.org/wiki/Modula-3
- gajjanag 10y agoI like your answer, but I was really referring to widely used languages.
- nickpsecurity 10y agoMaybe Fortran, then, given less undefined behavior. It's still widely used in high-performance computing given it's easier to optimize than C and has pre-optimized libraries.
- pnathan 10y agoIt's worth studying a bit on static analysis tools for C. C's lack of adequate specification and undefined/unspecified cases make it a shambling mess to have truly correct C, particularly multiplatform or in unusual CPU environments. C++ does not improve this.
- nwmcsween 10y agoSome of the UB is for 'unusual' CPU's that existed long ago.