new to c : please explain

This is a discussion on new to c : please explain within the C Programming forums, part of the General Programming Boards category; I've just started learning c. I would like someone to explain to me what happens in the code below. What ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63

    new to c : please explain

    I've just started learning c.

    I would like someone to explain to me what happens in the code below. What i don't understand is why it prints out all the characters i type. Shouldn't it only print out the first character, like the program below that.

    Code:
    #include <stdio.h>
    
    main() {
        int c;
    
        c = getchar();
        while (c !=EOF) {
            putchar(c);
            c = getchar();
        }
    }
    Code:
    #include <stdio.h>
    
    main() {
        int c;
    
        c = getchar();
        putchar(c);
        }
    }

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Why would it only print the first character? There is a while loop that keeps looping until an EOF is reached, so it will keep on printing characters until EOF.

    Also, your second program has an unmatched closing brace.

  3. #3
    Registered User freespace's Avatar
    Join Date
    Nov 2005
    Posts
    7
    The first program is echoing your typing because it has a loop. The statements in the body of the loop as enclosed by {} are executed as long as the loop's conditional is true. In this case, the conditional is "c != EOF". The loop is a while loop, so while "c != EOF" is true, the statements
    Code:
     putchar(c);
            c = getchar();
    are executed in order, until the conditional is false. Then the program exits.

    The second program does not contain a loop. It executes its instructions once, then exits.

    Hope this helps.

    Cheers,
    Steve

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    Sorry about the extra brace in the second code.

    Anyway, in the while loop isn't the putchar(c); executed first, which prints only one character. Then the c = getchar(); is executed and receives input from the keyboard. This of course repeats until EOF is reached.

    Wouldn't that mean one character is printed and after input is required again.
    Last edited by peterchen; 12-29-2005 at 08:39 PM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The characters you enter are not available to getchar until you press [Enter]. Then all of the characters you'd entered up to that point are available as well. Each character is not individually grabbed and printed when the stdin is line buffered, which is likely the case.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    yes, so getchar gets what you entered, putchar outputs it, getchar gets the next character you entered, putchar outputs it, getchar gets the next character you entered, putchar outputs it, and so on. Why would it behave any other way?

    So if you type "hello", getchar will return 'h', putchar will output 'h', then getchar will return 'e', putchar will output 'e', etc.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    Ok, i think i understand.

    I think i failed to understand that when you enter something into the program it is basically the same as if i had entered each character separately.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 02:16 PM
  2. Please Explain me few terms that i have listed in here.
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 08:20 AM
  3. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM
  4. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 03:46 AM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 01:22 AM

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