11 ms·
I still like C and strongly dislike C++
- jonny_eh 5y agoWorking link: https://web.archive.org/web/20210526230420/https://codecs.multimedia.cx/2021/05/why-i-still-like-c-and-strongly-dislike-cpp/ https://web.archive.org/web/20210526230420/https://codecs.mu...
- FpUser 5y agoBrowser times out on article. Here is my take. I like C when I work on firmware for less powerful microcontrollers. I like modern C++ when I work on backend servers / middleware. I like Delphi / Lazarus when I work on desktop software. I like Java Script when writing web front-end libraries / apps. I like Python when shell script gets a bit too complex. Etc. Etc. The real truth - I do not really like any of those. They're just practical tools that help me build my product. Product designed and created by me is what I like.
- whatshisface 5y agoI found a buffer overrun bug causing incorrect results in a C dependency recently. I don't like C anymore.
- sigjuice 5y agoYou are lucky if you only encountered this recently. We might not like C, but C is not going away anywhere.
- ncmncm 5y agoC is, in fact, going away, in many, many places. E.g. Gcc and Gdb are nowadays compiled with G++, and new code in them is C++. It is very easy to start this process in just about any C program, so it happens many times every day, all over the world, with little notice because the bump is unfelt.
- sigjuice 5y agoThat does not count as “going away”. Sure, newer functionality may be done in C++, but it is unlikely that all C code will disappear from the gcc and gdb codebases. The older versions of things written in C will still be around and remain in production.
- ncmncm 5y agoThe production of new C code is going away in many places that have a lot of old C code. In each project that converts to compiling with a C++ compiler, the new code is C++. Old C code hangs about, but is looked at less and less; soon, nobody wants to look at any of it. Parts get wholly rewritten in C++ when it seems like they might otherwise need much attention. If it is meaningful at all to talk about a language going away, it is about a decline in writing new code in it. Obviously all the code written in badly obsolete languages still exists, somewhere, but the demand for people to write any more of it falls at an increasing rate.
- Koshkin 5y agoWell, all languages have good and bad parts. C and (the modern) C++ are two completely different languages in what matters most, and, to be fair, it is C++ in which much of the world-class software, like Unreal Engine, is written in. As much as I love C, if faced with a large project, I would only choose C++ as the implementation language.
- munk-a 5y agoAt my first real dev job a big portion of my work heavily involved the Qt framework and, while at the time lambdas were still a big missing sore point, using that framework and their widespread habit of passing by const ref instead of pointer made me like C++ a lot more. I completely understand that pointers and dynamic memory management exists for really good reasons, but isolating global object creation into your main and leaning on internally managed lists of objects can be really strong from the perspective of writing good testable code. I've been working primarily in PHP for a while now (and honestly put a lot less value on language comparison) but from what I've seen the expressiveness and power of C++ (at least in the ways I like it) have mostly been superseded by Rust and I think that'd be where I started if I ever need to work on something where in application logic would actually be a product bottleneck (instead of data source interaction).
- 12thwonder 5y agoRight, Browsers too are written in c++ for the most part
- jrsj 5y agoIs Unreal Engine world-class software? I've heard a lot of negative things about it so it's hard to tell if it's just one of those things people complain about because its so popular or if it really does get things wrong
- AstralStorm 5y agoBad things compared to what? Unity which does not have half the features out of the box and every other game implements basics like FPS limiter wrong? Frostbite or Dawn Engine with its bad editors and performance problems? Crytek used by few games? (This one has a chance to challenge UE) Unigine which is also rather rare?
- jdashg 5y agoC is not a "portable assembly language" even if you really, really want it to be.
- teawrecks 5y agoC++ is?
- Jorengarenar 5y agoIt is not even more
- oconnor663 5y agoI like the arguments here: https://blog.regehr.org/archives/1520 https://blog.regehr.org/archives/1520 > Avoid repeating tired maxims like “C is a portable assembly language” and “trust the programmer.” Unfortunately, C and C++ are mostly taught the old way, as if programming in them isn’t like walking in a minefield.
- FranchuFranchu 5y agoThe way most C compilers and ABIs use the stack kills it imo. But you could tweak a compiler to change that.
- TchoBeer 5y agoWhy not?
- TheOtherHobbes 5y agoBecause there's no such thing as down-to-the-metal portable assembly.
- habibur 5y agoLook at C++ as a scripting language and you possibly will not dislike it as much.
- rahen 5y agoCERN actually does that with CLING for interactive HEP data science. https://root.cern/cling/ https://root.cern/cling/
- sigjuice 5y agoThanks! $ brew install cling $ cling ****************** CLING ****************** * Type C++ code and press enter to run it * * Type .q to exit * ******************************************* [cling]$ #include <stdio.h> [cling]$ #include <sys/utsname.h> [cling]$ struct utsname u; [cling]$ uname(&u); [cling]$ printf("%s %s %s\n", u.sysname, u.release, u.machine); Darwin 20.5.0 arm64
- rahen 5y agoYou can even use it interactively like ipython and get rid of the printf, as it automatically evaluates expressions. [cling]$ int i=21 (int) 21 [cling]$ i*2 (int) 42
- sigjuice 5y agoThanks! Is there a way to see strings nicely without printf? [cling]$ u.sysname (char [256]) "Darwin\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
- 5y ago
- tmccrary55 5y agoThey're both a huge pain in the ass. But it's easier to scratch C than C++.
- stephc_int13 5y agoSame. I should finish writing a big article title "Why I still don't like C++" for my yet unpublished blog... Even if I tend to use orthodox C++ for convenience. But I've been impressed lately by some newcomers, like Zig.
- oconnor663 5y ago> The reason why I still like C is that it is a simple language. Simple in a sense that it is easy to express ideas and what to expect from it. For example, if you want to retrieve an array value with two offsets, one of which can be negative, in C you write arr[off1 + off2] while in Rust it would be arr[((off1 as isize) + off2) as usize]. There's an argument to be made that C is a simple language compared to some, I'll grant that. But implicit casts and the integer promotion rules really aren't a good example of simplicity. Terseness, sure, but not simplicity. For example, from https://www.cryptologie.net/article/418/integer-promotion-in-c https://www.cryptologie.net/article/418/integer-promotion-in... uint8_t start = -1; printf("%x\n", start); // prints 0xff uint64_t result = start << 24; printf("%llx\n", result); // should print 00000000ff000000, but will print ffffffffff000000 result = (uint64_t)start << 24; printf("%llx\n", result); // prints 00000000ff000000 There are good reasons why e.g. Go, which prioritizes simplicity very highly, does not allow implicit integer casts.
- ohazi 5y agoBut if the comparison is with C++, then they would both rank equally poorly here, because C++ doesn't want to behave differently. If you write code like that in C frequently (e.g. working with system registers on embedded CPUs), you do eventually get used to explicitly casting defensively, and I agree that it's nice that you get saner defaults in e.g. Rust.
- gameswithgo 5y agoYeah implicit casts are nice when working on a toy or personal project, but if you are putting code on a rocket or a game server or a self driving car etc, etc, absolutely not.
- astrange 5y agoThis blog is by an ffmpeg developer and in my opinion you'd never have ffmpeg with that rule because everyone would have gotten bored of all the typing and stopped working on it.
- 5y ago
- tapirl 5y agoC syntax is overall simple. The only syntax often confuses me is function typedef.
- jimbob45 5y agoAnd ostensibly function pointers/function pointer calling.
- benburleson 5y agoOne of the best parts!
- Koshkin 5y agoWhy? Essentially, a typedef only differs from a variable declaration by, well, having the word ‘typedef’ in front of it, functions or function pointers being no exception.
- tapirl 5y agoSorry, I should be more accurately. It is function type uses. In fact, I'm not confused with simple function types. It is just that sometimes, complex function types are convolutions for me.
- yakubin 5y agoC syntax isn't simple. Unlike most modern languages, which have a context-free grammar, it has a context-sensitive grammar. You can see it in the following example: #define a stdio #define header <a.h> // five tokens: <, a, ., h, > (otherwise preprocessor wouldn't substitue "a") #include header // one token after substitution: <stdio.h> int main() { struct { int h; } stdio; a.h = 0; printf("%d\n", stdio.h); } What happened here is: 1. In #include directives a thing like <stdio.h> is one token; everywhere else (including other preprocessor directives) it is five tokens; 2. But an #include directive can have multiple tokens on the right hand side of "#include" before macro substitution. After macro substitution the strings are lexed again - into ONE token. 3. In main() "a.h" is substituted with "stdio.h" - this time it's THREE tokens. (Oh, and writing "#include <a.h>" will produce an error, because here we have a single token <a.h>, so no "a" to be substituted, and normally there is no "a.h" header file in include path.) So the same string gets lexed into a different number of tokens in different places; and that happens even after it got lexed first into the same number of tokens before macro substitution. Now have an example of a C file that compiles with GCC, but not with Clang: #define CMPS x /* */ <y> #define ALMOST_SHIFT<CMPS #define FWD_FST(x, y) x #define CONCAT(x, y) x ## y #define x # /* LOL */ define y stdio.h #include CMPS #undef x #undef y #include FWD_FST(<stdint.h>, y) // btw. comma is a valid character in a header name. but this will work anyway, // because lexing in C works in mysterious ways #include CONCAT(<std,def.h>) int main() { int x = 0; int y = 0; int z = 0; printf("%d\n", CMPS z); // printf("%d\n", z<ALMOST_SHIFT); // doesn't compile // but the rest works fine... } So clearly the C syntax isn't simple. It's so not simple, that compiler writers can't agree on what exactly it is. (I've been writing such programs for the purpose of using them as edgecases to test my C compiler.) Then of course you have the type declaration fiasco: <https://blog.golang.org/declaration-syntax https://blog.golang.org/declaration-syntax> Then there if of course the awful ternary operator. Oh, and I almost forgot: spaces are insignificant... unless you're in a "#define" directive. Talk about language consistency... Those things not only make life hard for compiler writers, but also for language users, because, I don't know about you, but when I write a program the smallest unit that I use to think about its source code is the token. When I can't easily predict tokenization, then something went really wrong. Same with other syntax complications. Guy Steele once said that when he designs a language he verifies that its grammar is LL(1) with a parser generator, which helps make sure that it's not only easy to write the parser and that parsing can be very efficient, but also that it's easy to understand by humans. C fails here big time.
- deleted 5y ago[deleted]
- bsder 5y agoI've actually gone backwards from my original positions. I used to think that using C++ was acceptable in some cases. I no longer do. The faults isn't even C++ the language, per se. It's the ecosystem--or more appropriately--the lack thereof. A lot of the modern languages integrate with C, properly. They integrate on Linux, Windows, OS X/macOS, iOS, Android, etc. Nothing integrates with C++ well, and it's the fault on the C++ side. That means dumping C++. Perhaps the C++ compiler writers will finally have some incentive to fix their crap.
- tovej 5y agoThe C compiler people and the C++ compiler people are the same people.
- zabzonk 5y agoIf you can integrate something with C, you can do the same with C++.
- Koshkin 5y agoThis is actually a pretty deep idea, not to rely on a single language for implementation. I would go as far as suggesting to use as many DSLs as possible, to make sure that each component or level of abstraction is adequately expressed.
- binarynate 5y ago> That means dumping C++. Or just integrate with C++ code by exposing it through C linkage with `extern "C" {}`. Sure, C is the de facto lingua franca of programming languages, but that doesn't seem like a pragmatic reason to choose C over a different language that may be better for the job. Also, C++ has a massive ecosystem.
- bsder 5y ago> Or just integrate with C++ code by exposing it through C linkage with `extern "C" {}`. Nope. Been there. Done that. Got the scars. For example, what happens when an exception gets thrown on the other side of that "extern C"? Yeah, undefined. The problem is that if you're using C++, you get the C++ machinery--allocators, exceptions, etc.--and you can't dodge it.
- bobbyi_settv 5y agoMicrosoft's compiler did not support C99 for a long time because they did not really think of it as a C compiler or a C/ C++ compiler; it is a C++ compiler. Its support for C90 was explained as being maintained "for historical reasons" ( https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/ https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an... ), but they had no reason to add support for new standards of a language that was outside the scope of the project. It's odd for the author to complain about the commingling of C and C++ in compilers and then complain about Microsoft specifically not doing that.
- pjmlp 5y agoI think they only moved away from that path due to customer pressure to keep supporting C on Windows. That is the official excuse to only support C on Azure Sphere, despite the whole security sales pitch.
- flohofwoe 5y agoThe MSVC compiler actually has distinct C and C++ compilers (or at least compilation modes). In C++ mode, no C99 is accepted. What's different from clang and gcc is that the MSVC C++ compiler doesn't accept any "modern C" code, while clang and gcc have C++ language extensions for this (for instance clang accepts the full C99 designated initialization feature set in C++).
- crazypython 5y agoDlang is nice. It's essentially C++, but with the warts fixed. Using dpp[0], you can #include C headers into D, and use their complex macros. [0]: https://code.dlang.org/packages/dpp https://code.dlang.org/packages/dpp
- aninteger 5y agoThey didn't fix all the warts, D's compile times are just as bad or maybe even worse [0][1]. [0]: https://blog.thecybershadow.net/2018/11/18/d-compilation-is-too-slow-and-i-am-forking-the-compiler/ https://blog.thecybershadow.net/2018/11/18/d-compilation-is-... [1]: https://forum.dlang.org/thread/pvseqkfkgaopsnhqecdb@forum.dlang.org https://forum.dlang.org/thread/pvseqkfkgaopsnhqecdb@forum.dl...
- pjmlp 5y agoUnfortunely D is full of warts, as the community is small and they keep changing their mind what D is supposed to be, while C#, C++, Java, Rust, Swift adopt the features that made D special 10 years ago.
- pretendgeneer 5y agoI want to like C more, it's simpleness is great until you need to do anything at compiletime then the macro's blow any C++ complexity out of the water. It's why I like zig so much, It really is the language that understands what was wrong with C and fix's those parts and doesn't do too much more. Macros suck -> comptime is just zig code Pointer or array is ambiguous most of the time -> array and ptr notation Error handling things with a lot of possible errors leads to goto's or messy clean up -> errdefer and defer make that clean.
- mathgenius 5y agoHow often have I heard of people running into C++ compiler bugs. Oh the horror. This is what originally bit me in the 90s, and I never went back to C++ again.
- zabzonk 5y agoBecause only C++ compilers have bugs, of course.
- amatecha 5y agoArchive link since the site appears to be down (too much traffic I guess) https://archive.is/E3GvJ https://archive.is/E3GvJ
- omoikane 5y ago> would e.g. C++24 as a separate language based in C++21 with most of outdated stuff thrown out be as popular? This seem to be arguing that C++ is accumulating cruft as it grows and it's bad, but I like the fact that C++ tries hard to maintain backward compatibility. The greatest asset of a language is all the stuff that is already written in it. That said, some outdated stuff do get thrown out, such as trigraphs.
- nynx 5y agoC is one of those languages that consistently looks fast, but often is slower than other languages. Without generics, it's difficult to have complex data structures for each type, so you end up with lots of pointer chasing and less than optimal data structures.
- typon 5y agoI want a C with templates (with support for C++20 concepts) and consteval, instead of a C with classes (C++) I don't care about RAII, so I don't care about the ownership tracking that Rust provides. I like to think that memory management is actually part of the problem, rather than a responsibility for the language's runtime. I want to generate hundreds of datastructures or algorithms at compile time that are optimal in specific situations using introspection of types. I want everything else in the language to get out of the way and know that I'm writing code that actually runs on a real computer, not an abstract machine.
- cmrdporcupine 5y agoSounds like you might like Zig.
- typon 5y agoNever gave it an honest look, maybe I should spend some time learning it
- AstralStorm 5y agoAny argument that starts with "I don't care about safety" is a bad idea. We could use more safety even, like automatic race condition analysis in language and more. Conservative extensions of C and C++ exist, and they're not very popular, just check how popular D is...
- mhh__ 5y agoD is that language, especially wrt to the code generation. Code generation and metaprogramming is what we obsess over.
- 29athrowaway 5y agoNobody knows C++. Nobody can know C++. There are situations that you will not interpret correctly no matter how much time you spend learning the language.
- AstralStorm 5y agoSame for C, with its tokenization and undefined or implementation defined behavior. It's a serious flaw in the language allowed because it makes writing optimizers and compilers easier.
- pjmlp 5y agoSame applies to any programing language that gets widespread industry adoption, with several implementation and years of production code, including C.
- jollybean 5y agoThis isn't true. Some languages are more complex than others and C++ is the Queen. I've never run into a bug in Java wherein a look at the compiler or runtim error, runtime stack and some look at StackExchange I couldn't solve. But I'm constantly fighting CMake, linker issues etc.. I avoid huge swaths of C++ because I consider them to risky to deploy without mastery.
- pjmlp 5y agoI doubt very seriosuly that you would surive a Pub Quiz of Java Puzzles that includes the Java language and standard library up to release 17. For bonus points we can include the Java implementations outside OpenJDK.
- jollybean 5y agoYou're right. But I would learn any new Java thing in a few minutes because it's explained and documented. I'm having difficulty compiling a Qt app right now due to complicated and arcane marco directives. The compiler / linker give me no information to work on, and there's no documentation. It takes 2x as long to develop in C++ it's grinding.
- known 5y ago"C++ solves _none_ of the C issues, and only makes things worse. It really is a crap language" --Linus Torvalds
- gattis 5y agoI used to feel the same. For a long time I got away with using C and python for everything. They paired well and I always came away feeling that my code was expressed elegantly enough for me. C was for stuff that had to be fast, python for things that were more functional or meta. Then I started a project that needed to be faster than python but also required some heavy-ish meta-programming. I wrangled for way too long with the C preprocessor, and while way more powerful than I had imagined, still fell well short of being able to do what I needed without it looking and feeling like a dog that had to be put down. I think I would have needed a code-generating script to do it and to me that sounds disgusting. But C++ templates did exactly what I needed. They look awful and I still don't even understand my own code when I try to go back and read it. But it made me understand and appreciate that C++ does have a place, and for certain projects it might actually be the best choice. I'm sure some fan boys of the newer C++ descendents (rust/golang/zig/et al) will disagree.
- Glavnokoman 5y agoType parametrization is actually done better in Zig. I wish it departed from "no implicit actions" dogma and let RAII in. It would be almost perfect...
- erdeszt 5y agoI kinda like that `defer` is very visible. Although I haven't worked on large Zig codebases so I don't know how that scales.
- Glavnokoman 5y ago`defer` does not allow passing ownership so it limits your designs quite significantly.
- LeCow 5y agoI prefer Rust simply because it's safer and C++ has become monolithic. Also gotta love an excellent package manager.
- pjmlp 5y ago> It is not safe e.g. out of bounds array access is rather common and there’s no runtime check for that while e.g. Borland Pascal let alone something more modern had it (even if you could turn it off in compilation options for better performance ALGOL dialects for systems programming had it 10 years before C was born. CPL the language that BCPL subset was designed as bootstrapping compiler, had it. PL/I the language used on Multics had it. PL.8, language created by IBM for their RISC project and LLVM like compiler toolchain, had it. As did plenty of others. > And in most cases you know what will compiler produce—what would be memory representation of the object and how you can reinterpret it differently (I blame C++ for making it harder in newer C standard editions but I’ll talk about it later), what happens on the function calls and such. C is called portable assembly language for a reason, and I like it because of that reason. When the target CPU is an old 8 or 16 bits CPU, and the code gets compiled with optimizations turned off.
- ncmncm 5y agoC simplicity is entirely illusory. The language ISO Standard, printed out, weighs kilograms. It is easy to confuse the language's extreme limitations with simplicity. Even if it were simple, programs coded in C get no benefit from that: the language's limitations mean that the program has to provide whatever the language does not. Thus, a program in C++ will be much simpler than the corresponding C program because you get to lean on what C++ provides. And, let's face it, practically all the complexity we encounter is in program code. The less there is of it to read, the less there is to understand.
- cmollis 5y ago"And everybody essentially picks a subset of C++ and writes in it ignoring the existence of other features.".. very true in my experience.
- CodeGlitch 5y agoBut isn't this great? You choose the bits of C++ that makes sense for your project and run with it.
- cmollis 5y agoyeah.. I really don't see why everyone gets so bent about it. I can certainly understand the cognitive load of learning essentially all of the features so you can find the ones that work.. but really, picking a set of features that work (and ignoring possibly better techniques) seems to true for all languages. Java, Python, etc.. map.filter.reduce.. is better than for loops, but I still see those. C++ has a LOT of features and 40 years of both advancement in the language, as well as strong third-party library development.. so, compared to something like Go, it's a bit daunting to use.
- rramadass 5y agoThat is not wrong at all, You use the language features which best suits the problem and/or you are most familiar with.
- jedisct1 5y agoSame here. C gives a lot of control, and makes it easy to map code to what the CPU will actually do. If you like C, you should really try Zig. Same simplicity and control, but really nice improvements to make code easier to read and maintain.