How to Print a Hexadecimal Value in C (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 (1)

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:

 fc00000000fc0x000000fc 000000fc0x000000fc0x000000fc 000000000000000000000 00000000 000000000x000000fc

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
How to Print a Hexadecimal Value in C (2024)
Top Articles
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 6080

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.