C Input Functions

C Input Functions

C programming language provides built-in functions to perform input operations.

 The input opearations are used to read user’s input from keyboard.

 C language provides us 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“.

 To use scanf() function in our program, we need to add the respective header file (stdio.h) using #include statement.( At the top of program)

 The following syntax is used for scanf() function…

Syntax:

scanf(“format strings”,&variableNames);

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

   int i;

   printf(“\nEnter any integer value: “);

   scanf(“%d”,&i);

   printf(“\nYou have entered %d number”,i);  

}

Output: Enter any integer number :35

            You have entered 35 number.

          Press any key to Continue.

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.

 Let us see another example program…

#include<stdio.h>

#include<conio.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 :

                 Integer =24 , float = 12.000000

             Press any key to continue.

In the above example program, we have 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>

#include<conio.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 : 23  21  30.89

             Total inputs read : 3

            Press any key to continue .

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…

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

   char ch;

   printf(“\nEnter any character : “);

   ch = getchar();

   printf(“\nYou have entered : %c\n”,ch);  

}

Output:  Enter any character : A

              You have entered : A

             Press any key to continue.

getch() function

The getch() function is similar to getchar function.

 This function is used to read a single character from the keyboard and also return character to the program.

 To read multiple characters we need to write the character multiple times.

We can also use a looping statement.

Let us see the following example program :—

#include<stdio.h>

#include<conio.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

             Press any key to continue .

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…

Example Program

#include<stdio.h>

#include<conio.h>

void main(){

   char name[30];

   printf(“\nEnter your favourite website: “);

   gets(name);

   printf(“%s”,name);

}

Output:  Enter your favourite website : bcaguru

             bcaguru

            Press any key to continue .

fscanf() function

The fscanf() function is used with the concept of files.

 This function is used to read data values from a file.

If we want to use fscanf() function the file must be opened in readers mode.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contribute
close slider
present

Contribute and Win Exciting Prizes & Gifts



whatsapp

You can also send file via Whatsapp

+91 9155653926

If you have any year ,any subject,any semester question paper and other study materials please send us

अगर आपके पास किसी सेमेस्टर के किसी भी साल और किसी भी विषय का क्वेश्चन पेपर है और कोई अन्य पढाई सामग्री तो उसे हमलोगों को भेजे