[ACCEPTED]-C what is the short form for long unsigned int-types

Accepted answer
Score: 25
long unsigned int = unsigned long

You use %lu to print those.

And similarly, %llu is 1 for unsigned long long.

%d    -   int
%u    -   unsigned
%ld   -   long
%lld  -   long long
%lu   -   unsigned long
%llu  -   unsigned long long
Score: 3

No, there is convention, you simply need 10 to look at the documentation.

And the formatting specifiers are not 9 a 1:1 mapping to the type, they control 8 the output format (hence the name). There 7 can be many output formats for a single 6 input value type.

For instance, %d is "decimal", since 5 it prints an integer as a decimal number. There 4 is also %i ("integer") that does 3 exactly the same thing.

For unsigned int values, you 2 have both %x (print in hexadecimal) and %u (print 1 in octal).

Score: 3

The definition of int, long, etc are specific 43 to the target. If the size of int and long 42 match then a


wont complain, but if long 41 is say 64 bits and int is 32 bits as an 40 example, then you will get the warning/error 39 you describe. One of two solutions:


For 38 the latter of course you have to insure 37 that the compiler associates int with the 36 %d size, if you get yet another warning 35 then adjust accordingly.


The compiler 34 is trying to help you out, by worrying about 33 C library stuff which is not really the 32 business of the compiler. printf() uses 31 a variable number of arguments, which hopefully 30 you properly matched to your format string. When 29 printf sees a %d on say a 32 bit system 28 it likely will only grab the next 32 bit 27 argument. But if you had put a 64 bit integer 26 in as a parameter it may grab one half of 25 that integer, and use the other half as 24 the next item in the format string. For 23 example

unsigned long ul;
float f;

printf("%X %f\n",ul,f);

Depending on your system, endianess, etc, a 22 32 bit system you should not at all be surprised 21 if the above code produced this output:

12345678 1.234000

because 20 that is what you told it to to. you told 19 it to take the lower 32 bits of ul and print 18 that as hex (%X) and the upper 32 bits of 17 ul and print that as float (%f) and putting 16 f in the function call was a waste you didnt 15 provide any formatting to use the floating 14 point value.

Now depending on the compiler 13 on the target system on a particular day 12 you may have the above code work as desired, take 11 a system/compiler where unsigned long is 10 interpreted as a 32 bit and %X is interpreted 9 as 32 bit, then you get a warning about 8 the 64 bit assignment but the printf makes 7 a little bit more sense.

Because of this 6 pain, compilers like gcc bother to try to 5 make your life better by assuming that when 4 you use a function called printf() you are 3 using the standard C one and they parse 2 through your format string looking for these 1 types of common mistakes.

Score: 1

However I don't really understand what I 12 am doing.

What you're doing is telling the 11 printf function how to display the data that you 10 provide following the format string. You'll 9 probably find that %lo doesn't really print 8 the results you expect -- it prints the 7 right data, but in octal (base 8) format.

Is 6 there a naming convention to get the short 5 form of a type? For example, int is %d, why??

The 4 "short form" is called a format 3 specifier. There's no convention that lets 2 you derive the appropriate printf format 1 specifier. You just have to look them up in an appropriate reference.

Score: 0

%lo = unsigned long integer printed in octal 6 digits. yes, docs are a good place to start.. but 5 somethings have a pattern, like o for octal, x 4 for hex, l for anything long etc. Getting 3 used to many such format specifiers will 2 help you get the pattern wherever there 1 is.

More Related questions