0

First of all, this is not a problem nor a bug. I just wanted to know how string input from stdin is handled in getchar(). Also, is the same method used for putchar() but just in reverse? These two functions are confusing me since getchar and putchar both take a single character from the input/ print a single char to the output. Is there like a special buffer for the string?

working code:

#include <stdio.h>

int main() {
    int c;

    c = getchar();
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
    return 0;
}
4
  • 1
    "Is there like a special buffer for the string?"- Do you speak about if you input the string and press Enter each character is subsequently taken? This buffer is from the terminal. You need to input f.e. a newline to flush the buffer from the terminal. It has nothing to do with the program itself in particular. Commented Jun 4, 2020 at 16:44
  • @RobertSsupportsMonicaCellio I wanted to know how the getchar() and putchar() really worked and how they handle strings Commented Jun 4, 2020 at 16:48
  • 1
    They don´t handle strings (as whole) at all. getchar just take a single character at each call from stdin, one after another. putchar() prints the the actual character. This repetition is what the while loop is for. Commented Jun 4, 2020 at 16:54
  • 1
    Yes, the buffer is the standard input stream called stdin. When you type something in the terminal the characters typed are added to stdin and all standard input functions that don't specify a file read the characters from it automatically. (Note that when you press Enter the character new line \n is added to the list of characters.) Commented Jun 4, 2020 at 16:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.