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?
By IncludeHelp Last updated : March 10, 2024
Input an integer value in decimal, octal or hexadecimal formats
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
C scanf() Programs »