Okay, I'm having an odd problem. I'm trying to make a program to test out the ranges of all the datatypes (exercise 2-1 in The C Programming Language, 2nd ed.). Here's what I wrote (from memory, but I think it's exact):
#include <stdio.h>
int main()
{
short si;
int i;
long li;
for (si = -1; si < 0; --si)
;
++si; // overflow si back to negative
printf("short integer range is %d to ", si);
for (si = 1; si > 0; ++si)
;
--si; // overflow si back to positive
printf("%d.\n", si);
for (li = -1; li < 0; --li)
;
++li;
printf("long integer range is %d to ", li);
for (li = 1; li > 0; ++li)
;
--li;
printf("%d.\n", li);
return 0;
}
Now, that program would output this:
short integer range is 1 to 32767.
(I think it output something about long integers, but not sure)
BUT, if I changed my first for loop to this:
for (si = -1; si < 0; --si)
putchar('\0');
it would output this:
short integer range is -32768 to 32767.
And then it would just sit there, not doing anything. I tried putting the putchar('\0'); in the other loops, but it didn't seem to have any effect.
Anyone know why this happens, or how to fix it? Thanks.
#include <stdio.h>
int main()
{
short si;
int i;
long li;
for (si = -1; si < 0; --si)
;
++si; // overflow si back to negative
printf("short integer range is %d to ", si);
for (si = 1; si > 0; ++si)
;
--si; // overflow si back to positive
printf("%d.\n", si);
for (li = -1; li < 0; --li)
;
++li;
printf("long integer range is %d to ", li);
for (li = 1; li > 0; ++li)
;
--li;
printf("%d.\n", li);
return 0;
}
Now, that program would output this:
short integer range is 1 to 32767.
(I think it output something about long integers, but not sure)
BUT, if I changed my first for loop to this:
for (si = -1; si < 0; --si)
putchar('\0');
it would output this:
short integer range is -32768 to 32767.
And then it would just sit there, not doing anything. I tried putting the putchar('\0'); in the other loops, but it didn't seem to have any effect.
Anyone know why this happens, or how to fix it? Thanks.