Home » C programming language

Hexadecimal (hex) Literals in C programming Language

Hexadecimal literals in C language: Here, we are going to learn about to hexadecimal literals in C language, how to use hexadecimal literals, how to assign hexadecimal literal to a variable etc?
Submitted by IncludeHelp, on February 11, 2019

Hexadecimal literals in C

Hexadecimal numbers are the technique to represent the numbers which uses the base-16 number; it uses 16 alphanumeric digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A/a, B/b, C/c, D/d, E/e, F/f. (Read: Computer number systems)

In C programming language, we can use hexadecimal literals in any expressions; we can assign hexadecimal numbers to the variables. To use hexadecimal literals, we use 0X or 0x as a prefix with the number. For example 0x10 is a hexadecimal number, which is equivalent to 16 in the decimal number system.

Printing hexadecimal number in decimal format

To print a hexadecimal number in decimal format, we use %X or %x format specifier.

Printing hexadecimal number in hexadecimal format

To print an hexadecimal number or other type of numbers in hex format, we use %X or %x format specifier.

#include <stdio.h>
int main(){
	
    //printing hexadecimal number in decimal format
    printf("%d\n", 0x10);
    //printing hexadecimal number in hexadecimal format 
    printf("%X\n", 0x10);
    //printing other format number in hexadecimal format
    printf("%X\n", 16);
	
    return 0;
}

Output

16
10
10

Using hexadecimal literals in the expressions

#include <stdio.h>
int main(){
	
    //adding two hexadecimal numbers
    printf("%d\n", (0x10+0x20));
	//adding hexadecimal, decimal numbers 
    printf("%d\n", (0x10+0x20+30+40));
    
    return 0;
}

Output

48
118

Assigning hexadecimal literals to the variables

#include <stdio.h>
int main(){
	
    int a = 0x10;
    int b = 0x76541;
    
    printf("a in hexadecimal: %x, and in decimal: %d\n", a, a);
    printf("b in hexadecimal: %x, and in decimal: %d\n", b, b);
    
    return 0;
}

Output

a in hexadecimal: 10, and in decimal: 16
b in hexadecimal: 76541, and in decimal: 484673

Input a number in hexadecimal format

To input a number in an hexadecimal format, we use %X or %x format specifier in the scanf() function

#include <stdio.h>
int main(){
	
    int a;
    
    printf("Enter value of a in hexadecimal: ");
    scanf("%x", &a);
    printf("a in hex: %X, and in decimal: %d\n", a, a);
    
    return 0;
}

Output

Enter value of a in hexadecimal: 0192Af
a in hex: 192AF, and in decimal: 103087

Error while using an invalid digit with hexadecimal literal

If we use any other digits except except 0-9 and A-F or a-f, program returns a compilation error "invalid digit 'x' in integer constant"

#include <stdio.h>
int main(){
	
    int a = 0x10A;
    int b = 0x129AG;
    
    printf("a in hexadecimal: %X, and in decimal: %d\n", a, a);
    printf("b in hexadecimal: %X, and in decimal: %d\n", b, b);
    
    return 0;
}

Output

prog.c: In function 'main':
prog.c:3:13: error: invalid suffix "G" on integer constant
     int b = 0x129AG;
             ^

Read more...






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.