HN user

aciuix

1 karma
Posts0
Comments7
View on HN
No posts found.

Checking future result of arithmetic where a wraparound is undesired or undefined, is a basic skill every C programmer should know.

C is not a scripting language. If you use the tools available to you, and don't abuse the language, then it is fairly hard to cause undefined behavior.

I think it is a matter of being consistent. Both ways have certain syntactic dis/advantages.

The first one enables you to have the function call directly in the if statement, but requires you to define a variable beforehand.

The latter gives you the option to check the return value, pass a NULL, if you don't need it for example, and use the return value directly.

The example shown in the article is in fact undefined behavior:

6.3.2.1,p2 If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.

For that to happen the programmer had to be overly clever in the first place, in which case it is his own fault.

If you stick to what is taught in any good tutorial/book, you don't even have to think about problems like this.

But if you decide to play with fire, then you should read the Standard and understand it.