Honestly, I do not understand why you should use% lu instead of% u, since you are working with int.
% lu should be used (in its most basic explanation) for unsigned long.
Most likely, it will print garbage if your compiler uses (and, of course, in 99% of cases) different storage sizes for int and long.
For example, according to the C standard, an unsigned int has a storage size of “At least 16 bits in size,” while an unsigned long has “At least 32 bits in size”.
Now let's take as an example 16 bits for int and 32 bits in length and let's look at an atypical example where the memory is all reset at the moment the program starts.
The value 12 is represented in memory as:
00000000 00001100
and if you type with% u, you get 12:
In place, if you specify printf to print as% lu, this will cause the memory made in printf to be as follows:
00000000 00001100 00000000 00000000
which corresponds to a long value of 786432
Edit: Passing the value of a variable into the printf function (and not the pointer (and size) of the variable) makes the code anyway. My previous “explanation” basically explained why a warning occurs and why it “usually” is the wrong approach.
Maurizio Benedetti Apr 25 '14 at 8:04 2014-04-25 08:04
source share