Thread: Explanation of a function.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242

    Explanation of a function.

    Hmm, someone typed this function below and I don't understand it.
    Code:
    void clear_buffer(void)
    {
         int ch;
         
         while((ch = getchar()) != '\n' && ch != EOF);
    }
    2 questions:
    1. Why does it clear the buffer? I mean, it's just a while loop that *I have no idea what is does, lol* - so please explain me what it does either
    2. Why did he type void function(void) - why is the parameter is void? I've seen it before and never actually understood what it does.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    When do you plan to read the tutorials?

  3. #3
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by eXeCuTeR View Post
    1. Why does it clear the buffer? I mean, it's just a while loop that *I have no idea what is does, lol* - so please explain me what it does either
    Some functions might leave characters unread in the stdin buffer where input from user is normally stored when you key.
    while((ch = getchar()) != '\n' && ch != EOF);
    will continually read, by virtue of the loop, one character at a time until encounters the "enter key" or '\n' "new line" or the "End Of File" whichever is first. Clearing whatever was left in the stdin, or not. But the idea is to have a clean buffer for new functions to be call for user input.
    Quote Originally Posted by eXeCuTeR View Post
    2. Why did he type void function(void) - why is the parameter is void? I've seen it before and never actually understood what it does.
    Functions are designed to return something and to accept some parameters inside the parenthesis. If the written prototype is: clear_buffer() the compiler makes some decisions about what is reading and will interprete as int clear_buffer( void ). Just because is not explicitly written doesn't mean is not there.
    Therefore if you want a function that doesn't return any value and doesn't accept any parameters is necessary to write void function() and even better void function ( void )
    Last edited by Aia; 11-24-2007 at 11:32 PM.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    I don't understand the EOF.
    What is it exactly?
    I know it's End Of File but it doesn't say to me anything.

    And thanks

    BTW,
    Why while?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    EOF is a constant that getchar() (and others) returns when it has reached the end of the file.

    the value of EOF is implementation defined.

    EOF is a macro.

    EOF has a value outside the range of a char, so you need an int when you store the return value of getchar and check if there is an error conditon.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    What file and what is stdin?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by eXeCuTeR View Post
    What file and what is stdin?
    For all intents and purposes stdin is treated like a file* because it works with parts of the I/O library and uses all of its constants. EOF is one of these. EOF is defined by the C standard as a negative number, signaling the end of a file. Functions that read stuff from files can return this when there is nothing left to read, but there are a few exceptions (i.e. fread, fwrite). That's all you need to know about EOF.




    *But it is _not_ a file. Basically, don't try anything like fclose or freopen on stdin or any other std handles. Just read from stdin, write to stdout, etc. and be happy.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What file
    The one you happen to be reading from, or even standard input.

    what is stdin?
    Standard input, commonly representing keyboard input at the command line. EOF in such a case is usually triggered by some special key combination, e.g., CTRL + Z or CTRL + D.
    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

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    But why (ch = getchar()) != EOF?
    and why I need stdin for if I could create my own FILE * and I could do much more things with it?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But why (ch = getchar()) != EOF?
    Because the input buffer has no more contents when EOF is returned.

    and why I need stdin for if I could create my own FILE * and I could do much more things with it?
    You do not need stdin, unless you want to read from standard input.
    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

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Standard input? I don't understand what's this standard input.

    Sorry and thanks

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Standard input? I don't understand what's this standard input.
    Generally, keyboard input at the command line.
    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

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by laserlight View Post
    Generally, keyboard input at the command line.
    there's no int argc, char *argv[] in some programs I see that use stdin.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    there's no int argc, char *argv[] in some programs I see that use stdin.
    Indeed, for command line arguments are optional.

    By the way, have you ever written a "hello world" program for the command line?
    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

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, not argv/argc - I think that Laserlight meant the "command prompt", rather than "command line arguments".

    stdin is whatever the input is when you use getchar() - it's commonly "the keyboard", but it could be a file if you do something like "a.exe < somefile" - in this case, the stdin is redirected and comes from "somefile" instead of the keyboard.

    The same applies to "stdout", which is where printf() goes - it's usually the "console", but sometimes it can be a file, e.g. "a.exe > someoutfile" - this writes the output of your application to "someoutfile".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 18
    Last Post: 12-31-2005, 01:56 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM