As mentioned in the comments, you are typing two characters. Letter a
and the new line character(\n
). show second getchar() accept \n
.
If you want to use second getchar() then before using it use fflush(stdin)
.
fflush(stdin) normally deletes (flushes) this type of extra character (in your case \n
). or you can do as below
#include<stdio.h>
#include<curses.h>
int main() {
char ch;
printf("Enter a charcter: ");
ch = getchar();
printf("\nyou typed the character ");
putchar(ch);
while ((getchar()) != '\n'); //fflush(stdin); /* use this*/
getchar();
return 0;
}
Here “while ((getchar()) != ‘\n’);” reads the buffer characters till the end and discards them(including newline) and using it after the “scanf()” statement clears the input buffer and allows the input in the desired container.
And also see the following links.
- Replacement of fflush(stdin)
- Alternative to C library-function fflush(stdin)
- Using fflush(stdin)
- Clearing The Input Buffer In C/C++