# 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

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

Program:

```#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
```

What's New

Top Interview Coding Problems/Challenges!

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