Q:

Input an integer value in any format (decimal, octal or hexadecimal) using \'%i\' in C

belongs to collection: C scanf() Programs

0

Input an integer value in any format (decimal, octal or hexadecimal) using '%i' in C

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

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)
{
	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 

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

total answers (1)

Input individual characters using scanf() in C... >>
<< Input decimal, octal and hexadecimal values in cha...