R
Richard Bos
Capstar said:unsigned int a = UINT_MAX;
printf("%u\n", a);
printf("%u\n", ++a);
When I compile and run this the result is:
4294967295
0
This doesn't surprise me because I overflowed a. But I was wondering if
the standard defines this behaviour,
For unsigned types, yes. Not for signed types or non-integers.
Another question I have is about the assert macro. Is it commonly seen
as a good practice to use this or is it better/safer to just always do a
check yourself and return if something is wrong?
If the answer depends on data you get from your users (even if
indirectly, for example to a file), check, and report _nicely_. Don't
bluntly abort just because someone made a typo.
If the answer depends _only_ on data you have generated yourself, or
which might have come from your user but which you have already
validated and should be correct - in other words, if the error in
question is a programming error, not a data error - then you can use
assert(). Even so, it's never nice to simply lose your user half an hour
of data entered because _you_ made a programming error, so be careful.
Richard