I was working on the challenge for chapter 9, Pointers, when I faced an issue with shorts.
// Find the largest positive value of a signed short (integer) current = 0;
current = 0;
previous = 0;
do {
previous = current;
current++;
} while (current > previous);
printf("The min value of a signed short is %d\n", current);
This would return -32768 but I expected the positive absolute of this number.
If each step would be printed, it would show something like:
current == 1
current == 2
…
current == 32768
step out of while loop since end has been reached.
result: current == -32768
Where does that minus sign come from here?
// Find the largest negative value of a signed short (integer)
current = 0;
previous = 0;
do {
previous = current;
current--;
} while (current < previous);
printf("The min value of a signed short is %d\n", current);
This would return 32768, but I expected the negative of this value.
// Find the largest negative value of a unsigned short (integer)
unsignedCurrent = 0;
unsignedPrevious = 0;
do {
unsignedPrevious = unsignedCurrent;
unsignedCurrent++;
} while (unsignedCurrent > unsignedPrevious);
printf("The max value of a unsigned short is %d\n", unsignedCurrent);
This would return 0, but I expected a large positive number.
// Find the largest negative value of a signed short (integer)
unsignedCurrent = 0;
unsignedPrevious = 0;
do {
unsignedPrevious = unsignedCurrent;
unsignedCurrent--;
} while (unsignedCurrent < unsignedPrevious);
printf("The min value of a unsigned short is %d\n\n", unsignedCurrent);
This would return 65535, but I expected 0.
EDIT: corrected the result of the final example