Format specifier %02x

user2717225 picture user2717225 · Aug 26, 2013 · Viewed 124.3k times · Source

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 .

Answer

aragaer picture aragaer · Aug 26, 2013

%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.