Reputation: 395
For example:
#include<stdio.h>
int main()
{
char c;
c = getchar(); // Let's say you enter AA
printf("%c\n", c);
}
Does the second character get stuck in some reserve spot in memory like the stack? Because I know if I were to added another c = getchar(), the second character would be assigned to the variable c.
Upvotes: 0
Views: 84
Reputation: 1
as you know getchar only take one variable.so the 2nd 'A' will not store any where.it will be on ROM for sometime but the moment you hit enter it will vanish.
Upvotes: 0
Reputation: 34583
If you enter AA
for a single character the remaining input will still be in the stdin
buffer, as this program demonstrates. Instead of printing the characters, it prints their ASCII value for clarity. I entered AA<Enter>
just once.
#include<stdio.h>
int main()
{
char c;
c = getchar(); // Let's say you enter AA<Enter>
printf("%d\n", c);
c = getchar(); // still another A to come
printf("%d\n", c);
c = getchar(); // still a newline to come
printf("%d\n", c);
return 0;
}
Program session
AA
65
65
10
Upvotes: 1
Reputation: 206667
Does the second character get stuck in some reserve spot in memory like the stack?
It is most likely in a buffer associated with stdin
.
Upvotes: 4