I have tried googling the first one for %lx, but I have no good results, BUT I have successfully searched up %ld which is just long int. Necessary for printing addresses I guess, but what is %lx for?
This is where I am confused:
int main()
{
int value = 25;
int *pointer = &value;
printf("%ld\n", pointer); // prints out the address of variable value( I hope)
printf("0x%lx\n", pointer); // Completely confused here, is this perhaps address in hex?
}
Would be awesome if someone can clear this confusion I am having!
I have ran this code, and I have the results, but I am still not sure what the lx does..I have seriously tried googling this "%lx" in google, but no results explaining it.
Edit: if I use 'p' to print address then have I been wrong in thinking %ld prints address? Confused.
%lx means to print an argument of type unsigned long in hexadecimal. (There is no format to print a signed argument in hexadecimal.)
%d is for signed int, Use %u specifically for unsigned int. Long and int types are same. %ld is for long int. printf Type Field Characters (CRT)
Here we will see what are the differences between %p and %x in C or C++. The %p is used to print the pointer value, and %x is used to print hexadecimal values. Though pointers can also be displayed using %u, or %x. If we want to print some value using %p and %x then we will not feel any major differences.
%d is a signed integer, while %u is an unsigned integer. Pointers (when treated as numbers) are usually non-negative. If you actually want to display a pointer, use the %p format specifier.
You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.
To print a pointer with printf, you should cast the pointer to void *
and use "%p"
.
We can talk about the difference between "%ld"
and "%lx"
when trying to print integers. %ld
expects a variable of type long int
, and %lx
expects a variable of type long unsigned int
.
More or less though, The difference between x
, o
, d
and u
are about how numbers are going to be printed.
x
prints an unsigned number in hexadecimal.o
prints an unsigned number in octal.u
prints an unsigned number in decimal.d
prints a signed number in decimal.i
prints a signed number in decimal.We can then attach l
to the format string for formats like %lx
to specify that instead of an int
, we're using a long int
(That is, an unsigned long int
, or long int
).
There is a table at cppreference that has additional information: http://en.cppreference.com/w/c/io/fprintf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With