Thread: Beginner: Why doesn't this work?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Beginner: Why doesn't this work?

    Hi, just started c programming on a Mac. I have this simple program and run it in a terminal but it doesn't terminate. Just keeps waiting for input. It shows EOF as -1 but if i type -1 it keeps waiting?
    Code:
    #include <stdio.h>
    
    main()
    {
    	int c;
    	
    	printf("%d\n", EOF);
    	
    	while ((c = getchar()) != EOF)
    		putchar(c);
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Welcome to the boards.

    There's a special keystroke to get an EOF from the console (keyboard input). In *nix OS's, that's usually Ctrl-D. In Dos/Windows it's Ctrl-Z.

    gg

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can't enter "EOF" as a regular keyboard character (like typing "-1"). Try CONTROL+C or some other sequence. (On Windows it's CTRL+Z). In XCode, it's CONTROL+C. Not sure what Terminal requires.

    Todd

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Thanks very much. It's CTRL+D. Back to it.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Check out the FAQ, lots of good stuff in there for beginners: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    From this FAQ entry, you'll see that you should have a return statement from main(). It's good practice to always specify the return type as well. In this case, main() returns an int.

    gg

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Thanks. Some great stuff there. I'll make sure to check it when i have a query.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  5. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 11:53 AM