How to get uppercase hex in printf()

198 Views Asked by At

I am experimenting with the following code. It outputs a in hex, sa in decimal. I cant figure out how to “Upper Case” the value for a as “F”.

#include <stdio.h>

Void foo()
{
    int a = 10;
    static int sa = 10;
    a += 5;
    sa += 5;
    printf("a = %x, sa = %d\n", a, sa);
}


int main()
{
    int i;
    for (i = 0; i < 10; ++i)
        foo();
}
1

There are 1 best solutions below

2
chqrlie On

To convert integers to hexadecimal format in uppercase, use %X. Note that the 0x prefix output for non zero values for %#x will also be uppercased with %#X

Similarly, you can use %E, %F, %G and %A to convert floating point numbers using uppercase letters instead of lowercase.

C23 added binary conversion with %b, and if used with the # modifier printf outputs a 0b prefix for non zero values. %#B can be used to produce a 0B prefix instead.