2

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.

user007
  • 2,156
  • 2
  • 20
  • 35

3 Answers3

7

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? :)

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • Thank you so much.. :) and sir one more thing is `long int` and `int` same?? because i try printing `printf("%ld",LONG_MAX);` but it gives the same output as `printf("%d",INT_MAX);`but it gives the same output.. any reason?? :\ – user007 Oct 05 '14 at 20:57
  • http://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be – Karoly Horvath Oct 05 '14 at 21:08
  • 1
    @user007: We say that such behaviour is implementation-defined. Try to compare `sizeof(int)` and `sizeof(long)`. These should have the same values (in number of bytes) for your compiler, but C standard only says that `sizeof(int) <= sizeof(long int)` and requires that `int` is at least 2 bytes and `long int` at least 4 bytes. – Grzegorz Szpetkowski Oct 05 '14 at 21:08
  • @GrzegorzSzpetkowski If they have the same size with a few compilers then what is the use of having 2 different data types if they can hold the same values?? Neither I guess we can perform any special functions on long int and not on int, as they both are numbers.. – user007 Oct 05 '14 at 21:21
  • @user007: It's mainly because of portatibility between various implementations. Some may have 16-bit `int`, some 32-bit, etc. – Grzegorz Szpetkowski Oct 05 '14 at 21:34
  • Hi, I just encountered something related to this question http://stackoverflow.com/questions/1472581/printing-chars-and-their-ascii-code-in-c Isn't this undefined behavior as well??? Using `%d` to print `char` ??? The first ans to this question. – user007 Jul 10 '15 at 16:14
  • 1
    @user007: nope. http://en.cppreference.com/w/cpp/language/variadic_arguments "bool, char, short, and unscoped enumerations are converted to int or wider integer types as in integer promotion" – Karoly Horvath Jul 10 '15 at 16:43
6

%lld is incorrect specification for int. According to the standard, it is undefined behavior:

7.21.6.1 The fprintf function

....

If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.


7.21.6.3 The printf function

....

The printf function is equivalent to fprintf with the argument stdout interposed before the arguments to printf.

AlexD
  • 32,156
  • 3
  • 71
  • 65
  • 1
    If I write `printf("%lld",2);` will that be defined?? – user007 Oct 05 '14 at 20:50
  • 2
    @user007 No. It is undefined. The format specification does not match the type. – AlexD Oct 05 '14 at 20:51
  • Hi, I just encountered something related to this question http://stackoverflow.com/questions/1472581/printing-chars-and-their-ascii-code-in-c Isn't this undefined behavior as well??? Using `%d` to print `char` ??? The first ans to this question. – user007 Jul 10 '15 at 16:12
5

You push 4 bytes of data (int-length) onto the stack in the call to the function, then tell the function to consume 8 bytes from the call stack to print it. So your value is trailed by 4 bytes of garbage.

You can tell the compiler to check for this.

Asmyldof
  • 173
  • 1
  • 7