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

In this article, we are going to learn how to input a value in decimal, octal and hexadecimal values in a character value using scanf() in C language?
By IncludeHelp Last updated : March 10, 2024

Here, we will declare an unsigned char variable and input different format's value like decimal format, octal format and hexadecimal format.

  • To input and print decimal value – we use "%d" format specifier
  • To input and print octal value – we use "%o" format specifier
  • To input and print hexadecimal value – we use "%x" format specifier

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

C scanf() Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.