How to display hexadecimal numbers in C? How to display hexadecimal numbers in C? c c

How to display hexadecimal numbers in C?


Try:

printf("%04x",a);
  • 0 - Left-pads the number withzeroes (0) instead of spaces, wherepadding is specified.
  • 4 (width) - Minimum number ofcharacters to be printed. If thevalue to be printed is shorter thanthis number, the result is right justified within this width by padding on the left with the pad character. By default this is a blank space, but the leading zero we used specifies a zero as the pad char.The value is not truncated even if the result islarger.
  • x - Specifier for hexadecimalinteger.

More here


i use it like this:

printf("my number is 0x%02X\n",number);// output: my number is 0x4A

Just change number "2" to any number of chars You want to print ;)


Your code has no problem. It does print the way you want. Alternatively, you can do this:

printf("%04x",a);