×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

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...

C Language Tutorial »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.