Undefined behaviour in C

Unexpected
Image by John Hain from Pixabay

One of C’s not so brilliant features is the range of Undefined Behaviour (UB). Things like using an uninitialised variable or having an int variable overflow don’t have behaviour defined; thus it is UB and you cannot accurately predict what will happen. Likewise accessing a NULL pointer can cause odd behaviour. It gets more sophisticated than that. What if you type cast an int to a float?

The LLVM blog have an interesting set of posts on UB and it’s definitely worth reading. I’ve done a lot of C programming, so I’m rarely surprised by my programs but its useful to know about these things. In my case, I started with assembler programming and then learnt C++ and C in that order so my perspective has always been to try and understand whats going on deep down.

Leave a Reply

Your email address will not be published. Required fields are marked *