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 an integer value in any format (decimal, octal or hexadecimal) using '%i' in C



Here, we are going to learn how to input an integer value in any format like decimal, octal or hexadecimal value using '%i' format specifier in C language?
Submitted by IncludeHelp, on September 13, 2018

We know that the decimal, octal, and hexadecimal value can be read through scanf() using "%d", "%o" and "%x" format specifier respectively.

  • "%d" is used to input decimal value
  • "%o" is used to input integer value in an octal format
  • "%x" is used to input integer value in hexadecimal format

But, there is the best way to read the integer value in any format from decimal, octal and hexadecimal - there is no need to use different format specifiers. We can use "%i" instead of using "%d", "%o" and "%x".

"%i" format specifier

It is used to read an integer value in decimal, octal or hexadecimal value.

  • To input value in decimal format - just write the value in the decimal format, example: 255
  • To input value in octal format - just write the value in octal format followed by "0", example: 03377
  • To input value in hexadecimal format – just write the value in hexadecimal format followed by "0x", example: 0xff

Program:

#include <stdio.h>

int main(void)
{
	int num;
	
	printf("Enter value: ");
	scanf("%i", &num);
	printf("num = %d\n", num);
	
	return 0;
}

Output

Run1: Reading value in decimal format
Enter value: 255
num = 255

Run2: Reading value in octal format
Enter value: 0377
num = 255 

Run3: Reading value in hexadecimal format
Enter value: 0xFF
num = 255 





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.