r/C_Programming • • Oct 18 '26

C limitations

Hi all,

I am currently learning c++. Its okay. Not sexy but also not as bad as some say.

The reason I am learning c++ is that I have read that C has limitations as soon as a project gets more complicated. For example, if you make a scientific calculator with many features like graphs etc.

Trading engines/tools are also not recommended to programm in C.

Since I have not learned C and have only read from some old forum posts etc, I cannot confirm/deny these facts.

I am aware that "everything" is possible and that C++ comes with QoL improvements especially with Classes. But I assume that doesn't mean that someone has to use C only for kernel work. Embedded is also possible with C++ right?

I would appreciate your thoughts and opinions.

The goal of this post is to understand how and if C could fit me more with my goals and what real advantages or disadvantages C has.

51 Upvotes

114 comments sorted by

View all comments

Show parent comment

2

u/Kriemhilt Oct 19 '26

C++ objects maintain pointers to function tables

Nah, only if they use virtuals or RTTI.

People writing performant and/or embedded C++ just don't use those language features where it matters, and don't suffer any overhead.

Admittedly, having a list of language features that you have to learn, and then also learn when & why not to use, is a lot of extra stuff for C++ learners.

Preferring C is still perfectly reasonable, but there's room to use C++ as "just" C with better type safety, type-safe generics, compile-time metaprogramming, and possibly newer features like reflection and stackless coroutines.

The more Java-like object-oriented C++ that you're arguing against hasn't been idiomatic since maybe 2011, even though it's still widespread in enterprise settings.

2

u/LordRybec Oct 19 '26

Sure, let's use a language with a bunch of advanced features but not use them, because of performance and memory issues, instead of using a language that is well suited to the application.

I'm not saying C++ doesn't have places where it works and sometimes even well, but if your application requires cutting out a significant portion of the language, you are probably using the wrong tool for the job.

And yes, part of the problem with C++ is that the number of features it has make it a lot harder to learn (and a lot harder to remember all of them even once you have learned them). Yeah, you don't need to know them all to program well in C++, but again, if you aren't going to learn and take advantage of the features, maybe you'd be more comfortable with a different tool.

As far as things like type safety, my specific use cases would be hindered by increased type safety. Type manipulation stuff is often quite necessary in embedded systems and low level programming in general, so stricter type safety means more unnecessary hoops to jump through. That said, increased type safety when you don't need heavy type manipulation can be really nice. Haskell has incredibly strong type safety, which often means that if you can get your program to compile, it has very few or maybe even no bugs. (Which is a bit disconcerting, to be honest.)

2

u/Kriemhilt Oct 19 '26

if your application requires cutting out a significant portion of the language

Strongly disagree on this. C++ is a big multi-paradigm language, and it's completely normal to pick a suitable subset for a particular region of code.

Obviously I do agree though that being big, and supporting multiple paradigms, makes it a lot more to learn.

2

u/LordRybec Oct 19 '26

This is the fallacy of the "do everything" language that drove Java into common use. "My language is a big language that can do all of the things really well" is never true. That's called bloat, and it's part of the reason C++ is substantially less performant than C unless you avoid using most of its non-C-like features.

I understand that it's normal to pick a suitable subset of code/syntax from a massively bloated language. This is part of the reason most modern applications are garbage under the hood. You assume that normal means good, but this is not always the case.

The reality is, the more generalist a high level language (or library, or framework, or platform...) gets, the worse it gets at doing any particular thing. This is because it has to support a huge amount of use cases that any given application only uses a small handful of, and that generalization adds a lot of overhead. This is why cout is so much more expensive than a write system call. (And even printf is moderately more expensive.) The write system call is perfectly well suited to printing out any string you give it, but cout wants to handle type conversion to text for a massive number of different things. write is very narrow and leaves things like conversion to the user. cout is extremely generalist and tries to do everything, the consequence of which is that it can't do anything with good performance or memory usage.

This extends to C++ in general. C is very narrow and leaves the vast majority of things up to the programmer, providing only a handful of basic tools. C++ tries to gives you the kitchen sink, but as a result it's not ideal for any application. (And Java tries to give you the moon on top of that.) If you are only using a small subset of C++ specific features, you are better off using a less generalist language that doesn't have the features you aren't using, because tossing a bunch of the generalist stuff will save you RAM, CPU time, and other resources (often including development time). Maybe you don't care about those things as much as the few C++ features you want to use, but the point is that there are significant, objective benefits to choosing a better suited language if your application requires cutting out a significant portion of the language you are using. Personal preference is purely subjective and does not magically make the objective benefits go away, no matter how strongly you disagree. Anyone can choose to use a poorly suited language for their application, but that doesn't make it wise or good to do so, even if it is normal to do so.

3

u/Kriemhilt Oct 20 '26

I strongly disagree with pretty much all of this. I doubt I'll persuade you, but I don't really want this sort of reactionary misinformation floating around unchallenged.

it's part of the reason C++ is substantially less performant than C unless you avoid using most of its non-C-like features.

  • C++ isn't substantially less performant than C, full stop.
  • It does add some features that have more overhead because they're providing facilities C doesn't have at all.
  • Nobody is forcing you to use those features, and it's perfectly normal not to.
  • The non-C-like features that are completely free include for example:

    • templates (which are much better macros for many of the situations where you use the preprocessor in C, for want of anything better)
    • type safety, which has no runtime overhead even where you do need the reinterpret_cast escape hatch, and has no downside at all except for verbosity where you're doing something unusual (or non-portable, or inadvisable)

that generalization adds a lot of overhead

entirely false, the entirely philosophy of C++ is that you don't pay for what you don't use.

This is why cout is so much more expensive than a write system call

No, it's because it's doing a lot more stuff. The design is now consider old & clunky anyway, and std::print was added for a reason, but it's not a fair comparison. If you don't need all that "stuff" (customizable typesafe formatting, facets, per-stream locales, etc. etc.) then just don't use it. There's no overhead for not using something, and it is again perfectly normal to just not use things you don't need.

write is very narrow and leaves things like conversion to the user

Scattering error-prone manual string formatting and buffer management through your code is empirically not an improvement, except in the very niche cases where that's your main focus or a huge bottleneck. Write being lean isn't an improvement if you still need to call snprintf to generate the buffer to write. C isn't "bloated" for having a redundant printf function in addition to snprintf+write, and C++ isn't bloated for adding a safer, more correct but slower, iostreams either.

C++ tries to gives you the kitchen sink, but as a result it's not ideal for any application.

C++ is closer to the "batteries included" philosophy that the standard library should provide enough tools, to a good enough standard, to at least get started.

std::unordered_map will give you a fairly reasonable hashmap for literally no effort, and if that implementation turns out to be a bad fit, switching to the boost or absl or folly implementations just requires a typedef.

C++ isn't limiting you at all by giving a sane default, and C isn't saving you time or overhead by giving you literally nothing to get started with.

significant, objective benefits to choosing a better suited language if your application requires cutting out a significant portion of the language you are using

Choosing what part of a language you're using isn't "cutting out" anything. You're still writing C even if you never use bitwise-XOR in a particular program. Nobody will accuse you of "cutting out" parts of the language, or suggest using a different language that doesn't have bitwise-XOR in the first place because "it's a better fit for your problem". Nobody will do this because the argument is insane on its face.

I understand this might be hard to believe if you're used to a single-paradigm language with a single reasonable way of doing things, and it's totally reasonable than not everyone likes languages that support lots of different styles, use cases, features etc. But deciding what language features or libraries support a given program is normal even in C. It's just less obvious because there's less scope to choose.

And the very obvious advantage to having a single multi-paradigm language is that I can choose different subsets for different parts of a program and still have it all interoperate without needing FFI bindings or multiple compilers. If I want my core logic to be purely functional, my support libraries to be templated, and the configuration library whose performance I don't care about at all, to use OOP and exceptions, there's no problem at all.

1

u/LordRybec Oct 20 '26

And I don't want this misinformation out there, but you posted it anyway.

C++ is indeed less performant on average than C, often by a pretty wide margin. Argue all you want, but it doesn't make you right.

3

u/Vorrnth Oct 21 '26

Please stop posting this. It's absolutely wrong.

-1

u/LordRybec Oct 21 '26

Take your own advice.

2

u/Vorrnth Oct 22 '26

Why would be c++ any slower for the same features? Doesn't make any sense.

3

u/Kriemhilt Oct 20 '26

My whole career of high-performance C++ development in a shambles!

Citation please.

2

u/LordRybec Oct 20 '26

Citation please? On Reddit? I see at least 6 claims in your last post, with not even a hint of a bibliography. You sound like all of the Java devs of the last two three decades who claimed that Java was just as fast as C because it had JIT and who could not be convinced that JIT itself and the associated profiling cost extra cycles that were rarely refunded by the benefits of JIT. C++ devs have been claiming for ages that C++ is faster than C, and you know exactly what evidence I've seen? Contrived examples compared against deliberately non-optimal C code, just like the kind of examples provided by Java devs.

C++ devs claiming that C++ is just as fast, or sometimes even faster, than C have been around since the 1990s, demanding proof of counterclaims that C is in fact faster, but providing no practical, real life examples of C++ code using C++ features that this is actually true. And you have the nerve to demand proof from us?

If you actually wanted to know which is faster, you could have found the proof yourself with a simple Google search. There are hundreds, if not thousands, of examples online. Now, I'm sure you are going to claim that many of those examples are comparing apples to oranges, for example, cout takes approximately 60 times as long as printf, but cout is the default output call for C++ while printf is the default for C. If you are using printf in C++ to achieve the same speed, then you are essentially using C to cheat the numbers. This also applies to using structs instead of objects and so on. If you are foregoing C++ features to try to make C++ as fast as C, you aren't programming in C++. You are programming in C and compiling with the C++ compiler. Sure, I know, you "seriously disagree". I don't care if you disagree or not. If you cut out all of the C++ features that make C++ slow just to prove it can keep up with C, you are only proving that C is as fast as C, not that C++ can keep up with C.

Also, high performance C++ development? Bull crap. Your approach to this whole thing tells me you have no clue what high performance programming actually is. If you did, you would understand and agree with my claims intuitively. I believe you that you have some C++ experience, and maybe you've even had to do some optimizations to your programs (that you probably wouldn't have had to do with C) now and then. High performance? Not a chance. I'm not going to try to throw around my experience or credentials, because this is Reddit, and you can never know whether I'm telling the truth or not, but if you think what you were doing with C++ was high performance computing but don't understand enough hardware fundamentals to know that C++ can't keep up with C unless you are quite literally just writing C and compiling it with a C++ compiler, I can promise you what you were doing was not even remotely high performance anything.

2

u/Kriemhilt Oct 20 '26

No, all this tells me is that you don't know how to write performant C++. You're absolutely hung-up on the parts that nobody writing performant C++ uses for that specific reason, and you undervalue the features C doesn't have because you don't know how to use them.

Using C++ as essentially C with a better type system, and support for zero-cost abstractions that don't rely on macros, is an absolutely valid approach.

Completely agree on the Java stuff, but then I work in a field where unpredictable latency spikes are unacceptable, and trying to write Java while avoiding GCs is an exercise in masochism.

0

u/LordRybec Oct 20 '26

I know how to write performance C++. Write C.

I actually used to write mostly C++ and before that Java. My first modern language was Java. As a casual game dev (at the time), I quickly discovered Java couldn't keep up (it's faster now, but it's still slow compared to most other languages), so I learned C++. I then programmed mainly in Python and C++ for the next 15 years. Somewhere in there I went to college (I already had 10 years of experience with C++ and 20 years of programming experience in general by that time), where they also taught C++. I ultimately switched to C when I switched over to research, because I was only programmer on my team sufficiently experienced in C++ to optimize the massive programs we were writing for data analysis and certain kinds of simulation. The reason I switched to C is because I could almost always get a 2 to 4 times improvement in speed and reduce memory use by 10% to 20% just by rewriting in C. By the time I switched to C I already had significant experience converting crummy C++ to highly performant C++, and even that would typically get some benefit in both speed and memory by moving it over to C.

I do know how to use all of the features of C++. I went through education on that twice, once on my own and then a second time in college. Some I elected not to use up front, because they make debugging take substantially longer and harder and provide no benefits in terms of dev time, code readability, or performance. The rest all have performance costs. Most of them aren't obvious and cannot be solved or mitigated by the compiler, but they are sold as "zero cost" because the instructions themselves aren't more expensive. If you go out of your way to code differently, you can mitigate a lot of them, but the only way to avoid them entirely is to not use C++ features at all. What it sounds like to me is you are one of those C++ coders who mainly uses C features and avoid C++ features except where you know they aren't likely to have a cost that matters. It's not that hard in C++ write bottlenecks without C++ features to get massive performance gains, while writing code that either doesn't get called much or maybe is in a thread that doesn't need to go fast with C++ features. That's the same thing I did with game programming in C++, and it will get you pretty good results. I used techniques like this when I was in college to do things in C++ that even the professors didn't know you could get to run that fast (and then I got docked points on my grade for not using objects and expensive C++ types, because it was an OOP class I was supposed to do everything in OOP even if it didn't make sense and would destroy performance). The reality though is that if you can do highly performant code in C++, the same thing in C will almost always go faster and use less memory if you write performant C code. But, you and all of the others keep claiming that C++ can keep up, but the only examples I ever see are performant C++ code versus poorly written or at least not optimal C code.

Anyhow, I started programming games at a fairly young age, on systems with very limited CPU power and memory, and I learned to actually do time and memory profiling on my code to test what would perform best. I haven't done comprehensive time/memory profiling comparing C and C++, but I've done enough on bottleneck algorithms that actually matter, and C always wins. Note that I didn't say ties. It always wins, and often (though not always) by a large factor. Even empty programs in C and C++ aren't comparable. You say C++ makes a point of not "charging" for features you don't use, but this isn't true. Even empty C++ programs are doing more setup before calling main and using more background memory to track metadata that C doesn't track. The only way to avoid this is to compile without stdlib, but if you do that, you lose most of features that make C++ attractive over C.

And as far as having a "better type system", I've not found the C++ type system to be any better. C++ namespaces are nice, but swap a dot for an underscore, and you have the same thing in C. I don't find myself needing to use C macros for anything except defining global constants and platform filtering. I know some people think they need macros to do complex things, but I've found that you can generally do those things much more elegantly and cleanly without macros. I don't know how you think the C++ type system is better, but all I've found is that it requires a lot more mental space and effort to keep track of that I could be using for actually solving programming problems with instead. C has all of the types and type creation tools I need, and they are easier and faster to use and even easier to reason about than the horrific mess that is C++.

And yeah, I feel like modern Java traded fairly consistent poor performance for faster performance most of the time with massive lag spikes now and then. And I don't think it is just the GC. Java has had JIT since the mid 1990s, but over the years it has put more and more into the profiler and JIT, to try to improve speed. The problem is, profiling and compiling are inherently expensive. A light profiler and JIT can improve overall performance without noticeable lag spikes during profiling and compiling, but Java put a huge amount of effort and code into extremely deep profiling and extreme JIT optimization, and the result of that is lag spikes from the profiler and JIT. GC lag spikes are consistently worse in most applications, but the JIT and profiler aren't great either. They do increase average performance in most cases (though sometimes they end up consuming more resources than they save), but that comes at the cost of lag during deep profiling analysis and then the optimizing JIT following that. So modern Java is around twice as fast as old Java on average (on comparable hardware), but it's plagued with lag spikes that are a serious problem in games and high performance applications.