While you are giving like this, It will not ignore the white spaces.
scanf("%c",&ch);
When you are giving the input to the first scanf then you will give the enter('\n'). It is one character so it will take that as input to the second scanf. So second input will not get input from the user.
scanf(" %c",&ch);
If you give like this, then it will ignore that white space character, then it will ask for the input from the user.
Answer from Karthikeyan.R.S on Stack OverflowHow to input a character in C
c programming, questions regarding getting user input functions
Still confused in taking Input in C
How to read from user input without the \n ?
While you are giving like this, It will not ignore the white spaces.
scanf("%c",&ch);
When you are giving the input to the first scanf then you will give the enter('\n'). It is one character so it will take that as input to the second scanf. So second input will not get input from the user.
scanf(" %c",&ch);
If you give like this, then it will ignore that white space character, then it will ask for the input from the user.
The first program doesn't work properly, because the scanf function when checking for input doesn't remove automatically whitespaces when trying to parse characters.
So in the first program the value of c will be a char and the value of ch will be the '\n' (newline) char.
Using scanf("\n%c", &varname); or scanf(" %c", &varname); will parse the newline inserted while pressing enter.
Can someone explain how can i input a single character like for example in the below code on line 10 i tried to input a character using scanf() but it doesn't seem to work. It doesn't show any errors but moves on without waiting for an input.
/* Programm to Find out the maximum Range of an antena */
#include <stdio.h>
int main(void) {
float height;
int range;
char matric;
printf("How high is the Antena\n");
scanf("%f", &height);
printf("What Unit is it?\nM - for Meter\nK- for Km");
scanf("%c", &matric);
if(matric =='M'){
range = sqrt(12800000*height);
printf("Range is %d M",range);
}
else{
range = sqrt(12800*height);
printf("Range is %d KM",range);
}
return 0;
}
if i write %s instead of %c in line 10 it works. can someone please help