Input & Output Functions in 'C'

  • Input means to provide the program with some data to be used in the program
  • Output means to display data on screen or write the data to a printer or a file.
  • C programming language provides many built-in functions to read any given input and to display data on screen when there is a need to output the result.
  • In this tutorial, we will learn about such functions, which can be used in our program to take input from user and to output the result on screen.
  • All these built-in functions are present in C header file "#include<stdio.h>"
  • To use the built in functions we have to specify header files at head in which a particular function is defined(written) while discussing about it.

printf() & scanf()



printf() Function


"This Function is used to print formatted data from the keyboard,where format of string depends on the control string"


EXAMPLE 1:

#include<Stdio.h>        // header file
printf("Printf function is used to print any data");         // prints the message hello world 
            

            



Output:

Printf function is used to print any data



EXAMPLE 2

Printing Variables on Screen


#include<stdio.h>        //header  file
void main()
{
    int var1=10;
    float var2=20.26;
    char var3 = 'A';

    printf("integer variable var1 = %d\n",var1);        // %d format specifier used to print integer variable
    printf("Float variable var2 = %f\n",var2);        // %f format specifier used to print Float variable
    printf("Character variable var3 = %c\n",var3);     // %c format specifier used to print character variable
}



Output:

integer variable var1 = 10
Float variable var2 = 20.26
Character variable var3 = A






Scanf() Function


"This function is used to accept formatted data from the keyboard ,where format of string depends on the specifiers"


Syntax:


scanf("%d",&var);        // %d is specifier of integer variable which stores intger varaible


//examples 

scanf("%d",&var1);   // Accepts the decimal input from keyboard and stores to variable "var1"

scanf("%f",&var2);   // Accepts the floating number as  input from keyboard and stores to variable "var2"

scanf("%c",&var3);   // Accepts the character as input from keyboard and stores to variable "var3"

Note: Scanf() Functions Stores the Variable at certain Address.
Concept of Address we will see in "Pointer" Module,for now just remember this.

Example:

#include<stdio.h>              //header file


void main()
{
int  var1;
float var2;        //declarations of variables
char var3;

printf("\nEnter the value of integer variable:");
scanf("%d",&var1);                              //Input for integer variable.

printf("\nEnter the value of float variable:");
scanf("%f",&var2);                  //input for float variable.

printf("\nEnter the value of Character Variable:");
scanf("%c",&var3);           //input for character variable.

printf("Value of integer variable is %d\nValue of float variable is %f\nValue of character variable is %c",var1,var2,var3);
}

Output:

Enter the value of integer variable:10
Enter the value of float variable:14.2
Enter the value of Character Variable:J
Value of integer variable is 10
Value of float variable is 14.2
Value of character variable is J




Further Topics




Related Posts: