If we use two scanf function in our source code to take two different types of values from user then compiler show some error that is -
First scanf function take the input from user but in second scanf function program will not stop to take value from user .
This error comes because When we input first number, then 3 input goes on standard input stream i.e. number,\n,\0
Process context read the input and also the \n , after reading the \n it thinks that this is the end of input and control goes to second scanf function.
\0 is remaining at input stream so at second scanf function process context take \0 as input and thinks that this is the end of input .
It only print (enter the character) but doesn't stop to take input from user and move forward in the source code and print only integer value which are input by first scanf function.
To remove this error permanent we use getchar() in our source code just after the first scanf , getchar() only print one input at a time and it flush the \0 from the input stream so the input stream is empty now .
At second scanf function process context take new input from the user and provide accurate result of source code.
getchar() can take many characters as input ,it will store all input until it didn't get \n as input .
when it found \n in the line , it printed all the characters entered by the user one by one in different lines.
stdin- The file descriptor number zero is connected with standard input stream(stdin).The descriptor zero takes input from stdin until it gets Escape sequence(\n),this escape sequence occurs because you have press the enter key ,when you press the enter key while giving input it declared in stdin as \n\0 when it gets escape sequence which means to Process context that "End of Input "and it jumps to new line and when it comes to retake the input value it find \0 in stdin pipeline which means that here is the "End of String",so it ends the input stream.
Answer :- getchar() is a function that reads a single character from the standard input stream stdin. It is the c library function that gets a single character from the stdin. we use scanf function in our source code to take different types of values from user. we use getchar() in our source code only print one input at a time.
When scanf function take the input from user we input first number but three input goes on standard input stream (number \n \0)
Then Process context read the First input and it also read the Escape sequence ( \n ) after reading the Escape sequence ( \n ) process context thinks that this is the end of input.
Process context read Escape sequence (number /n) but ( \0 ) is remaining at input stream so that second scanf function take ( \0 ) as input and thinks that this is the end of input and again print the stdout.
So that we use getchar() in our source code after the first scanf.
getchar() only print one input at a time and it flush the ( \0 ) from the input stream so that input stream is empty and second scanf function take new input from the user. getchar() take many characters as input ,it will store all input. getchar() take input until it did not get ( \n ) as input after that when it found ( \n ) it printed all the characters entered by the user one by one in different lines and print accurate result.
Answer:- It is a function use to read a character.It is used to nullify the error while using two scanf function in a program while taking two different type of input from the user.
Let take an example to clear:-
1- writting a program to take 2 integer numbers and a character from user.
#include<stdio.h>
int main()
{
int a,b;
char c;
printf("Enter the number\n");
scanf("%d%d",&a,&b);
printf("The value of a and b is :%d\n%d\n",a,b);
printf("Enter the character\n");
scanf("%c",&c);
return 0;
}
result:-
Enter the number
1
2
The value of a and b is :1
2
Enter the character
In this it did not taken the character value ,so i.e. why we use getchar() to fill the \0 space.
See the programme :- the output of programme depends on compiler to compiler in which way the parameter are passed . Generally parameter are passed from right to left .
How to write a c program to calculate bike’s average consumption from the given total distance (integer value)traveled in(km)and spent in fuel in liters,float number-2decimal point. Solution
#include int main { Float distance; Int fuel; Printf(“enter the value from user\n”); Scanf(“%f%d”,&distance,&float”); Flaot average; Average = distance/fuel; Printf(“the average is %f\n”,average); return 0; }
int main represents that the function returns some integer even ‘0’ at the end of the program execution.’0’ represents the successful execution of a program int main(void) represents that the function takes NO argument.
getchar is used to get a character and putchar is used to display it. It is similar to, scanf and printf but, the main difference is that it can only be used for characters. It can be used with header file string.h.