C what is short form for long unsigned int

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, I understand that% lo fixes the warning. However, I do not quite understand what I am doing.

Is there a naming convention for getting a short form type? For example, int is% d, why?

Thank!

+5
source share
5 answers

However, I do not quite understand what I am doing.

What you do tells the function printfhow to display the data you provide after the format string. You will probably find that it %lodoes not print the expected results - it prints the necessary data, but in octal (base 8) format.

Is there a naming convention for getting a short form type? For example, int is% d, why?

" " . , printf. .

+1
long unsigned int = unsigned long

%lu . ​​

, %llu unsigned long long.


%d    -   int
%u    -   unsigned
%ld   -   long
%lld  -   long long
%lu   -   unsigned long
%llu  -   unsigned long long
+22

, , .

1:1, ( ). .

, %d "", . %i ( "integer" ), .

unsigned int %x ( ), %u ( ).

+3

int, long .. . int long ,

printf("%d",var); 

, , 64 int - 32 , /, . :

printf("%ld",var);
or
printf("%d",(int)var);

, , , int % d, , .

EDIT:

, C, . printf() , , , . printf % d, , 32- , , 32- . 64- , .

unsigned long ul;
float f;

f=3.4;
ul=0x3F9DF3B612345678;
...
printf("%X %f\n",ul,f);

, endianess .., 32- , :

12345678 1.234000

. , 32 ul hex (% X) 32 ul , float (% f) f , , .

, , , /, unsigned long 32 , % X 32 , 64 , printf .

Because of this pain, compilers like gcc try to make your life better, assuming that when you use the printf () function, you use the standard C-unit and they parse your format string looking for these types of common errors.

+3
source

% lo = unsigned long integer printed in octal digits. yes, documents are a good place to start .. but something has a pattern, like o for octal, x for hex, l for something long, etc. Getting used to many of these format specifiers will help you get the template wherever you are.

0
source

All Articles