- An Overview of Hexadecimal Numbers in C Programming Language
- Format Specifiers in C Programming Language
- The Process of Conversion to a Hexadecimal Value
- Implementation of Printing Hexadecimal Values
- Use %x to Print Hexadecimal Value in Small Letters in C
- Use %X to Print Hexadecimal Value in Small Letters 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:
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.
- You first need to divide the supplied number by
16
and note the remainder of the operation. - Continue to do step 1 until the quotient equals 0.
- 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