2
votes

so I've been teaching myself C and I've come across the 'getchar()' and 'putchar()' methods from 'stdio.h'. As I understand it, 'getchar()' takes the most recent character from the text stream and stores it into a variable whilst 'putchar()' takes this variable and prints it to the terminal.

So I've written the following piece of code:

    #import<stdio.h>

void main () {
    printf("Enter a character and it will be repeated back to you:\n");
    int c;
    while (c != EOF) {
        c = getchar();
        printf("You entered : ");
        putchar(c);
        printf("\n");
    }
}

and I expected it to read the keyboard input and print it to the screen one character at a time. As an example, if I were to type "home", the output would be:

You entered : h You entered : o You entered : m You entered : e

but instead i get:

home You entered : h You entered : o You entered : m You entered : e

The characters are printed as im typing and then repeated afterwards. I'm not quite sure what I'm doing wrong here or if I am doing anything wrong and just don't quite grasp the concept. Can anyone explain whats happening here?

1
This program exhibits undefined behavior, since c is uninitialized the first time it's used. You will also pass EOF to putchar when you read it, which you shouldn't do. - Tom Karzes
void main() should be int main(void). If you have a book that tells you to use void main(), its author doesn't know C well enough to be writing about it. (Unless it's referring to some specific freestanding implementation, but that's unlikely.) - Keith Thompson
The book is written Brian W, Kercnighan and Dennis M. Ritchie. At the moment, the main function is just "main(){}" but i added the void because I have experiencec with C# and return types and stuff like that. - Armature
You will get more mileage out of #include <stdio.h>. Leave import to python... - David C. Rankin
back in the days of K and R, the returned type defaulted to int, however, in modern C, that is a problem and you have to explicitly state: int as the returned type - user3629249

1 Answers

7
votes

The output you're getting is expected.

Unless you make use of OS-specific functions to change the terminal settings, terminal input is only made available to the application when you enter a full line. The terminal driver buffers lines to allow you to edit before submitting it, and it echoes your input as you're typing it.

Once the line is entered, each call to getchar() retrieves one character from the line (as well as the final newline).

However, there is a bug in your program unrelated to your question. You're testing c before you assign it the first time. Also, the c != EOF test is checking the input from the previous iteration, which already tried to print that input, but you can't print EOF.

A better way to write the loop would be:

while ((c = getchar()) != EOF) {
    printf("You entered : ");
    putchar(c);
    printf("\n");
}

Or if the combined assignment and test is confusing, you can do:

while (1) {
    c = getchar();
    if (c == EOF) {
        break;
    }
    puts("You entered: ");
    putchar(c);
    putchar('\n');
}