- Write a program in c to take an Integer, Character and Float as input using scanf and print them using printf function.
To understand this program, you should have knowledge of Input and Output in C
Input/Output in C can be achieved using scanf() and printf() functions. The printf and scanf are two of the many functions found in the C standard library. These functions are declared and related macros are defined in stdio.h header file. The printf function is used to write information from a program to the standard output device whereas scanf function is used to read information into a program from the standard input device.
Function Prototype of printf and scanf in C
Function name | Function prototype |
---|---|
printf | int printf(const char* format, …); |
scanf | int scanf(const char* format, …); |
Format Specifier of printf and scanf Functions
Format specifier | Description |
---|---|
%d | Signed decimal integer |
%u | Unsigned decimal integer |
%f | Floating point numbers |
%c | Character |
%s | Character String terminated by ‘\0’ |
%p | Pointer address |
C Program to read and print an Integer, Character and Float using scanf and printf function
This program takes an integer, character and floating point number as input from user using scanf function and stores them in ‘inputInteger’, ‘inputCharacter’ and ‘inputFloat’ variables respectively. Then it uses printf function with %d, %c and %f format specifier to print integer, character and floating point number on screen respectively.
/* * C program to take Integer, Character, Float as inputs using scanf * and then prints it using printf */ #include <stdio.h> #include <conio.h> int main(){ int inputInteger; char inputCharacter; float inputFloat; /* Take input from user using scanf function */ printf("Enter an Integer, Character and Floating point number\n"); scanf("%d %c %f", &inputInteger, &inputCharacter, &inputFloat); /* Print Integer, Character and Float using printf function */ printf("\nInteger you entered is : %d", inputInteger); printf("\nCharacter you entered is : %c", inputCharacter); printf("\nFloating point number you entered is : %f", inputFloat); getch(); return 0; }
Program Output
Enter an Integer, Character and Floating point number 5 A 2.542 Integer you entered is : 5 Character you entered is : A Floating point number you entered is : 2.542000
Points to Remember
- We use “\n” in printf() to generate a newline.
- C language is case sensitive. So, printf() and scanf() are different from Printf() and Scanf().
- You can use as many format specifier as you wish in your format string. You must provide a value for each one separated by commas.
- Program stops running at each scanf call until user enters a value.
- Ampersand is used before variable name “var” in scanf() function as &var. It is just like in a pointer which is used to point to the variable.