When searching for everything about getchar() function in this really great site, I found this post: Why doesn't getchar() wait for me to press enter after scanf()?
#include <stdio.h>
int main()
{
int value;
printf("1. option 1.\n2. option 2.\n3. option 3.\n4. Exit\n\nMake an option: ");
scanf("%d", &value);
switch (value)
{
case 1:
printf("you selected the option 1.");
break;
case 2:
printf("you selected the option 2.");
break;
case 3:
printf("you selected the option 3.");
break;
case 4:
printf("goodbye");
break;
default:
printf("thats not an option");
break;
}
getchar();//here is the question,why it's useful ?
return 0;
}
I understand the whole program, and I understand that each time it is called, getchar reads the next input character from a text stream and returns that as its value. That is, after
c = getchar();
the variable c contains the next character of input. The characters normally come from the keyboard.
But here is the question: why did the programmer call getchar() at the end of the program?
this practice is used especially for console applications, that way you force the program to not stop until you press a key so you can read the output. Usually the console closes when the program execution ends