I am using Turbo C++.
This is code:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("%d",1000*100);
printf("\n%d",1000*10);
getch();
}
Output:
-31072
10000
Why does the first
printf()give wrong and signed value? Whether integer have range (-2147483648 to +2147483647).And in second
printf()it gives right value with right sign. How?
1000*100=100'000. If your
intis 16-bits-long, then the result is higher than the maximum supported value, which is 32'767. If you're running a 32-bit OS or you're making a 32-bit program, consider using "long", which is 4 bytes in the case of 32-bits OSs or 8 bytes for 64-bit ones; the highest supported value will respectively be: 2'147'483'647, 9'223'372'036'854'775'807. To have alongas an output, use one of the following format specifiers:%l,%ldor%li. To see the maximum value forint, includelimits.hand check (see the last source).Hope it helps.
Sources: