need some explanation

This is a discussion on need some explanation within the C Programming forums, part of the General Programming Boards category; can some1 explain how this code works... Code: #include <stdio.h> main() { int c; c = getchar(); while (c != ...

  1. #1
    Registered User abhisheksahni's Avatar
    Join Date
    Nov 2011
    Location
    india
    Posts
    6

    need some explanation

    can some1 explain how this code works...
    Code:
    #include <stdio.h>
     main()
    {
    int c;
    c = getchar();
    while (c != EOF) {
    putchar(c);
    c = getchar();
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm sure the vast majority of us could explain it, but we're not new students learning it. Have you tried the program? Just enter some letters and numbers and either press ctrl+z (Windows) or ctrl+d (Linux), to end the program. It works with just one character at a time, but changes the character you enter, into an int (int's are bigger than char's you see). Only an int can be used to detect the EOF (end of file) integer value.

    And welcome to the forum!

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    Why don't you explain your understanding of how it works? We can then correct any misunderstandings you might have.

    If someone explains the code to you, but fails to address something that is blocking your understanding, then you would be even more confused than you are now.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User abhisheksahni's Avatar
    Join Date
    Nov 2011
    Location
    india
    Posts
    6
    i have tried the program...
    it accepts wat ever i enter in a single line not just one character

  5. #5
    Registered User abhisheksahni's Avatar
    Join Date
    Nov 2011
    Location
    india
    Posts
    6
    Quote Originally Posted by grumpy View Post
    Why don't you explain your understanding of how it works? We can then correct any misunderstandings you might have.

    If someone explains the code to you, but fails to address something that is blocking your understanding, then you would be even more confused than you are now.
    i didnt understand how the code uses and integer c as it is a getchar function.. also since we arnt using files here y should we us eof?? and lastly y do we use c=getchar(); again at the end of the code???

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by abhisheksahni View Post
    i didnt understand how the code uses and integer c as it is a getchar function.. also since we arnt using files here y should we us eof?? and lastly y do we use c=getchar(); again at the end of the code???
    You use a file - stdin, which C opens for you, every time it's running. (along with four others).

    It works with int's because it was made so it detects EOF, and it works with every char, individually, but in a loop, see? It's like keyboarding - you press one key at a time, but your line of text may be quite long.

    getchar() pauses the program, or removes the last char from the input stream of the keyboard buffer, if present. Terminal windows (not run from inside an ide), immediately close upon completion of the running program - so without a pause, you couldn't see what had happened, since the terminal window (aka command window) just closed up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explanation pls
    By dpp in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2009, 02:08 PM
  2. need explanation
    By Ssfccsh in forum C++ Programming
    Replies: 6
    Last Post: 03-01-2004, 12:40 PM
  3. Explanation Please
    By atari400 in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2003, 08:39 AM
  4. Explanation
    By fallonides in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 12:41 PM
  5. Further Explanation
    By sketchit in forum C Programming
    Replies: 1
    Last Post: 09-24-2001, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21