Input Functions in C



C programming language provides built-in functions to perform input operations. The input opearations are used to read user values (input) from keyboard. C programming language provides the following built-in input functions...

  1. scanf()
  2. getchar()
  3. getch()
  4. gets()
  5. fscanf()

scanf() function

The scanf() function is used to read multiple data values of different data types from the keyboard. The scanf() function is built-in function defined in a header file called "stdio.h". When we want to use scanf() function in our program, we need to include the respective header file (stdio.h) using #include statement. The scanf() function has the following syntax...

Syntax:

scanf("format strings",&variableNames);

Example Program

#include <stdio.h>
void main(){
   int i;
   printf("\nEnter any integer value: ");
   scanf("%d",&i);
   printf("\nYou have entered %d number",i);   

}
Output:

Enter any integer value: 55
You have entered 55 number

In the above example program, we used the scanf() function to read an integer value from the keyboard and store it into variable 'i'.

The scanf function also used to read multiple data values of different or same data types. Consider the following example program...

#include <stdio.h>
void main(){
   int i;
   float x;
   printf("\nEnter one integer followed by one float value : ");
   scanf("%d%f",&i, &x);
   printf("\ninteger = %d, float = %f",i, x);   

}
Output:

Enter one integer followed by one float value : 20 30.5
integer = 20, float = 30.5

In the above example program, we used the scanf() function to read one integer value and one float value from the keyboard. Here 'i' is an integer variable so we have used format string %d, and 'x' is a float variable so we have used format string %f.

The scanf() function returns an integer value equal to the total number of input values read using scanf function.

Example Program

#include <stdio.h>
void main(){
   int i,a,b;
   float x;
   printf("\nEnter two integers and one float : ");
   i = scanf("%d%d%f",&a, &b, &x);
   printf("\nTotal inputs read : %d",i);   

}
Output:

Enter two integers and one float : 10 20 55.5
Total inputs read : 3

getchar() function

The getchar() function is used to read a character from the keyboard and return it to the program. This function is used to read only single character. To read multiple characters we need to write multiple times or use a looping statement. Consider the following example program...

#include <stdio.h>
void main(){
   char ch;
   printf("\nEnter any character : ");
   ch = getchar();
   printf("\nYou have entered : %c",ch);   

}
Output:

Enter any character : A
You have entered : A

getch() function

The getch() function is similar to getchar function. The getch() function is used to read a character from the keyboard and return it to the program. This function is used to read only single character. To read multiple characters we need to write multiple times or use a looping statement. Consider the following example program...

#include <stdio.h>
void main(){
   char ch;
   printf("\nEnter any character : ");
   ch = getch();
   printf("\nYou have entered : %c",ch);   

}
Output:

Enter any character :
You have entered : A

gets() function

The gets() function is used to read a line of string and stores it into character array. The gets() function reads a line of string or sequence of characters till a newline symbol enters. Consider the following example program...

#include <stdio.h>
void main(){
   char name[30];
   printf("\nEnter your favourite website: "); 
   gets(name);
   printf("%s",name);
}
Output:

Enter your favourite website: www.btechsmartclass.com
www.btechsmartclass.com

fscanf() function

The fscanf() function is used with the concept of files. The fscanf() function is used to read data values from a file. When you want to use fscanf() function the file must be opened in reading mode.