×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

Input hexadecimal value in C language

Here, we are going to learn how to input a value in hexadecimal format? To input value in hexadecimal format using scanf() – we use "%x" or "%X" format specifier.
Submitted by IncludeHelp, on September 06, 2018

Problem statement

In C programming language, we are able to input a hexadecimal value using scanf() function, for that – we use %x or %X format specifier.

Syntax

 scanf("%x", &variable_name);

Example

Input:
Input a hex value: FA

Output:
Value in hexadecimal format: FA, Decimal format: 250

Input hexadecimal value in C language

#include <stdio.h>

int main()
{
    unsigned char var1;
    unsigned short var2;
    unsigned int var3;
	
    printf("Enter 1 byte  value in Hexadecimal: ");
    scanf("%x", &var1);
    printf("Enter 2 bytes value in Hexadecimal: ");
    scanf("%x", &var2);
    printf("Enter 4 bytes value in Hexadecimal: ");
    scanf("%x", &var3);	
	
    printf("Value of var1 in HEX: %X, Decimal: %d\n", var1,var1);
    printf("Value of var2 in HEX: %X, Decimal: %d\n", var2,var2);
    printf("Value of var3 in HEX: %X, Decimal: %d\n", var3,var3);
	
    return 0;
}

Output

Enter 1 byte  value in Hexadecimal: FA
Enter 2 bytes value in Hexadecimal: 20AC
Enter 4 bytes value in Hexadecimal: 1234ABD0
Value of var1 in HEX: FA, Decimal: 250
Value of var2 in HEX: 20AC, Decimal: 8364
Value of var3 in HEX: 1234ABD0, Decimal: 305441744

C Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





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