Thread: Behavior of getchar() and putchar()

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    4

    Behavior of getchar() and putchar()

    Hi, I have one or two question to said methods as a beginner in C.

    Definition of getchar(): Each time it is called, getchar reads the next input character from a text stream and returns that as a value.

    While putchar prints a character each time it is called.

    Now I got the following programm:

    Code:
    #include <stdio.h>
    
    
    int main() {
    
    
        int c;
    
    
        while ((c = getchar()) != EOF) {
            putchar(c);
        }
    }
    Why is the method getchar() able to read a text like "hallo" and why is putchar() able to print this text "hello"?

    putchar is said to print a (one?) character each time it is called. So shouldn't putchar() only be able to print the first character of "Hallo", so "H"? Or is there a loop defined in the method putchar() that traverses the char sequence of the text passed to the method? And why am I able to pass a text to c when c is a) defined as int and b) when getchar is supposed to read a single character?

    I'm using Code::Blocks on Windows and run applications on a console.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Or is there a loop defined in the method putchar() that traverses the char sequence of the text passed to the method?
    No, the loop is plain to see in your code - it's the while loop which wraps around calls to both getchar() and putchar()

    You need to realise that getchar() will be called repeatedly, not just once.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    4
    Yes, but the while loop only calls getchar() so I can pass a char, while putchar() is called inside the while-loop, where it prints the character I passed. But though getchar awaits a Character (or int), I can pass to it a Character-sequence ("Hello") and while putchar is meant to print a character, it prints the character-sequence "Hello". And in my understanding, putchar should print the first character of "Hello", means "H" and when returning to the while-loop, call the getchar method again to let me pass a new character.

    Well, I guess I just tink too complicated and should just accept the fact. :b

  4. #4
    Registered User
    Join Date
    Jul 2016
    Posts
    4
    Though in the end you are right. I just ran the code:

    int c = getchar();
    putchar(c);

    passed "Hello" to getchar() and putchar() printed "H".

    So how does it work in the case of the while loop? Does put char print the "H", returns the rest of the character sequence "ello" to pass it to c and continues til the char-sequence is finished (EOF)?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps this will explain.
    Code:
    #include <stdio.h>
    
    int myGetChar(void) {
      int ch = getchar();
      printf("\nGetchar called: returning ch=%c (decimal=%d)\n", ch,ch);
      return ch;
    }
    int myPutChar(int ch) {
      printf("Putchar called: ch=%c\n", ch);
      putchar(ch);
      return ch;
    }
    
    int main() {
      int c;
      while ((c = myGetChar()) != EOF) {
        myPutChar(c);
      }
      return 0;
    }
    
    
    $ gcc bar.c
    $ ./a.out 
    hello
    
    Getchar called: returning ch=h (decimal=104)
    Putchar called: ch=h
    h
    Getchar called: returning ch=e (decimal=101)
    Putchar called: ch=e
    e
    Getchar called: returning ch=l (decimal=108)
    Putchar called: ch=l
    l
    Getchar called: returning ch=l (decimal=108)
    Putchar called: ch=l
    l
    Getchar called: returning ch=o (decimal=111)
    Putchar called: ch=o
    o
    Getchar called: returning ch=
     (decimal=10)
    Putchar called: ch=
    
    
    
    Getchar called: returning ch=� (decimal=-1)
    Things to note:
    1. stdin is line buffered, so the code doesn't run until return is pressed.
    2. when a newline is entered, the loop runs until there is no more input (ie, the newline just entered)
    3. if EOF is signalled (by pressing ctrl-d (linux/unix) or ctrl-z (dos)), getchar returns the constant EOF (which happens to be -1 on my machine).
    Since the condition is != EOF, the putchar does NOT get called in this case.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2016
    Posts
    4
    That helped, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() and putchar(c)
    By an96 in forum C Programming
    Replies: 5
    Last Post: 06-13-2015, 04:21 PM
  2. Getchar,putchar
    By Filster in forum C Programming
    Replies: 26
    Last Post: 08-03-2011, 04:45 PM
  3. getchar putchar
    By chess2009 in forum C Programming
    Replies: 7
    Last Post: 03-06-2011, 02:44 AM
  4. need help please! getchar, putchar, etc.
    By sue in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 08:40 PM

Tags for this Thread