I am trying to print hexadecimal values in C.
A simple known program to print hexadecimal value...( Using %x or %X format specifier)
#include<stdio.h>
int main()
{
unsigned int num = 10;
printf("hexadecimal value of %u is %x\n", num, num);
}
Output: hexadecimal value of 10 is a
Instead of getting output as a, I need to get output as 0xa in hexadecimal format (making if more explicit for the user that the value in expresses in hexadecimal format).
%#xalso works: see en.cppreference.com/w/c/io/fprintfo, u, x, Xare documented. It says The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation. The#modifier is documented above that, under "flag characters".%dexpects a signedint. You could use%uinstead of%d, which prints an unsigned decimal value. So you could dounsigned int num = 10;thenprintf("%u %#x\n", num, num);That would be strictly correct, since both format specifiers expect anunsigned int.