Greetings , and again today when i was experimenting on language C in C99 standard , i came across a problem which i cannot comprehend and need expert’s help.
The Code:
#include <stdio.h>
int main(void)
{
int Fnum = 256; /* The First number to be printed out */
printf("The number %d in long long specifier is %lld\n" , Fnum , Fnum);
return 0;
}
The Question:
1.)This code prompted me an warning message when i try to run this code.
2.)But the strange thing is , when I try to change the specifier %lld to %hd or %ld,
the warning message were not shown during execution and the value printed out on the console is the correct digit 256 , everything also seems to be normal even if i try with
%u , %hu and also %lu.In short the warning message and the wrong printing of digit only happen when I use the variation of long long specifier.
3.)Why is this happening??I thought the memory size for long long is large enough to hold the value 256 , but why it cannot be used to print out the appropriate value??
The Warning Message :(For the above source code)
C:\Users\Sam\Documents\Pelles C Projects\Test1\Test.c(7): warning #2234: Argument 3 to 'printf' does not match the format string; expected 'long long int' but found 'int'.
Thanks for spending time reading my question.God bless.
There are three things going on here.
printftakes a variable number of arguments. That means the compiler doesn’t know what type the arguments (beyond the format string) are supposed to be. So it can’t convert them to an appropriate type.intare “promoted” tointwhen passed in a variable argument list.intandlongare the same size, even when pointers are 64 bits wide (this is a willful violation of C89 on Microsoft’s part – they actually forced the standard to be changed in C99 to make it “okay”).The upshot of all this is: The compiler is not allowed to convert your
intto along longjust because you used%lldin the argument list. (It is allowed to warn you that you forgot the cast, because warnings are outside standard behavior.) With%lld, therefore, your program doesn’t work. But if you use any other size specifier,printfwinds up looking for an argument the same size asintand it works.