Answers/Solutions to Exercises in Chapter 3, Exercise 1
E1: While compiling a C program, the GNU C compiler gave us an error message warning: comparison is always true due to limited range of data type. What do you think, does it warrant a closer inspection of the statement it refers to, or can it be safely ignored? If you think that it cannot be safely ignored, can you give an example where it may be essential?
A1: No, it should not be ignored. There are many such situation and they all
indicate some deficiency in the logic of the program, or an unintentional error. For
instance, if a variable x is defined as unsigned char its value can range only between
0 to 255. Thus the statement x < 256 is always be true.
Either it should not be tested at all, or may be something lese has been intended. Another
example may be a variable x defined as unsigned int --- the statement x > 0
is always true.
A "classical" example from the book:
unsigned char i;
...
...
for(i = 0; i < 256; i++) {
body of the loop
}
...
This leads to an infinite loop.
Back to Answers/Solutions Index Back to Answers/Solutions for Chapter 3 Index