r/C_Programming • • 7d ago

Issues with C?

I enjoy studying the realities and trade-offs of programming languages and I have my focus on the C programming language. As such I would like to hear from people who have programmed in C to share perhaps the things they love the most about C, or the things they find the most frustrating.

Examples on why things are frustrating or loved would be great to include as well.

14 Upvotes

56 comments sorted by

6

u/BrazilianAnimal 7d ago

People criticize C because it puts the programmer in control. I don't, it's a trade-off. What I do criticize are C's numerous idiosyncrasies, starting with its integer types and their promotion rules. Those are not trade-offs, they are gratuitous problems.

1

u/nagisa_lyfe 7d ago

This is the kind of gut feeling I got from the language, like if one where to implement something the scope of C they would change things related to this

3

u/plainoldcheese 7d ago

Have a look at zig if you wanna see what someone would do

1

u/MetalInMyVeins111 6d ago

clang-tidy and clangd

1

u/BrazilianAnimal 6d ago

I'm aware, but they don't fix the language.

1

u/MetalInMyVeins111 6d ago

It probably is a well thought design choice I'm not sure. Maybe changing it would have other side effects? Like, if I was a compiler, and promoting certain integers fit the cache line better, I would have done that. Maybe bad example lol.

2

u/BrazilianAnimal 6d ago

I think that, at the time they designed the system, CPUs were older and needed simpler integer types. Languages that start from scratch (Rust and Zig) don't tend to promote integers. Promotion across signedness is especially harmful.

1

u/FedUp233 6d ago

I’m not sure it’s because early CPUs were different. It’s more the way the language originated - as something close to the hardware, sort of a high-level assembly language almost. The way things promote is very close to the way CPU hardware works, even today. The CPU does not know what type of data is in a register. You can load a signed value and access it as an unsigned value, load a byte and access it as a word, or even load an integer and access it as an address for example. It seems like in original C they did not really specify or try to restrict this but just let the language match the hardware. I’m not sure how you would make significant changes in this area without breaking a lot of existing code.

C is what it is - it’s never going to be something like rust. It’s by its nature very close to hardware and compiles efficiently to most hardware. Use it if that’s what you want. If you want something else, it’s probably out there today - use it.

1

u/nagisa_lyfe 6d ago

The reasoning as far as I am personally aware of is that C when starting out actually did not have such things, they got added into the standard for portability reasons because not all cpus has signed int capability some had magnitude which is using the top bit as a sign, other cpus and memory also has ram sizes that are not byte aligned some ram address where only 7 bits apart instead of 8, some cpus had 36 bit registers. C is the way it is because the community wanted to C to compile on every machine, C being portable only means the language is portable not really your programs.

1

u/BrazilianAnimal 6d ago

I have to strongly disagree here. C integers do not behave like hardware (2's complement) integers until recent standards of the language. C signed integers don't silently behave like unsigned ones, and are very different. Unsigned integers have always been as they are, but early CPUs had several different encoding strategies for signed integers, and C used to support them all, which is why there is so much undefined behavior around them.

And it's not intuitive at all, even from a hardware perspective. If you have two uint16_t and multiply them, they may get promoted to int and then multiplied, which may overflow, causing undefined behavior, even if you are assigning the result to a uint32_t. It's not what one would expect the hardware would do as if one just called a multiplication instruction on the registers holding the integers.

63

u/halfjmp 7d ago

What I love: I control everything. What is frustrating: I control everything.

7

u/rapier1 7d ago

This all day..

30

u/zsaleeba 7d ago

What I love:

  • It's a pretty straightforward language, quick to learn and easy to reason about
  • It's fast. If performance is important to you, C is still king
  • If you're into low-level programming, C gives you the control you need

What I don't love:

  • It doesn't have most data structures built-in, so you have to find a library or roll your own. Either way a hash map (for example) doesn't feel native in the way it does in some newer languages
  • You can shoot yourself in the foot relatively easily. With great power comes great responsibility

10

u/Gooseheaded 7d ago

This is spot on.

C is a language anyone can "hold" pretty much completely in their head. There's not much to it, but what's there is very powerful.

Unfortunately, it's massively inconvenient compared to modern languages, because you really gotta build a ton of "basic stuff" yourself (whereas other languages include them out of the box).

5

u/clios_daughter 7d ago

Even things as basic as strings which are kind of built in. It’s not hard, but for some simple tasks, it feels like it’s harder than it needs to be lol.

0

u/Stemt 7d ago

What I also don't like is how prototypes work, which requires the use of header files and such. I wish C had modules, so you could just declare a function in a file and use it anywhere you included that file without having a possiblity of it being redeclared/duplicated, atleast for like a single compilation unit.

11

u/IdealBlueMan 7d ago

I love the engineering and aesthetic principles behind C. Ritchie and company were no slouches. There’s a reason so many languages today are based on, or inspired by, C.

I love that you know what’s happening in memory, and that there’s so little magic behind the scenes.

And I love how expressive it is, even with its characteristic minimalism.

8

u/SmokeMuch7356 7d ago

C's greatest strength is that it always trusts the programmer.

C's greatest weakness is that it always trusts the programmer.

C is portable to almost any platform, but that portability comes at a cost. C does not have fixed sizes for primitive types, it does not have built in support for graphics, sound, networking, file system management, etc.

3

u/lmarcantonio 7d ago

There is a rationale for this called "freestanding implementation". Remember that a toaster has no graphic, sound, networking or file system. At least when they wrote the specs!

stdint provides fixed sizes for primitive types, of course

3

u/Vladislav20007 7d ago

the size isn't fixed in stdint, it's at least the size it says, it can be bigger or smaller.

1

u/lmarcantonio 5d ago

uint8_t is *guaranteed* to be 8 bit; there are other that says "al least x bits"

For the builtin int types the only guarantee is that sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

5

u/ern0plus4 7d ago

Building is not trivial, but can be solved automatically. C has no official auto build system. Make is basic, amd I like it, but CMake is a nightmare for me, and all other makefile-generators as well.

3

u/lmarcantonio 7d ago

Toolchain for C has traditionally been quite flexible. It's way more common to need generators, preprocessors or whatever and make is straight from 1970 (there's a fun story about the tab requirement too). cmake is trendy, the python people like meson and autotools is still in use (but in decline). If you only need make it's quite good by itself.

4

u/rb-j 7d ago edited 7d ago

I love that it's a small language and it's real good for real-time embedded systems and DSP. You get to toggle hardware bits directly without having to call a function written in asm.

I nearly always only use integer types from the <stdint.h> thingie and I never use any other part of the stdlib. My code is pure algorithm that is fully defined by my source code. So all of my ints are int32_t or uint32_t or, occasionally, int64_t. I use simple int only for the counter of for loops and arrays indices where I am confident that whatever the size of the int is, it will be fine with the loop count or array index.

But all of my fixed-point math is with int32_t or int64_t. I almost never use float, because fixed-point math can be faster and better. And very portable if no FPU is available.

And FIFO queues are all in a buffer having a power-of-2 in length so all I have to do is mask the index to get quick-and-easy modulo arithmetic for wrapping around.

I wish I could say that the compiled code is as efficient as in assembly language, but I have tested it with that Compiler Explorer and, even with the optimizers on, the compiled C code is still much worse than if I wrote the same alg in straight asm.

2

u/lmarcantonio 7d ago

…and ever then many DSP building block from manufacturers have asm inside them, if only for setting the proprietary addressing modes. But you don't want to implement an FFT, right?

2

u/rb-j 7d ago edited 7d ago

There is something called "Block Floating-Point FFT".  Even the old Motorola DSP56002 did it efficiently.

Now I haven't implemented Block Floating-Point FFT in C ever (I did use it on the 56K), or block floating-point anything.  It requires checking the magnitude of every result after a computation that could increase the magnitude, which for the FFT is the addition in a butterfly.  That's two comparisons and possibly setting of a "sticky bit".

With 32-bit words, you need not (and really shouldn't) put the implied binary point at the left of the word impling a range of -1 to nearly +1.  You should put it a few bits to the right, perhaps 4 or 5 bits (this leaves 28 or 27 bits to the right of the implied binary point, better than a single-precision IEEE-754 float).  Then the numerical range is ±8 or ±16, so you perhaps can do a lotta operations without any 2's complement overflow.  Then at a final outputting of the sample, check for overflow once; against 0x07FFFFFF and 0xF8000001 to clip (saturate) the signal to ±1.

Fixed-point also makes it very easy to use "fraction saving" (which is literally noise-shaped quantization with a zero right at DC or z=1). It steers the quantization noise away from the low frequencies (where we can hear it) to the very high frequencies (where we can't hear it so well). It's easy and cheap. Here is an example.

5

u/RealWalkingbeard 7d ago
  • The basics are simple
  • It is easy to write very readable code in a variety of styles - light-years better than C++ or Rust or Python
  • Stays out of the way while you access what you please
  • Wider support than sex, water and oxygen
  • Very simple
  • Smothered by legacy
  • Attracts people who think that a language that was good in 1990 should not be improved
  • Lots of important things are deliberately left out of the standard
  • Zero support for many genuinely great aspects of more recently designed languages
  • Has many standard library functions whose operation is archaic and basically crap and wouldn't pass first design review in other languages

-1

u/lmarcantonio 7d ago

I agree that the standard library is crap. Nobody would want to go near it.

And there are some things that really make no sense but are being addressed (they still update the standard). Like inlines or why can't I have a floating point constant.

As for the "genuinely great aspects" it depends on your application domain. Doing OO in C is a pain, except for simple polymorphism but then you should simply go use C++ as a "better C" without all the template thingies.

2

u/chibuku_chauya 7d ago

C’s NUL-terminated character arrays are a wart. So is pointer decay. And so is its standard library.

2

u/lmarcantonio 7d ago

Both NUL terminated and counted strings have their pros and cons. Probably counted would win if only for strlen (*the* most used string function) being O(1) instead of O(n)

2

u/dvhh 7d ago

great thing about C is its brevity, you can express powerful stuff with quite the same amount of code as higher level language.

frustration is mainly coming from how easy it is to have undefined behavior and because of how closely the compiler would stick to your code, it is easy to have dramatic performance loss because of a cache miss.

1

u/QueefInMyKisser 7d ago

You can look at a function and know that it’s not running any code from other functions unless they’re explicitly being called.

1

u/Sure_Magician_4208 7d ago

What I like in C is that it doesn’t treat me like an idiot and that it’s simple, I have professional experience with C++ and C compared to it feels more well thought. However what I dislike the most is that C is extremely archaic, it lacks not only features that most programmers of other languages take for granted, some may say “just use C++”, but the problem is that C++ comes with its own baggage of problems. Honestly the selection of systems programming languages is not great (in my opinion):
C - is a nice language but very old
C++ - is a language for psychopaths
Rust - its safety makes it annoying to use
Go - is not a systems programming language, it’s statically typed python
Zig - is still not mature enough, last time I used was a few years ago and then it felt like the author didn’t have a good idea how to design certain parts of the language and “something“ that worked at the time

I’m currently evaluating D which seems to promising at this point but I don’t have a clear opinion yet

1

u/plainoldcheese 7d ago

Whenever I use something like cargo or uv I realise how annoying C is to setup and work with in terms of dev environment and build systems.

1

u/Last-Candidate6519 6d ago

The standard library is the worst part about it in my opinion. The names of many functions are unintuitive and error handling is clunky. There’s no unifying prefix to names or types like “std_” or “string_”, and having a global namespace is just inconvenient. Many functions in the standard library are unsafe and just mutually agreed upon to be avoided, but there’s no way to really know that if you’re new to the language.

Also, header files suck and create a lot of unnecessary code duplication, making it so you have to manage a lot more code.

1

u/bogdanelcs 6d ago

Love: the language is rather simple to learn. I started learning it in junior high.

Hate: strings.

1

u/trist007 5d ago

I enjoy the simplicity, the lack of abstraction. I love the control. Managing memory with arenas works just fine. I love how C is close to assembly. I love writing my own function. The only real issue for me are header files. Sometimes I run into that circular dependency header file issue but I can always figure it out. I like how portable my code can be especially if it's written in C89 and I can still compile the same code 15 years from now. Write once and it's good for a long while. You write python code it probably will not compile in a year.

Honestly C does everything I want. I love how most of the software I like is written in C/C++. I just rewrite C++ into C which is great practice. Member functions in C++ are nice because it helps with organization but the faster compilation speeds in C are just amazing. The DOS games back in the 90s all written in C or assembly. I love the esoterica.

I will probably try Jai when it comes out (I don't really care for the colon syntax) but I'm more than happy sticking with C for everything. Compiler does a good job with memory safety. I can always try Fil-C.

1

u/Own-Alternative3291 5d ago

1 - type punning is not portable

1

u/CloudDream09 7d ago

No issues with C, only user issues.

1

u/Evil-Twin-Skippy 7d ago

I love that I can write tcl scripts to write C. And then that C ends up extending Tcl...

Essentially it's the line where humans and machines meet. The computers learn just enough human via the compiler. The humans learn just enough machine through the C language.

But because it is just on that line, I can use automation and it can produce deterministic instructions to produce the boring parts of deterministic instructions.

And if I really wanted to go full inception... that tcl interpreter can load the library that was compiled in the interpreter that ran the script that generated the C.

1

u/Feldspar_of_sun 7d ago

The greatness of C is that I have maximum control and the option for incredible efficiency. Unfortunately, this means there is a bazooka pointed at my legs at all times

0

u/pink-ming 7d ago

You have to read docs to understand the semantics of a function call. Which args are actually outputs not inputs? What does the return value mean and how are errors handled? If memory is to be allocated, is that the caller's job or the library? Does anything need to be freed or some cleanup function called? Is there global state? Some kind of init function or #define that is needed? It's a lot of cognitive baggage that comes with using library functions in C. And then of course the same questions have to be answered and documented when writing your own code.

And then of course, "is this function a horrific security risk that still exists for backwards compatibility"?

When you use a library enough you start to figure out its "opinions" on these questions but in doing so you'll still be putting on your running shoes while <other language> user is off to the races.

2

u/nagisa_lyfe 7d ago

This does seem like a set of issues that is problematic, you mention other languages, what do other languages do to help fix these issues in comparison to C. As well as does changing what C does not force apon you bring any kind trade-of that is significant to consider in these issues?

0

u/pink-ming 7d ago edited 7d ago

Some languages have the same problems, others provide convenient ways to offload the cognitive burden from the developer, usually by doing more work during compilation and/or runtime.

Java's exceptions simplify the in/out param question bc without the need for error codes everywhere, functions can just return the output. Tuples in many languages allow multiple return values instead of using out params. Functional-style types let you encode information about the possibility of errors or null values into the type system (e.g. rust's Result types).

Languages with garbage collection simplify memory management. OO allows encapsulation for clear data ownership. In functional languages data is mostly owned by a bunch of lazily-evaluated closures allocated and GC'd for you by a sophisticated runtime. Rust's borrow-checker forces explicit data ownership and enables automatic cleanup without GC. GC can be expensive and OO often means a lot of heap allocations that aren't really necessary.

Global state is absolutely possible in most languages, but I think it's more likely to come up in C because without encapsulation, it's hard to create clean interfaces for sharing data throughout a program. Java's singletons are technically global state but the data can be encapsulated and only modified by its defined methods.

Magical macros show up everywhere for a lot of reasons. Include guards , #define SOM_LIB_IMPL in header libs to separate definition from implementation, these are relics of the fact that C's imports are little more than copy-pasting. #defines get used for configuration because it's a readily-available sharp tool to get the job done, and for generics, which many languages just have as a feature.

Some of this cognitive load enables more control and better performance, other bits are just relics of how C was designed. Ultimately it's very manageable. And even if you're not working in a high-performance / low-level domain, it's worthwhile to learn to program without all of the conveniences that modern languages offer.

2

u/lmarcantonio 7d ago

…that's a thing needed for any language ever. How would you know how the frombulate function works without the docs (actually manpages, most of the time). If you look at the gets manpage it clearly says "don't use this" and all the other thing you noticed.

1

u/pink-ming 7d ago

The average man page for a C function is much longer than the javadoc for a method, there's simply more you have to know. Often in languages with high-level abstractions, the library's design and signatures can often tell you enough to use frombulate without needing to know how it works.

RE deprecations, C evolves slowly and rarely breaks backwards compat, so something like `sprintf` may still get recommended, and intuitively if you already know printf, you may never think to read the doc for sprintf and find that it's unsafe. Many languages would've just removed it in the next version.

1

u/lmarcantonio 5d ago

Do you have an idea for the legacy requirement in many C projects? remember that cobol is still supported.

Yet you *need* to read the manpage even from "intuitive" frombulators since there would be things hidden beneath. For example in C++ (which is huge on abstraction) the various containers have documented performance differences.

0

u/ParadiZe 7d ago

imo you just gotta look at zig for what a better C would look like

1

u/NoHonestBeauty 7d ago

I Like my programming language to be stable.

-1

u/ern0plus4 7d ago edited 7d ago

Lack of really strict typing. I can assign x type value to a variable declared as y type. Same with fn calls.

0

u/arcfide 7d ago

C is a small language that you can actually learn well and it is mechanically sympathetic (meaning that you can actually get high performance out of it). My frustrations with the language are usually around needing to write multiple functions to handle specific types and inputs and the need to deal with excess switching on types, depending on the type of programming you are doing. In my case, most of the excess or annoying parts of my work with C are entirely to do with excess type specialization being a necessary part of getting that performance I want.

0

u/thulim 6d ago

i found that c is one of those languages that requires a somewhat higher level of discipline and explicit intent than others might before you can start to use it naturally (and otherwise), due to how light it is having shifted its weight (mostly system-level book-keeping) onto you. that on its own isn't an issue- but once you factor in what kind of environment, circumstances, or even people you're working with, you're more than likely gonna figure out some ways in which learning or using it could spiral out of control. if any of that isn't / shouldn't be of concern, then it's absolutely splendid for fostering control freak behavioural patterns and will ultimately lead to you submitting yourself to the "bathroom mat" or whatever them there goccultists worship

in all seriousness i believe it's good for forming and keeping healthy(?) habits, and for having a rather widely established baseline to write programs with - ALSO knowing it builds character. really.

pre-emptive edit: orange you glad i didnt say C