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 programs » C scanf() programs

Input a hexadecimal value using scanf() in C



Here, we are going to learn how to input a hexadecimal value using scanf() in C programming language?
Submitted by IncludeHelp, on September 12, 2018

Here, we have to declare an unsigned int variable and input a value in hexadecimal format.

To input a value in hexadecimal format – we use "%x" or "%X" format specifier and to print the value in hexadecimal format – we use same format specifier "%x" or "%X".

  • "%x" – prints value with lowercase alphabets (a to f)
  • "%X" – prints value with uppercase alphabets (A to F)

Note: In scanf(), you can use both of the format specifiers "%x" or "%X" – it does not affect to user input, but in the printf()"%x" or "%X" matters for printing alphabets in hexadecimal value (a to f or A to F).

Program 1:

#include <stdio.h>

int main(void) 
{
	unsigned int value;

	//input "123afc"
	printf("Enter hexadecimal value without \"0x\": ");
	//using %x (small x)
	scanf("%x", &value);
	printf("value = 0x%x or 0X%X\n", value, value);

	//input "123AfC"
	printf("Enter hexadecimal value without \"0X\": ");
	//using X (capital x)
	scanf("%X", &value);
	printf("value = 0x%x or 0X%X\n", value, value);

	return 0;
}

Output

Enter hexadecimal value without "0x": 123afc
value = 0x123afc or 0X123AFC
Enter hexadecimal value without "0X": 123AFC
value = 0x123afc or 0X123AFC


Program 2: Testing program with invalid hexadecimal value

#include <stdio.h>

int main(void) 
{
	unsigned int value;

	//testing with invalue value 
	//while input, we are using alphabets 
	//which are greater than F 
	//as we know, hexadecimal allowes only 
	//A to F / a to f - which are equivelant
	//to 10 to 15

	printf("Enter a hexadecimal value: ");
	scanf("%x", &value);
	printf("value = %x\n", value);

	return 0;
}

Output

Enter a hexadecimal value: 123apd
value = 123a

Explanation:

In the hexadecimal value 123apd, "p" is not a hexadecimal digit, thus, the input is valid/acceptable till valid digits. Given input 123pad is considered as 123a.






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.