Thread: getchar() and putchar(c)

  1. #1
    Registered User an96's Avatar
    Join Date
    Jun 2015
    Posts
    10

    getchar() and putchar(c)

    Hello everyone, this is my first post.

    I also, began to learn programming only a week ago.

    This is an example in Kernighan and Ritchie book ("The C programming language).

    Code:
    #include <stdio.h>
    
    main() {
        int c;
        
        while ((c = getchar()) != EOF) 
            putchar(c);
    }
    My question is: Why putchar print all the characters taken by getchar and not only the last one ?
    Last edited by an96; 06-13-2015 at 12:56 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that the putchar call is within the loop body, i.e., we could also have written:
    Code:
    while ((c = getchar()) != EOF)
    {
        putchar(c);
    }
    EDIT:
    By the way, good job with the code bbcode tags and proper indentation. Note though that in modern C, main should be declared as explicitly returning an int and with void as the parameter, unless you want command line arguments, e.g.,
    Code:
    int main() {
    You should also explicitly return 0; at the end of main unless compiling with respect to C99 or later.
    Last edited by laserlight; 06-13-2015 at 12:59 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User an96's Avatar
    Join Date
    Jun 2015
    Posts
    10
    Quote Originally Posted by laserlight View Post
    Notice that the putchar call is within the loop body, i.e., we could also have written:
    Code:
    while ((c = getchar()) != EOF)
    {
        putchar(c);
    }
    EDIT:
    By the way, good job with the code bbcode tags and proper indentation. Note though that in modern C, main should be declared as explicitly returning an int and with void as the parameter, unless you want command line arguments, e.g.,
    Code:
    int main() {
    You should also explicitly return 0; at the end of main unless compiling with respect to C99 or later.
    You are right, the book is old and it's normal that its content may be deprecated.

    I still don't know what is the meaning of void (because i haven't studied it yet).

    But my question is: why putchar insert the output only when I press Enter, and all the characters, not the last only ?
    Last edited by an96; 06-13-2015 at 01:52 PM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by an96 View Post
    why putchar insert the output only when I press Enter, and all the characters, not the last only ?
    That has to do with how input to the console is handled by the system. Most of the time it's line-buffered, meaning it waits for the user to input a whole line before passing it to the program.
    Devoted my life to programming...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by an96
    I still don't know what is the meaning of void (because i haven't studied it yet).
    In this context it means that you declare that the main function does not take any arguments.

    Quote Originally Posted by an96
    But my question is: why putchar insert the output only when I press Enter, and all the characters, not the last only ?
    When you press enter, the input enters the standard input buffer (or rather the standard input stream, as that is the concept in C), upon which getchar reads the first character (as an unsigned char converted to int), prints it, then reads the second character, prints it, etc, until getchar returns EOF (i.e., it has detected end of file or an input error).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User an96's Avatar
    Join Date
    Jun 2015
    Posts
    10
    I understand it now: putchar operate like a queue and void means "no argument" (altough Dev C++ works fine also without it). Thanks to laserlight and GReaper for the replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() and putchar()
    By kawaikx15 in forum C Programming
    Replies: 5
    Last Post: 04-13-2011, 11:02 AM
  2. getchar putchar
    By chess2009 in forum C Programming
    Replies: 7
    Last Post: 03-06-2011, 02:44 AM
  3. getchar and putchar
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 03-11-2009, 10:29 PM
  4. need help please! getchar, putchar, etc.
    By sue in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 08:40 PM