How to Print a Hexadecimal Value in C

Waqar Aslam Feb 02, 2024
  1. An Overview of Hexadecimal Numbers in C Programming Language
  2. Format Specifiers in C Programming Language
  3. The Process of Conversion to a Hexadecimal Value
  4. Implementation of Printing Hexadecimal Values
  5. Use %x to Print Hexadecimal Value in Small Letters in C
  6. Use %X to Print Hexadecimal Value in Small Letters in C
How to Print a Hexadecimal Value in C

This post will discuss printing hexadecimal values using the C programming language.

An Overview of Hexadecimal Numbers in C Programming Language

A hexadecimal number is a value constructed using 16 symbols in the C programming language. These symbols include the ten normal numerical systems ranging from 0 to 9 and the six additional symbols ranging from A to F.

The hexadecimal number system in C is called the base-16 number system. It is impossible to save hexadecimal numbers in a data type such as float, long, or double; however, you can store them in an integral data type.

To input a hexadecimal value in the C programming language, you must use scanf, which provides format specifiers such as %x and %X.

A hexadecimal value is represented in the C programming language as either 0x or 0X. To print an integer number in hexadecimal format, the format specifier %x or %X must be used inside the printf() command.

The value is printed in hexadecimal format with the alphabets in lowercase when %x is used, as (a-f). The value is printed in hexadecimal format with the alphabets in uppercase when %X is used, as (A-F).

Format Specifiers in C Programming Language

If you want 0x to always be included in the output, you shouldn’t use the # symbol since it causes 0x to be prepended to the output even if the value is 0.

Producing leading zeroes may be accomplished by combining the width field with the 0 flags in the following manner: the value of %08x adds leading zeroes to the integer so that it has a width of 8.

There is also the possibility of using the precision field, which reads as follows: %.8x pads the integer with leading zeroes to make it a total of 8 digits. Therefore, 0x%.8x is another option for you to consider for your goal.

If a prefix is formed as part of the conversion, such as 0x for # or - for negative integers in signed conversions, these conversion requirements will be different. The length of the prefix will be counted for the width, but it will not be counted for the accuracy specifier.

Here’s an example of how various specifiers may be used to provide various output styles for hexadecimal numbers.

#include <stdio.h>

int main() {
  printf("%10x\n", 252);
  printf("%010x\n", 252);
  printf("%#010x\n", 252);

  printf("%10.8x\n", 252);
  printf("%#10.8x\n", 252);
  printf("0x%.8x\n", 252);

  printf("%10x\n", 0);
  printf("%010x\n", 0);
  printf("%#010x\n", 0);

  printf("%10.8x\n", 0);
  printf("%#10.8x\n", 0);
  printf("0x%.8x", 252);
}

Output:

        fc
00000000fc
0x000000fc
  000000fc
0x000000fc
0x000000fc
         0
0000000000
0000000000
  00000000
  00000000
0x000000fc

The Process of Conversion to a Hexadecimal Value

Now, let’s look at how the system does this conversion so we can fully comprehend it.

  1. You first need to divide the supplied number by 16 and note the remainder of the operation.
  2. Continue to do step 1 until the quotient equals 0.
  3. Combine the remaining components in the opposite sequence in which they were acquired.

Implementation of Printing Hexadecimal Values

In this example, for instance, when we divide 252 by 16, we get 15 as the quotient and 12 as the remainder. Now, we will divide 15 by 16; the quotient will be 0, while the remainder will be 15.

It is important to note that when 15 is divided by 16, the resulting quotient is 0. So, here is where we stop calculating.

The two remainders acquired after the first two stages are 12 and 15, and their corresponding representations in hexadecimal are c and f. The hexadecimal representation of remainders is organized here in the opposite direction of how they were produced.

Therefore, the value 252 will be represented in hexadecimal as fc in this instance.

Use %x to Print Hexadecimal Value in Small Letters in C

The hexadecimal value will be printed using the %x format modifier in tiny letters. Like, ff, ffd, etc.

Let us look at the following example to see how the %x format specifier works.

The first step is to import the necessary libraries and construct a main() function.

Next, create a variable with the datatype int and name it integerValue. Then, give it an integer value that will be converted into the corresponding hexadecimal value.

Then, we need to convert the integer value to hexadecimal, which the %x specifier will automatically accomplish for us and print the value in small letters.

Code:

#include <stdio.h>

int main() {
  int integerValue = 252;
  printf("The converted value in hexadecimal is: %x\n", integerValue);
  return 0;
}

Output:

The converted value in hexadecimal is: fc

Use %X to Print Hexadecimal Value in Small Letters in C

In this example, we will use %X in the print statement to output the value in capital letters.

#include <stdio.h>

int main() {
  int integerValue = 252;
  printf("The converted value in hexadecimal is: %X\n", integerValue);
  return 0;
}

Output:

The converted value in hexadecimal is: FC
Author: Waqar Aslam
Waqar Aslam avatar Waqar Aslam avatar

I am Waqar having 5+ years of software engineering experience. I have been in the industry as a javascript web and mobile developer for 3 years working with multiple frameworks such as nodejs, react js, react native, Ionic, and angular js. After which I Switched to flutter mobile development. I have 2 years of experience building android and ios apps with flutter. For the backend, I have experience with rest APIs, Aws, and firebase. I have also written articles related to problem-solving and best practices in C, C++, Javascript, C#, and power shell.

LinkedIn