Q:

Input hexadecimal value in C language

0

Input hexadecimal value in C language

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now