Handle with care – C programming

Warning sign
Image by Clker-Free-Vector-Images from Pixabay

If you had programmed in say BASIC before learning C then you’d be used to a world where the worst that could happen might be a divide-by-zero crash or a hang-up due to an infinite loop. But comes to C and it has a whole raft of undefined and unexpceted behaviours.

Just how big is that list of undefined behaviours? Would you believe 193? A developer with the user name Earnestly on GitHub has compiled a list of 193 undefined behaviours. MInd you, it came from a 2007 draft C99 specification which you can find here. Note, if you scroll up a bit you can also find a list of unexpected behaviours such as what happens when you call malloc with 0 as a parameter.

Even more interesting take a look at Translation limits which is a list of maximums in the document. Such as 1023 cases in a switch statement. or 127 nesting levels of blocks or 63 nesting levels of conditional inclusion. If you’ve ever wondered how nested #include files can go, it turns out it’s 15.

I wouldn’t bother trying to learn all the undefined or unexpected behaviours. You just have to know how to write code that doesn’t make any assumptions for instance when a signed int overflows what happens? Or if you add an unsigned int to a signed int? Best if you can, not doing these things in the first place. If you do, it might work ok for you on your computer but if someone else runs it on a different system. it may behave quite differently.


(Visited 7 times, 1 visits today)