HOWTO · C

How to Print a Hexadecimal Value in C

This article demonstrates how to print a hexadecimal value in C Programming Language.

On this page

To print an integer in hexadecimal in C, pass it to printf with %x for lowercase digits or %X for uppercase digits. For example, printf("%x", 252); displays fc.

Hexadecimal is a base-16 representation of an integer. It uses the digits 0–9 and the letters a–f (or A–F); it is a way to display the value, not a separate C data type. Store the number in an appropriate integer type, then let the printf conversion specifier choose its displayed representation. The argument type must match the conversion; %x and %X expect an unsigned int argument after the usual integer promotions.

scanf is a separate input operation. It can read hexadecimal input, but it is not needed when you already have a value to print.

Choose Lowercase or Uppercase Hexadecimal Output

Use %x when you want alphabetic hexadecimal digits in lowercase, such as fc. Use %X for uppercase letters, such as FC. Both conversions represent the same integer; only the letter case changes. In a format string, place the conversion where you want the number to appear, and provide the matching integer argument to printf.

Add a Prefix, Width, or Leading Zeros

The # flag requests an alternative form: for a nonzero hexadecimal value it adds 0x with %x or 0X with %X. For zero, it does not add a prefix. A field width is a minimum total output width, including any prefix; for example, %10x right-aligns the digits in a field of at least ten characters. Add the 0 flag, as in %010x, to pad with zeroes on the left when precision is omitted.

Integer precision sets a minimum number of hexadecimal digits. For example, %.8x produces at least eight digits, padding with leading zeroes. When precision is specified, the 0 flag is ignored. Combining # and a width or precision lets you control the prefix and digits together; for example, %#10.8x requests the alternate form and at least eight digits within a minimum width of ten.

Convert a Decimal Integer to Hexadecimal

To convert 252 by hand, divide by 16 and record each remainder: 252 gives quotient 15 and remainder 12; 15 gives quotient 0 and remainder 15. Read the remainders in reverse order. Remainder 15 maps to f and remainder 12 to c, so 252 is fc in hexadecimal. printf performs this conversion for a matching integer argument, so manual division is useful for understanding the result rather than a required step in a program.

Compile and Run a Complete Example

This complete C program prints the same value in both letter cases, then shows width, zero padding, alternate form, and precision. The example is unexecuted here, so no observed output is included; compile it with a C compiler to see the formatting on your system.

#include <stdio.h>

int main(void) {
    unsigned int value = 252;

    printf("lowercase: %x\n", value);
    printf("uppercase: %X\n", value);
    printf("width:     [%10x]\n", value);
    printf("zeroes:    [%010x]\n", value);
    printf("prefix:    [%#x]\n", value);
    printf("precision: [%.8x]\n", value);
    printf("zero with alternate form: [%#x]\n", 0u);

    return 0;
}