Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format specifier %02x

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 .

like image 598
user2717225 Avatar asked Aug 26 '13 07:08

user2717225


People also ask

What is X in printf?

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.

What is percent X in C?

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.

What does %# x mean in C?

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.

What does 02x mean in C?

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


2 Answers

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

like image 102
aragaer Avatar answered Oct 02 '22 08:10

aragaer


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

like image 21
PulkitRajput Avatar answered Oct 02 '22 07:10

PulkitRajput