Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
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

Home » C solved programs » C basic 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

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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



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 (2015-2018), Some rights reserved.