Home » C programming language

Working with Hexadecimal values in C programming language

Hexadecimal value has 16 alphanumeric values from 0 to 9 and A to F, with the base 16. (Read more about Computer number systems), here we will learn how to work with hexadecimal values in c programming language?

Representation of Hexadecimal numbers in C programming

In C programming language, a Hexadecimal number is represented by preceding with "0x" or "0X", thus the value in Hexadecimal can be written as "0x64" (which is equivalent to 100 in Decimal).

Assigning the Hexadecimal number in a variable

There is no special type of data type to store Hexadecimal values in C programming, Hexadecimal number is an integer value and you can store it in the integral type of data types (char, short or int).

Let suppose, we have two values in Hexadecimal "64" (100 in Decimal) and "FAFA" (64250 in Decimal).

We are storing "64" in an unsigned char variable (64 is small value and can be stored with in a Byte) and "FAFA" in the int variable.

Consider the following statements

unsigned char a=0x64;
unsigned char b=0xFAFA;

Printing the number in Hexadecimal format

To print integer number in Hexadecimal format, "%x" or "%X" is used as format specifier in printf() statement.

"%x" prints the value in Hexadecimal format with alphabets in lowercase (a-f).

"%X" prints the value in Hexadecimal format with alphabets in uppercase (A-F).

Consider the code, which is printing the values of a and b using both formats

int main()
	unsigned char a=0x64;
	int b=0xFAFA;
	printf("value of a: %X [%x]\n",a,a);
	printf("value of b: %X [%x]\n",b,b);
	return 0;


    value of a: 64 [64] 
    value of b: FAFA [fafa] 

Reading value in Hexadecimal format

"%x" or "%X" is used with scanf() statement to read the value from the user.

Consider the following code

#include <stdio.h>
int main()
	unsigned char a;
	int b;
	printf("Enter value of a: ");
	printf("Enter value of b: ");
	printf("Value of a: Hex: %X, Decimal: %d\n",a,a);
	printf("Value of b: Hex: %X, Decimal: %d\n",b,b);
	return 0;


    Enter value of a: 64
    Enter value of b: FAFA
    Value of a: Hex: 64, Decimal: 100 
    Value of b: Hex: FAFA, Decimal: 64250

Declaring integer array by assigning hexadecimal values

Consider the following example, where integer array is declaring with the Hexadecimal values and printing in both formats Decimal and Hexadecimal.

#include <stdio.h>
int main()
	int arr[]={0x64, 0xAB0, 0xA0A0, 0xFAFA, 0x100};
	int i;
	printf("Array elements are\n");
		printf("Decimal: %d, Hex: %X\n",arr[i],arr[i]);
	return 0;


    Array elements are
    Decimal: 100, Hex: 64 
    Decimal: 2736, Hex: AB0 
    Decimal: 41120, Hex: A0A0 
    Decimal: 64250, Hex: FAFA 
    Decimal: 256, Hex: 100


Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.