r/C_Programming • • Jun 11 '26

Question C ways to manage errors?

I'm still learning C (I come from C++) and I'm not familiar with how to manage errors in C, besides asserts. What are the different ways to do this in C? How do they work?What are their pros and cons?

35 Upvotes

58 comments sorted by

View all comments

Show parent comment

2

u/pjl1967 Jun 11 '26

Google it, e.g., here.

0

u/heavymetalmixer Jun 11 '26

Huh, that's quite specific. So I should use both together for different purposes, right? Sounds like asserts were made for checking stuff that only changes due to UB . . . or someone messing with memory.

5

u/pjl1967 Jun 11 '26

So I should use both together for different purposes, right?

Yes.

Sounds like asserts were made for checking stuff that only changes due to UB.

No. Undefined behavior is, well, undefined. You can't check for it reliably. UB literally means "anything can happen." For example, it's entirely possible for an invariant to be violated due to UB yet an assert explicitly checking for just that condition not fail.

For a trivial example, you can use assert to check that a function argument is not NULL. If it is NULL, it means you messed up by passing a NULL pointer.

IMHO, code needs to fail fast and hard to catch mistakes lest you do irreparable damage unknowingly.

Also, from Why Learn C, §16.6:

Do not use assertions to validate input from a human (e.g., via keyboard), machine (e.g., via socket), or file — even trusted humans, machines, or files. Your program should not crash because a human made a typo or you received or read corrupted or otherwise unexpected data.

1

u/heavymetalmixer Jun 11 '26

Aren't null pointers and checks for addresses and indexes out of bounds also errors?

3

u/pjl1967 Jun 11 '26

It's debatable. Reasonable people can disagree. I've already stated that I believe those things are invariant violations and therefore bugs that need to be fixed, not errors returned to the user.

2

u/Crtusr Jun 13 '26

The problem With UB is that the compiler makes assumptions about your code, so if it assumes a certain block of code as unreachable, it will eliminate it.

i.e.

int *ptr = NULL; //for clarity ptr = malloc(BYTES);

*ptr = 5;

if(ptr == NULL) { return ERROR; }

free(ptr);

Here the compiler assumes that since pointer was dereferenced before the check it will assume ptr is not NULL so it will delete the whole if block. There are less obvious cases but this is good for illustration purposes

At least that is how I understood it. There are more evil ones like UB caused via type promotion. These are extremely hard to debug, mostly because you will be caught off guard when they happen and you would be like "...but my logic was perfect".

1

u/heavymetalmixer Jun 13 '26

So I should use asserts inside functions often to make sure the invariants are kept?

2

u/Crtusr Jul 02 '26

NASA's Power of ten puts a limit on the amount of asserts Inside functions (I believe one or two), but as many have said, it is for things that should never happen, that means, you are assuming there is a bug in your logic (that is not UB, because it can bypass assertion).