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.


Advantages of the Input and Output Program in C

  • Understanding Input and Output Operations : This program helps beginners understand the basic input and output operations in C programming. It demonstrates how to read input from the user and display output to the screen.

  • Building Block for User Interaction : The Input and Output Program serves as a building block for programs that require user interaction. It lays the foundation for reading user input, processing it, and displaying output, which are essential for interactive applications.

  • Introduction to Data Types : Writing this program introduces you to different data types in C programming, such as integers, characters, and floating-point numbers. It demonstrates how to declare variables of these data types and perform input and output operations on them.

Importance of Practicing the Input and Output Program for Beginners

  • Understanding of Data Representation : Implementing this program enhances your understanding of how different data types are represented in C programming. You will learn how to format input and output for integers, characters, and floating-point numbers.

  • Preparation for Real-World Applications : The skills and concepts you learn from this program prepare you for real-world applications that involve input and output operations. It equips you with the foundational knowledge needed to build interactive programs.

  • Development of Input and Output Skills : This program helps beginners develop essential input and output skills, which are fundamental for programming. It improves your ability to interact with the user and handle input and output operations effectively.

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
  • 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.

Important Points to Remember

  • Data Type Matching : Ensure that the format specifiers used in scanf and printf match the data type of the variables. Using the wrong format specifier can lead to undefined behavior.

  • Buffer Flushing : Be aware of the buffer flushing behavior of scanf and printf. Use fflush(stdin) to flush the input buffer before reading input if needed.

  • Format Output Appropriately : Format the output of the program to display the input and output values clearly. Use appropriate formatting options to ensure that the output is easy to read and understand.
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