I try to print the maximum value of int
in a program.
Using the following code::
#include <stdio.h>
#include <limits.h>
int main(void) {
printf("%d",INT_MAX);
return 0;
}
The output I get is:: 2147483647
But, when I change my printf
statement to printf("%lld",INT_MAX);
my output becomes 577732524332023807
. INT_MAX
value is supposed to be inside the range of long long int
, then why is it not able to convert INT_MAX
into the correct number in long long int
.
Thanks for any help in advance.
printf
is a variadic function, it doesn't know its argument types, it relies on recieving the correct hints in the format string.
You invoked undefined behaviour with "%lld"
, because you haven't passed a long long int
.
To fix it, you need to cast - then you'll see the correct result:
printf("%lld", (long long int) INT_MAX);
Enable compiler warnings? :)