C what is the short form for long unsigned int
Solution 1
However I don't really understand what I am doing.
What you're doing is telling the printf
function how to display the data that you provide following the format string. You'll probably find that %lo
doesn't really print the results you expect -- it prints the right data, but in octal (base 8) format.
Is there a naming convention to get the short form of a type? For example, int is %d, why??
The "short form" is called a format specifier. There's no convention that lets you derive the appropriate printf format specifier. You just have to look them up in an appropriate reference.
Solution 2
long unsigned int = unsigned long
You use %lu
to print those.
And similarly, %llu
is for unsigned long long
.
%d - int
%u - unsigned
%ld - long
%lld - long long
%lu - unsigned long
%llu - unsigned long long
Solution 3
No, there is convention, you simply need to look at the documentation.
And the formatting specifiers are not a 1:1 mapping to the type, they control the output format (hence the name). There can be many output formats for a single input value type.
For instance, %d
is "decimal", since it prints an integer as a decimal number. There is also %i
("integer") that does exactly the same thing.
For unsigned int
values, you have both %x
(print in hexadecimal) and %u
(print in octal).
Solution 4
The definition of int, long, etc are specific to the target. If the size of int and long match then a
printf("%d",var);
wont complain, but if long is say 64 bits and int is 32 bits as an example, then you will get the warning/error you describe. One of two solutions:
printf("%ld",var);
or
printf("%d",(int)var);
For the latter of course you have to insure that the compiler associates int with the %d size, if you get yet another warning then adjust accordingly.
EDIT:
The compiler is trying to help you out, by worrying about C library stuff which is not really the business of the compiler. printf() uses a variable number of arguments, which hopefully you properly matched to your format string. When printf sees a %d on say a 32 bit system it likely will only grab the next 32 bit argument. But if you had put a 64 bit integer in as a parameter it may grab one half of that integer, and use the other half as the next item in the format string. For example
unsigned long ul;
float f;
f=3.4;
ul=0x3F9DF3B612345678;
...
printf("%X %f\n",ul,f);
Depending on your system, endianess, etc, a 32 bit system you should not at all be surprised if the above code produced this output:
12345678 1.234000
because that is what you told it to to. you told it to take the lower 32 bits of ul and print that as hex (%X) and the upper 32 bits of ul and print that as float (%f) and putting f in the function call was a waste you didnt provide any formatting to use the floating point value.
Now depending on the compiler on the target system on a particular day you may have the above code work as desired, take a system/compiler where unsigned long is interpreted as a 32 bit and %X is interpreted as 32 bit, then you get a warning about the 64 bit assignment but the printf makes a little bit more sense.
Because of this pain, compilers like gcc bother to try to make your life better by assuming that when you use a function called printf() you are using the standard C one and they parse through your format string looking for these types of common mistakes.
icedTea
Updated on February 21, 2020Comments
-
icedTea over 4 years
when compiling my program with GCC I get the following warning:
format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int
Now just by playing around I realize %lo fixes the warning. However I don't really understand what I am doing.
Is there a naming convention to get the short form of a type? For example, int is %d, why??
Thanks!
-
Mysticial over 12 years+1, It's a shame that late answers like this don't get the upvotes that they deserve.
-
unwind over 12 years@RichardJ.RossIII: Good catch, thanks. I should take my own advice a bit more often, it seems. Fixed.