Thread: getchar()

  1. #1
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41

    Lightbulb getchar()

    Code:
    #include<stdio.h>
    
    int main ()
    {
    	int c;
    
       while((c=getchar()) !=EOF)
       	putchar(c);
    }
    getchar takes one character from the stadin and putchar should
    print that character before any more input.

    then
    why the output is

    hello
    hello

    instead of hheelllloo
    Last edited by shiju; 12-05-2003 at 12:50 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because it's buffered. Flush your output stream each time through the loop.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41

    Unhappy

    Thanks Quzah for your reply.

    But after flushing also it is giving the same output.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) Don't use EOF. Test it against '\n'
    2) The process won't even go into the loop until the Enter key is pressed. Once it the enter key is pressed then the loops starts and getchar() gets the letters from the buffer for the input stream.
    3) You are saying c is an integer but you are assigning it a character. See the problem here?
    4) Only way to get the effect you want is to go a different route.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    No, this is good code appearing in K&R An integer is used because EOF is -1 on some systems. On many system stdout is buffered and output doesn't happen unless if '\n' appears or the buffer overflows. Try entering alot of characters without newlines and see if it overflows. Of course there is a limit to how many you can enter so you might try to do this automatically in a loop.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I think your program is performing expectively.
    It's really stdin that's buffered and that's why your getting those results. But you can't flush stdin(or shouldn't) so your kind of stuck. Perhaps try entering
    h <newline>
    e <newline>
    ..
    o <newline>

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No, this is good code appearing in K&R
    That means jack and squat to me. There are plenty of examples of bad code that is published.

    An integer is used because EOF is -1 on some systems.
    Just because it works on some systems is no reason to say its correct.

    By defination getchar will only return EOF when "on end of file or error." Now lets see are we reading from a file? Nope. The chance of an error occuring? Slim to none.

    Edit: No overflow problems with 1420 characters buffered.
    Last edited by Thantos; 12-06-2003 at 12:11 AM.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    That means jack and squat to me. There are plenty of examples of bad code that is
    Perhaps. The author of c doesn't make too many blatant errors though. The reason why this code does not use a user interface that you are used to is that in unix(and in dos) you can let stdin be a file by doing something like ./prog < file.txt EOF can be returned upon user input but this depends on what OS. C-d for unix and C-z for windows

    Correctness is a difficult property to define. It's entirely what the author of the code wants.

    I believe that the stdout buffers might be flushed when stdin is accessed. This is the way with C++, I think. Try this program
    Code:
    #include <stdio.h>
    
    int main(void) { printf("hello"); }
    On my system there is no output.
    Last edited by okinrus; 12-06-2003 at 12:55 AM.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Clearly shiju is not using redirection due to their output.

    Remember code is "wrong" if it doesn't have the desired results.

    Try this program... On my system there is no output.
    On my system it won't even compile. Of course I have it setup to not compile if there are any warnings.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Yes, that code is wrong because there is no return 0. Oh well, it would probably output "hello" anyhow. I believe this has happened to me before but it doesn't always happen.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Thantos
    1) Don't use EOF. Test it against '\n'
    Why not? EOF is a valid test.

    Originally posted by Thantos
    2) The process won't even go into the loop until the Enter key is pressed. Once it the enter key is pressed then the loops starts and getchar() gets the letters from the buffer for the input stream.
    In the provided (first post) example, yes. But not if you add a fflush call to the loop like I suggested.

    Originally posted by Thantos
    3) You are saying c is an integer but you are assigning it a character. See the problem here?
    No. There is no problem. In fact, this is exactly how you want to test characters, as integers, not as char.

    You cannot test EOF with a char data type. For that matter, take a look at some standard library functions for use with characters:

    int getc(FILE *stream);
    int getchar(void);
    int isalnum (int c);
    int isalpha (int c);
    int iscntrl (int c);
    int isdigit (int c);
    int isgraph (int c);
    int islower (int c);
    int isprint (int c);
    int ispunct (int c);
    int isspace (int c);
    int isupper (int c);
    int isxdigit (int c);

    Now this may not mean "jack and squat" to you, but it does mean something to the ANSI standard committee. So perhaps you should take it up with them?

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Why not? EOF is a valid test.
    But it doesn't work for what they are trying to do. If they were trying to read from a file its all good. However from the output they gave its pretty obvious they are not. So using input from stdin there is no way to end the program normally. How did I come to this conculsion, by running the dang program.

    I'll admit I was wrong on the int part. But I will not conced the EOF part due to what the orginal poster gave as their output.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Thantos
    But it doesn't work for what they are trying to do. If they were trying to read from a file its all good. However from the output they gave its pretty obvious they are not. So using input from stdin there is no way to end the program normally. How did I come to this conculsion, by running the dang program.

    I'll admit I was wrong on the int part. But I will not conced the EOF part due to what the orginal poster gave as their output.
    Well you're wrong again. Depending on your OS you can send EOF from the console by using CTRL+Z or CTRL+D usually.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Depending on your OS you can send EOF from the console by using CTRL+Z or CTRL+D usually
    What about the OS's that can't? And I try to avoid having to use escape characters to terminate a program.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What about the OS's that can't?
    This is a stretch. Name an operating system that is unable to signal EOF and supports interactive input.

    >And I try to avoid having to use escape characters to terminate a program.
    Why? If you're reading characters and every character is valid input, how do you terminate the program without placing extra restrictions on valid input or using an interactive option mechanism that would be awkward (at best).
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM