I have a simple program :
#include <stdio.h> int main() { long i = 16843009; printf ("%02x \n" ,i); }
I am using %02x
format specifier to get 2 char output, However, the output I am getting is:
1010101
while I am expecting it to be :01010101
.
Functions belonging to the printf function family have the type specifiers "%p" and "%x". "x" and "X" serve to output a hexadecimal number. "x" stands for lower case letters (abcdef) while "X" for capital letters (ABCDEF). "p" serves to output a pointer.
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.
so for %#x the value is simply prefixed with 0x . Where %x would yield 34ab , %#x would yield 0x34ab . Follow this answer to receive notifications.
%02x means print at least 2 digits, prepend it with 0 's if there's less. In your case it's 7 digits, so you get no extra 0 in front. Also, %x is for int, but you have a long. Try %08lx instead.
%02x
means print at least 2 digits, prepend it with 0
's if there's less. In your case it's 7 digits, so you get no extra 0
in front.
Also, %x
is for int, but you have a long. Try %08lx
instead.
%x
is a format specifier that format and output the hex value. If you are providing int or long value, it will convert it to hex value.
%02x
means if your provided value is less than two digits then 0
will be prepended.
You provided value 16843009
and it has been converted to 1010101
which a hex value.
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