Thread: printf does not work out of loop K&R array example

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

    printf does not work out of loop K&R array example

    Code:
    #include <stdio.h>
    /* count digits, white space, others */
    main()
    {
    	int c, i, nwhite, nother;
    	int ndigit[10];
    	nwhite = nother = 0;
    	for (i = 0; i < 10; ++i)
    		ndigit[i] = 0;
    	while ((c = getchar()) != EOF){
    		if (c >= '0' && c <= '9')
    			++ndigit[c-'0'];
    		else if (c == ' ' || c == '\n' || c == '\t')
    			++nwhite;
    else
    			++nother;
    	}
    printf("digits =");
    		for (i = 0; i < 10; ++i)
    			printf(" %d", ndigit[i]);
    printf(", white space = %d, other = %d\n",
    			   nwhite, nother);
    }
    

    this code an example for an array in K&R 2 but it is not executed unless printf inside the while loop is there a specific reason for that or may be something wrong with my mac ?


  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You need to manually trigger an end-of-file indication on stdin when you run the program (after supplying all the input you intend to be counted). The way you do that depends on your operating system, but definitely requires action by you (the user).

    If you don't do that, getchar() will never return the value EOF, so the while loop will never terminate, and the printf() statements will not be reached.
    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
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    Thank you very much! I found that control+d

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    Or you could just replace the EOF with
    Code:
    while ((c = getchar()) != '0')
    and the program should stop with the loop.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by Dakshin View Post
    Or you could just replace the EOF with
    Code:
    while ((c = getchar()) != '0')
    and the program should stop with the loop.
    It'll certainly stop the loop, but given that the loop should accept any character and not just digits, your suggestion breaks the algorithm.
    My best code is written with the delete key.

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Try creating a text file that contains your input. Then run the program like: ./foo < sample.txt, or "cat sample.txt | ./foo"

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    Quote Originally Posted by Prelude View Post
    It'll certainly stop the loop, but given that the loop should accept any character and not just digits, your suggestion breaks the algorithm.
    I don`t see why... the 0 is in quotes, isn`t it? The program is checking a character, not integer
    It's a miracle that curiosity survives formal education - Albert Einstein

  8. #8
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    The program is checking a character, not integer
    Exactly. The program should be able to accept a '0' as acceptable input, not to stop the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array manipulation doesn't work in printf
    By johan.g1 in forum C Programming
    Replies: 2
    Last Post: 11-14-2012, 10:05 PM
  2. Problem with printf (in a while loop)
    By dark_rug in forum C Programming
    Replies: 6
    Last Post: 10-12-2012, 11:37 PM
  3. Simple printf and for loop help
    By andrew89 in forum C Programming
    Replies: 5
    Last Post: 12-23-2011, 05:05 AM
  4. printf doesnt work
    By edmund085 in forum C Programming
    Replies: 27
    Last Post: 09-02-2011, 06:18 AM
  5. newbie - while loop and printf
    By nickch in forum C Programming
    Replies: 7
    Last Post: 04-14-2005, 02:33 PM