Home » C programs » C scanf() programs

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?
Submitted by IncludeHelp, on September 13, 2018

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







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 some rights reserved.