C Program for Input and Output of Integer, Character and Floating Point Numbers

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.

#include <stdio.h>

int main(){
    int inputInteger;
    char inputCharacter;
    float inputFloat;
    
    /* Take input from user using scanf function */
    printf("Enter Integer,Character and Float number\n");
    scanf("%d %c %f", &inputInteger, &inputCharacter,
        &inputFloat);
    
    printf("\nInteger you entered is : %d", inputInteger);
    printf("\nCharacter you entered is : %c", inputCharacter);
    printf("\nFloating point number you entered is : %f",
        inputFloat);
    
    return 0;
}
Output
Enter Integer,Character and Float 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.
Related Topics
C program to read and print string
C program for addition, subtraction, multiplication, division and modulus of two numbers
C program to convert string to integer
C program to convert lowercase string to uppercase
C program to add digits of a number
C program to check whether an alphabet is a vowel or consonant
C Program to print fibonacci series
List of all C programs