×

C - INPUT OUTPUT


In this chapter, we will be focusing on two in-build funtions i.e scanf() and printf() to do some input-output tasks.


INPUT

Input is the data that a program needs to perform certain task and that data is given externally, means during the execution of a program we gives some data as an input to that program.

In c for taking input from user generally scanf() is used.


OUTPUT

The data that you see on the console is the output given by that program.

In c for showing something on console generally printf() is used.


Below are some examples to illustrate input-output.

Output Examples:

Integer output

File: integer_output.c
#include<stdio.h>
void main()
{
    int a;
    a = 45;
    printf("The number is %d",a); //INTEGER OUTPUT
    // To print integer on console %d is used
}
Output
The number is 45

Character output

File: char_output.c
#include<stdio.h>
void main()
{
    char a;
    a = 'd';
    printf("The character is '%c'",a); //CHAR OUTPUT
    // To print character on console %c is used
}
Output
The character is 'd'

Decimal output

File: decimal_output.c
#include<stdio.h>
void main()
{
    float a;
    a = 45.653999;
    printf("The Decimal Number is %f",a); //FLOAT OUTPUT
    // To print Floating point value on console %f is used
}
Output
The Decimal Number is 45.653999

To understand input better we are using both input-output together.

Input-Output examples

Program to add two number.

File: int_input_output.c
#include<stdio.h>
void main()
{
    int a, b, addition;

    printf("Enter first number :\n"); //OUTPUT
    scanf("%d",&a);  //INTEGER INPUT
    //To read integer value %d is used in scanf()

    printf("Enter second number : \n "); //OUTPUT
    scanf("%d",&b);  //INTEGER INPUT

    addition = a+b; //ADDITION
    printf("The addition is %d",addition); //OUTPUT

}
Output
Enter first number :
45
Enter second number :
55
The addition is 100

Input-Output examples

Program for division (Float input).

File: float_input_output.c
#include<stdio.h>
void main()
{
    float a, b, division;

    printf("Enter Dividend :\n"); //OUTPUT
    scanf("%f",&a);  //FLOAT INPUT
    //To read float value %f is used in scanf()

    printf("Enter Divisor : \n "); //OUTPUT
    scanf("%f",&b);  //FLOAT INPUT

    division = a/b; //DIVISION
    printf("The division is %f",division); //OUTPUT

}
Output
Enter Dividend :
948.69874
Enter Divisor :
69.489645
The division is 13.652375

In the next chapter, we will learn how to make decision in c.