Q:

Input decimal, octal and hexadecimal values in character variables using scanf() in C

belongs to collection: C scanf() Programs

0

Input decimal, octal and hexadecimal values in character variables using scanf() in C

All Answers

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

Program:

#include <stdio.h>

int main(void) 
{
	//data range of unsigned char is in,
	//1) decimal format 0 to 255
	//2) octal format 0 to 377
	//3) hexadecimal format 0 to ff
	
	unsigned char var;
	printf("Enter decimal value b/w 0 to 255: ");
	scanf("%d", &var);
	printf("var = %d\n", var);

	printf("Enter octal value b/w 0 to 377: ");
	scanf("%o", &var);
	printf("var = %o\n", var);

	printf("Enter hexadecimal value b/w 0 to ff: ");
	scanf("%x", &var);
	printf("var = %x\n", var);

	return 0;
}

Output

Enter decimal value b/w 0 to 255: 198
var = 198
Enter octal value b/w 0 to 377: 172
var = 172
Enter hexadecimal value b/w 0 to ff: f9
var = f9

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

total answers (1)

Input an integer value in any format (decimal, oct... >>
<< Input octal value using scanf() in C...