Thread: Problem with the putchar() Function

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    Problem with the putchar() Function

    I just started learning to program and I can't get past this example because nothing gets printed to my screen.

    Code:
    /* copy input to output, version 1 */
    
    #include <stdio.h>
    
    main()
    {
    	int c;
    
    	c = getchar();
    	while (c != EOF);
    	{
    		putchar(c);
    		c = getchar();
    	}
    }
    I'd really appreciate some help. Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > while (c != EOF);
    Remove the ; at the end of this line.
    Your loop does nothing, except loop forever.
    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.

  3. #3
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    Yes. Your bug is that u put ";" after while... :
    Code:
    	while (c != EOF);
    	{
    		putchar(c);
    		c = getchar();
    	}
    If you do this the instructions that are between the {} will never get executed.
    I have stopped reading Stephen King novels. Now I just read C code instead.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    Wow. Can't believe I missed that. Thank You.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM