Thread: Copying input to output K&R there is no output

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    25

    Copying input to output K&R there is no output

    Code:
    #include <stdio.h>
    /* copy input to output; 1st version */
    main()
    {
    	int c;
    	c = getchar();
    	while (c != EOF) {
    putchar(c);
    		c = getchar();
    	}
    }
    there is no output and no errors at all what would be wrong ?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    getchar() buffers input, and waits until a newline is encountered, before returning the first character typed.

    The net effect is that it will appear as if your program waits for a line before echoing that line.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What do you mean "no output"

    Did you type anything in?
    Code:
    $ gcc bar.c
    $ ./a.out 
    hello world
    hello world
    hey
    hey
    it works
    it works
    Blue is what I typed, the rest is what the program printed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    I totally misunderstood copying input to output, I thought it would print out the code.
    Thanks you Salem and grumpy!

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    What? You thought that code was a quine????? Not even close.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    Quote Originally Posted by grumpy View Post
    What? You thought that code was a quine????? Not even close.
    Oh! you called that quine. Another one got learned thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-24-2009, 01:28 AM
  2. Input/Output
    By Soul.India in forum C Programming
    Replies: 1
    Last Post: 10-30-2003, 05:39 AM
  3. copying input to output
    By kermit in forum C Programming
    Replies: 15
    Last Post: 08-09-2003, 08:57 PM
  4. output as input
    By DDC in forum Linux Programming
    Replies: 3
    Last Post: 02-23-2003, 07:59 AM
  5. Input/Output
    By PaulMelia in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 08:13 AM