1. C program to Input integer, float and character values using one scanf().
#include <stdio.h>
/* C program to Input integer, float and character values using scanf(). */
int main()
{
int inputInt;
char inputChar;
float inputFloat;
/* Take input from user using scanf function */
printf("Enter Integer,Character and Float number\n");
scanf("%d %c %f", &inputInt, &inputChar,
&inputFloat);
printf("\nInteger you have entered is : %d", inputInt);
printf("\nCharacter you have entered is : %c", inputChar);
printf("\nFloating point number you have entered is : %f",
inputFloat);
return 0;
}
Output:
The program displays the following output:
Enter Integer, Character and Float number
15
C
2.500
Integer you have entered is : 15
Character you have entered is : C
Floating point number you have entered is : 2.500000
Explanation:
This C program shows how to get input from the user for three different types of data: whole numbers, single letters/symbols, and numbers with decimals.
Here's how it works, step by step:
Setting up space to store the input (Variable Declaration):
int inputInt: This line creates a space in the computer's memory called inputInt. The word int tells the computer that this space will hold a whole number (like 15, -10, or 0).
char inputChar: This line creates a space called inputChar. The word char says that this space will hold a single character (like 'C', 'a', or '5').
float inputFloat This line creates a space called inputFloat. The word float means that this space will hold a number that can have a decimal part (like 2.500, 3.14, or -0.01). We use float for these kinds of numbers.
Getting input from the user (using scanf()):
printf("Enter Integer, Character and Float number\n");:
This line displays a message on the screen, asking the user to type in the three values. The \n makes the next thing the user types appear on a new line.
scanf("%d %c %f", &inputInt, &inputChar, &inputFloat);:
This is where the program actually reads what the user types.
scanf() is a built-in C function that reads input.
"%d %c %f" tells scanf() what kind of input to expect:
%d: Expects a whole number.
%c: Expects a single character.
%f: Expects a number with a decimal.
&inputInt, &inputChar and &inputFloat tell scanf() where to store the input.
The & symbol is very important it gives scanf() the location in the computer's memory where the variables are.
For example, &inputInt tells scanf() where the inputInt variable is located.
When the user types in the data (for example, "15 C 2.500") and presses Enter, scanf() does the following:
Reads "15" and stores it in the inputInt space.
Reads "C" and stores it in the inputChar space.
Reads "2.500" and stores it in the inputFloat space.
Showing the user what was entered (using printf()):
The final three printf() lines show the user the values that were entered:
printf("\nInteger you have entered is : %d", inputInt);:
Displays the whole number that the user typed in. The \n starts a new line.
printf("\nCharacter you have entered is : %c", inputChar);:
Displays the single letter or symbol that the user typed in. The \n starts a new line.
printf("\nFloating point number you have entered is : %f", inputFloat);:
Displays the number with a decimal that the user typed in. The \n starts a new line.
The %f usually shows six digits after the decimal point.