Thread: Trying to understand printf, getchar and putchar behavior

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    2

    Trying to understand printf, getchar and putchar behavior

    Hello!

    I'm trying to understand text input and ouput in C by using the next piece of code:

    Code:
    /*EOF is found at stdio.h file as a symbolic constant,
    specifically as #define EOF (-1)*/
    
    
    main()
    {
        int c;
        long counter = 0;
            
        while((c = getchar()) != EOF){
            ++counter;
            printf("%ld",counter);
        }          
        printf("\nEnd of loop\n");
        
    }
    When running the program, I use characters asd as text input, it does nothing until enter key is pressed, then the terminal outputs:

    1234
    which I think is the count of the number of iterations done before next line(enter) value is used as input, the program continues until EOF is the input.

    By adding putchar(c); into the loop:

    Code:
    /*EOF is found at stdio.h file as a symbolic constant,
    specifically as #define EOF (-1)*/
    
    
    main()
    {
        int c;
        long counter = 0;
            
        while((c = getchar()) != EOF){
            putchar(c);        
            ++counter;
            printf("%ld",counter);
        }          
        printf("\nEnd of loop\n");
        
    }
    Again when running I use characters asd as text input, after pressing enter key, the terminal outputs:

    a1s2d3
    4
    Why does it always waits until the text line is finished?.

    I'm not a experienced programmer, but I thought that with the last code, the terminal will echoe every character used as input (printchar(c);) together with the iteration number (printf("%ld",counter);).

    In example, if input is character a, the output will be printed instantly without waiting for next line value:

    a1
    not requiring next line to produce:

    a1
    2
    There must be a reason of this behavior, but I ignore the point.

    Sorry for the beginner question & thanks for reading! :)

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It's always nice trying to discover things on your own, but sometimes you need knowledge of the underlying system to understand why they happen.

    In your case, it's called "buffered input". In a nutshell, reading input 1 character at a time from the system is costly( CPU-time wise ), therefore most standard input is "line buffered", meaning that it waits until the user types an entire line and sends it to the program all at once.

    You should also know that in C( and most other respectable languages ) exist a newline, a special character that tells the console to go down the next line. That is what is being printed between "3" and "4" in your second code's test. Yes, getchar() reads that too as a character.

    Finally, EOF isn't typically encountered by newcomers because they rarely deal with complex programs. It doesn't appear by itself unless you've redirected standard input to a file or pressed Ctrl-Z( Windows ) or Ctrl-D( *Nix and family ).
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    2
    Thanks for replying GReaper!

    I would like to know how does the application described by the above C code communicates to this buffer. I'm thinking that maybe this buffer is part of the virtual memory controlled by the system, so that the application advertises the shell that the buffer is needed when using the input/output functions, in this case getchar(c); which is the basic input function of C.

    Is this a short answer for the communication between the buffer and the apllication? or is something more complex? If there is no a short answer, can you suggest a book please?

    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Behavior of getchar() and putchar()
    By Strict in forum C Programming
    Replies: 5
    Last Post: 07-06-2016, 02:22 PM
  2. getchar() and putchar(c)
    By an96 in forum C Programming
    Replies: 5
    Last Post: 06-13-2015, 04:21 PM
  3. Getchar,putchar
    By Filster in forum C Programming
    Replies: 26
    Last Post: 08-03-2011, 04:45 PM
  4. getchar() and putchar()
    By kawaikx15 in forum C Programming
    Replies: 5
    Last Post: 04-13-2011, 11:02 AM
  5. getchar putchar
    By chess2009 in forum C Programming
    Replies: 7
    Last Post: 03-06-2011, 02:44 AM

Tags for this Thread