1

Here is my code

#include <stdio.h>
#include <stdlib.h>
int main(){
char c,d;
c=getchar();
d=getchar();
printf("%c   %c",c,d);
return 0;}

here i asked for 2 inputs but it is stopping after taking one input. I mean if i write only one character and press enter it is just showing only that character. Not asking any further inputs.

enter image description here

But when i am giving two inputs at the same time and then press enter it is showing both.

enter image description here

  • 2
    getchar takes 'enter' also as a character. Every key you press will be considered as an input by getchar. In first case its printing 'c' and a new line '\n' whereas in 2nd case its printing both characters. – golimoli May 24 '21 at 16:55
  • For line-based input, I recommend that you always use [`fgets`](https://en.cppreference.com/w/c/io/fgets). Anything else is confusing. However, you may want to [remove the newline character](https://stackoverflow.com/q/2693776/12149471) when using that function. – Andreas Wenzel May 24 '21 at 16:59

1 Answers1

3

So, getchar() is taking a single character that is typed. But if you type c followed by enter you're actually giving it 2, with the second one being the enter. Thus, the second getchar() is actually receiving the enter key and hence you see the extra blank line in your top output.

Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69