0

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.

Luis Averhoff
  • 385
  • 6
  • 22
  • Did you have a look at - http://stackoverflow.com/questions/3676796/what-does-getchar-exactly-do – badola Oct 20 '15 at 16:43

3 Answers3

4

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

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
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

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.

Samir
  • 1
  • 1