Want your C program to intelligently read numbers, whether they're standard decimal, octal (starting with 0) or hexadecimal (starting with 0x)? The %i format specifier in scanf() makes it easy! This C program demonstrates how to input integer values in any of these formats, showcasing C input flexibility and number base detection.
Here's the C code example:
Master Flexible Integer Input in C with the %i Specifier
#include <stdio.h>
int main() {
int number; // Sets up a spot to hold a whole number.
printf("--- Inputting Integers with the Smart '%%i' Specifier ---\n\n");
// Try typing a decimal number (like 123)
// Or an octal number (like 0123 - which is 83 in decimal)
// Or a hexadecimal number (like 0x123 - which is 291 in decimal)
printf("Enter an integer value (decimal, octal with '0' prefix, or hex with '0x' prefix): ");
// The magic happens here! '%i' automatically figures out the number base.
if (scanf("%i", &number) != 1) {
printf("Oops! That wasn't a valid integer. Please try again.\n");
// Clears out any leftover junk in the input buffer.
while (getchar() != '\n');
return 1; // Ends the program with an error.
}
// Shows the number as a regular decimal.
printf("You entered: %d (decimal)\n\n", number);
// Let's try again with a different format!
printf("Enter another integer value (decimal, octal with '0' prefix, or hex with '0x' prefix): ");
if (scanf("%i", &number) != 1) {
printf("Oops! That wasn't a valid integer. Please try again.\n");
while (getchar() != '\n');
return 1;
}
printf("You entered: %d (decimal)\n\n", number);
// And one more time!
printf("Enter a final integer value (decimal, octal with '0' prefix, or hex with '0x' prefix): ");
if (scanf("%i", &number) != 1) {
printf("Oops! That wasn't a valid integer. Please try again.\n");
while (getchar() != '\n');
return 1;
}
printf("You entered: %d (decimal)\n\n", number);
return 0; // Program finished successfully.
}
Output:
See It Work (Example Output)
Run this program, and it will ask for numbers three times.
Here's how it might look:
Inputting Integers with the Smart '%i' Specifier
Enter an integer value (decimal, octal with '0' prefix, or hex with '0x' prefix): 100
You entered: 100 (decimal)
Enter another integer value (decimal, octal with '0' prefix, or hex with '0x' prefix): 0100
You entered: 64 (decimal)
Enter a final integer value (decimal, octal with '0' prefix, or hex with '0x' prefix): 0x100
You entered: 256 (decimal)
Notice how 0100 (octal) is read as 64 decimal, and 0x100 (hexadecimal) is read as 256 decimal. The %i specifier handles the conversion for you!
How Each Part Works
Let's quickly break down this C program for flexible integer input:
#include <stdio.h>: Brings in standard C functions like printf() and scanf().
int number;: Sets up an int variable to hold whole numbers.
The Magic of scanf("%i", &number);: This is the core. printf() asks for input.
scanf() with %i intelligently reads your number: as decimal (no prefix), octal (with 0 prefix), or hexadecimal (with 0x or 0X prefix).
The &number tells scanf() where to store the value.
Showing Results (printf("You entered: %d (decimal)\n\n", number);): Displays the number's decimal equivalent after scanf() processes your input.
This program is a great example of C input flexibility, showing how to read integers in various formats using a single scanf() specifier. A practical C code example for understanding number base detection in C.
Basic and Operators practice program assignments in C Language :
Practice/Assignment set 1:
1.Write a program in C to Input integer, float and character values using one scanf().
2.Write a program in C language to find the Area of Circle.
3.Write a program in C language to Input an integer value and print with padding by Zeros in C
4. Write a program in C language to Input float value and print it with specified digit after decimal point in C
5.Write a program in C language to find sum of the digits of any three digit number.
6. Write a program in C language to Input an unsigned integer value using scanf().
7.Write a program in C language to Find area of Triangle
8.Write a program in C language to Input a hexadecimal value using scanf() in C
9.Write a program in C language to Input octal value using scanf()
10.Write a program in C language to Convert temperature given in farenheight to degree celcius.
11.Write a program in C language to Input octal value using scanf()
12.Write a program in C language to Input decimal, octal and hexadecimal values in character variables using scanf() in C
13.Write a program in C language to Input an integer value in any format (decimal, octal or hexadecimal) using '%i' in C
14.Write a program in C language to Input side value and find area of Square.
15.Write a program in C language to Input individual characters using scanf() in C
16.Write a program in C language to Read a memory address using scanf() and print its value in C
17.Write a program in C language to Skip characters while reading integers using scanf() in C
18.Print your name, date of birth, and mobile number
19. Write a C program to convert specified days into years, weeks and days.
Note: Ignore leap year.
21.Write a C program that accepts two item's weight and number of purchases (floating point values) and calculates their average value.
22.Write a C program that accepts an employee's ID, total worked hours in a month and the amount he received per hour. Print the ID and salary (with two decimal places) of the employee for a particular month.
Test Data :
Input the Employees ID(Max. 10 chars): 0342
Input the working hrs: 8
Salary amount/hr: 15000
Expected Output:
Employees ID = 0342
Salary = U$ 120000.00
23.Write a C program that accept an students roll number and six subject markes, find total and average marks.
24.Write a C program that accepts an employee's ID, basic salary in a month and calculate total salary.
The formulat for
Total salary=(basic salary-tax)+House Rent allowance+Bonus+Traveling allowance+other allowance
tax 10% of basic salary
House Rent allowance 17% of basic salary
Bonus 7% of basic salary
Traveling allowance 8% of basic salary
other allowance 10% of basic salary
25.Write a C program to convert a given integer (in days) to years, months and days, assuming that all months have 30 days and all years have 365 days.
Test Data :
Input no. of days: 2535
Expected Output:
6 Year(s)
11 Month(s)
15 Day(s)
Practice/Assignment set 2:
1. C program to find sum of the square of all natural numbers from 1 to N.
Series: 1^2+2^2+3^2+4^2+..N^2
2. C program to find sum of the all natural numbers from 1 to N.
Series: 1+2+3+4+..N
3.C program to find the sum of Natural Number/Factorial of Number of all natural numbers from 1 to N.
Series: 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
4) C program to find sum of following series:
1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
5) C program to find sum of following series:
1 + 3^2/3^3 + 5^2/5^3 + 7^2/7^3 + ... till N terms