Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Get and putchar in c

  1. Aug 18, 2009 #1
    Every time I start off with programming, I get stuck with these get and putchar functions.

    First off, as appose to what's in the books, the getchar takes in more than a character...it can take in complete pages, forget strings.

    Then the putchar only prints one character...after printing that, if it's called again, it's not that it'll print the next character that the variable has in store, but the same character again and again until we call getchar with the same variable assigned to it (for e.g. c = getchar()); NOW if putchar is called again, it will print the next character.

    Surprisingly getchar, when called above, did not take in new inputs, but when it was called the first time, it did take new ones.

    It appears that getchar() has a pointer applied on the variable which assigns to it. Assuming this variable to be c, if the function getchar() does not have anything stored in behalf of c, then it will take in fresh inputs.

    Then, each time c = getchar() is called, instead of taking new inputs, this pointer which has been pointing to a character stored in c will now will point to the next character and putchar prints this pointed character.

    Also I think the real information is stored in the function getchar(), since c = getchar() might destroy information in c if getchar() returns something.


    I know I'm wrong...so can someone correct me?
     
  2. jcsd
  3. Aug 18, 2009 #2

    sylas

    User Avatar
    Science Advisor

    getchar reads one character. To read more than one character with getchar, you need a loop to read whatever you like one character at a time.

    putchar takes an argument, and prints out whatever you have in that argument.


    getchar is a macro, and it refers to the stdin stream. It gets the next character from that stream every time you call it.

    Nope.

    Any assignment statement replaces what was in the variable with a new value.

    If you write c = 'Z', then this will replace whatever was previously in c with a new value.

    Cheers -- sylas
     
    Last edited: Aug 18, 2009
  4. Aug 18, 2009 #3

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Is this school work? If you're just trying to teach yourself programming, please don't start with ancient, painful C standard I/O functions!

    - Warren
     
  5. Aug 18, 2009 #4
    It's in the course, I really dont have an option.
     
  6. Aug 18, 2009 #5

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Okay. Let us know if you still have questions.

    - Warren
     
  7. Aug 18, 2009 #6
    getchar reads a char stored in the variable?

    Suppose, I do putchar(c), and I made inputs of sheets of information in c...then putchar(c) should print the same sheets...but it's only printing a character!

    I'm not getting anything here!

    Yes, I know, so if getchar() returns something, all values in c will be replaced right?
     
  8. Aug 19, 2009 #7

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    getchar() waits until the user types a key on their keyboard, and then returns that character to you.

    putchar() prints only a single character(). That's why it's called put char.

    What exactly are you trying to do?

    - Warren
     
  9. Aug 19, 2009 #8
    Returns?

    Now, when I getchar, I can type in whole words, but actually I expect it to enter the next sequence of commands even if 1 character has been inputted (like with microsoft's getch()).

    This character should get stored in a variable...that's expected.
     
  10. Aug 19, 2009 #9

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    So, you're saying that you expect execution of your program to stop executing on getchar() and wait for you to type a single character, but it continues to wait indefinitely, no matter how many characters you type?

    Can you show us your code?

    - Warren
     
  11. Aug 19, 2009 #10

    sylas

    User Avatar
    Science Advisor

    A variable is a bit of memory where you can store a single value.

    Your variable c can only store one character at a time. It cannot store a "sheet".

    If you can show some code, I can tell you what it is doing.

    Cheers -- sylas
     
  12. Aug 19, 2009 #11

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    What you're observing, dE_logics, is I/O buffering. When you type characters, they don't go directly to your program. Their journey includes:

    (1) The hardware sends them to the operating system (OS)
    (2) The OS routes them to your console window
    (3) Your console window returns them back to the OS
    (4) The OS hands them to the C libraries
    (5) The C library hands them to your program

    There is a lot of buffering going on in all of those parts -- and for the most part it is completely transparent. However, there is one particular part of this journey that can trip people up:

    Somewhere in step (3) or step (4), it is very common for characters not to be passed one at a time: instead, it is "line buffered" -- characters are saved up until you hit the enter key (or you type a very large number of characters), and then they are handed to the C libraries all at the same time.


    Your program doesn't notice anything weird -- when you called getchar() all it knows is that it asked the C library to give it a character, and getchar() returned a character to it.

    However, you notice something weird because you see both ends of this pipe -- you know you've typed a bunch of characters, but you don't see your program respond until you hit the enter key.


    Note that if your program asks the C library for a character and there aren't any to be had, your program gets put to sleep until a character is available. Your program has no idea it was put to sleep -- all it knows is that it asked for a character, and it got one.
     
  13. Aug 19, 2009 #12

    rcgldr

    User Avatar
    Homework Helper

    I don't know what compiler you're using, but note that _cgets() has been broke in Visual Studio since 2002 (perhaps before). I'm not sure if getchar() suffers from a similar issue.
     
  14. Aug 19, 2009 #13
    That's what I expect from getchar()...but that doesn't happen, so I'm trying to understand what happens, or what's the behavior of getchar()?

    I don't have a program...the topic is getchar().

    If I make it an integer, it does store and print sheets.

    putchar() can reproduce sheets with this loop -
    Code (Text):

     #include <stdio.h>

       /* copy input to output; 1st version  */
       main()
       {
           int c;

           c = getchar();
           while (c != EOF) {
               putchar(c);
               c = getchar();
           }
       }
     
    As printed in a book.

    Apparently that limit is unknown to me :D...despite trying.

    So actually this issue is more of a library problem.
    That function getchar() has stored the complete line and whenever it gets called, it assigns the value returned by it to a variable (as in the main program).

    Each time getchar() gets called, it returns a new character if the thing entered by the user is not a character but a line...each call will return a new character from within the inputed line.

    Very awkward behavior.

    gcc
     
  15. Aug 19, 2009 #14

    sylas

    User Avatar
    Science Advisor

    Your variable is storing a single character only. But as the program runs, it stores every successive character in turn, prints that one characters, and then gets the next one, and so on, until the program is finished.

    The function only works on one character at a time. What you are seeing is input output buffering. The operating system doesn't make input available to your program until you have a complete line. This is important, so that you can delete characters you have typed. When you hit return, all the line is then made available to the program, which processes it one character at a time using getchar.

    Cheers -- sylas
     
  16. Aug 19, 2009 #15
    So to getchar, an input buffer is made available instead of a character, each time it's called, it fetches a new character from the input buffer...as a result until the input buffer turns empty, to getchar it will always appear that something has been inputted.

    The capacity to print 'sheets' is actually the capacity of the buffer and not the integer variable.

    putchar simply prints this read character.
     
  17. Aug 19, 2009 #16

    sylas

    User Avatar
    Science Advisor

    Yes, your first paragraph has got it.

    As you dig deeper into the entrails of your operating system, there's a lot of stuff going on in the background, but in a nutshell, that's a good account. If the buffer is empty, then your program will wait until there's something in the buffer again.

    When reading from a terminal or command window, the input buffer accessed by getchar is usually filled up a line a time. So when you hit return, your program will loop through as many times as it needs to get the characters from the line one at a time, until they are all processed, and then it will pause, waiting until you've filled up the next line of input and pressed return again. When reading from a disk, the buffer is filled a block at a time. Getchar doesn't care. This lets the programmer think of it as simply getting input one character at a time.

    However, the capacity to print sheets is simply because you have a loop that prints every character you read; this doesn't depend on buffers to work. The program would still print sheets even if all the buffering was removed, because of the loop.

    Cheers -- sylas
     
  18. Aug 20, 2009 #17
    Ok...thanks a lot, finally I got it.
     
  19. Aug 20, 2009 #18
    But I think there is no use of this end of file indicator that getchar() puts...since getchar() will store only one variable.....

    So what's the point of doing so?
     
  20. Aug 20, 2009 #19

    sylas

    User Avatar
    Science Advisor

    getchar needs to have some way to indicate that it failed to get a character. That's actually why it returns a value of type integer, rather than a value of type char.

    getchar will return the value EOF, which is an integer constant, when there is no more input available. Otherwise, it gets the next available character, and returns that. Hence the special EOF value must be different from every possible character value.

    The loop keeps reading characters, until it getchar returns EOF. When getchar returns EOF, the loop finishes and the program terminates. Otherwise, it prints out whatever character was read, and then reads the next character, and loops around again back to the EOF test.

    Cheers -- sylas
     
  21. Aug 20, 2009 #20
    Ok...so it was put to ensure it got a character.

    Thanks a lot!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Get and putchar in c
  1. C - getting input (Replies: 3)

Loading...