Thread: the c programming language example program doesn't make sense

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Sweden
    Posts
    2

    Exclamation the c programming language example program doesn't make sense

    I'm a beginner to C and programming and I've got a problem with the counting words, digits, characters program examples in the beginning tutorial chapter of "the C programming language, second edition". Those program doesn't make sense to me. As the example says, I use the FOR loop to repeat the call of getchar() and count inputs. But the program is programmed to print the results of counting of inputs when the test fails, and the FOR loop is terminated. Since getchar() never encounters the EOF signal when I run/use the program in the commandline tool (typing input on the keyboard) the program stays in the FOR loop forever and never prints out the results. I understand that the program really counts number of inputs and stores it in a variable... but am I wrong that those examples are usless programs? Is there something i've missed with the meaning/use of the int EOF in those examples? What is the meaning with having getchar() != EOF as the test part in the loop (FOR) when that condition always becomes true (in those examples) since the value of EOF by defenition chouldn't be mixed up with things you type on the keyboard?
    Code:
    #include <stdio.h>
    main()
    {
    	int nc;
    	
    	for (nc = 0; getchar() != EOF; ++nc)
    		;
    	printf("%d", nc);
    }
    Last edited by Ken Fitlike; 09-10-2006 at 03:43 PM. Reason: code posted, superfluous attachment removed

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You can simulate EOF with like, CTRL + Z in the Windows console, or you can pipe files into the input with redirectors '<'

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Sweden
    Posts
    2
    Thanks. Do you know how to simulate EOF in OS X (unix)? how do you pipe files into the the program did you say?

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I believe it's CTRL + D, here's some info about unix redirects: http://www.westwind.com/reference/OS...ine/pipes.html (It is still '<' apparently)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. That language do you program in?
    By h3ro in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 04-17-2007, 05:41 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM