I worked on exercise 2-1 K & R, the goal is to calculate the range of different types of variables, below my function to calculate the maximum value of a short intmay contain:
short int max_short(void) {
short int i = 1, j = 0, k = 0;
while (i > k) {
k = i;
if (((short int)2 * i) > (short int)0)
i *= 2;
else {
j = i;
while (i + j <= (short int)0)
j /= 2;
i += j;
}
}
return i;
}
My problem is that the return value of this function is: -32768which is obviously wrong since I expect a positive value. I can not understand where the problem is, I used the same function (with changes in the types of variables) to calculate the maximum value that the int can contain, and it worked ...
Although the problem may be caused by comparisons within operators ifand while, consequently, type casting, this did not help ...
Any ideas what causes this? Thanks in advance!
EDIT: undefined NOT .