Luis Averhoff
Luis Averhoff

Reputation: 395

if I were to use the getchar() function and enter two characters, where does the second character get stored?

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

Answers (3)

Samir
Samir

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

Weather Vane
Weather Vane

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

R Sahu
R Sahu

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

Related Questions