chexprintfformat-specifiers

How to print hexadecimal values in "0x" format in C


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


Solution

  • There are 2 ways to achieve this: