Thread: question on k&r problem

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    question on k&r problem

    Hello, this question arose when testing the answer to one of k&r's exercises.

    The idea is to count blanks, tabs, and newlines before EOF is reached.

    insert
    Code:
    #include <stdio.h>
    
    main()
    {
    	int c;
    	int b = 0;
    	int t = 0;
    	int n = 0;
    
    	while ((c = getchar()) != EOF) {
    		if (c == ' ')
    			b++;
    		if (c == '\t')
    			t++;
    		if (c == '\n')
    			n++;
    	}
    
    	printf("Blanks : %d\nTabs : %d\nNewlines : %d\n", b, t, n);
    }

    Starting the program then pressing CTRL-C yields :


    C:\>counter.exe

    Blanks : 0
    ^C
    C:\>


    So the process, after it gets the SIGINT signal, breaks it out of the loop and prints part of the printf statement. Why only part . . . wondering if anyone knows what is going on inside the computer, thanks.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    EOF is Ctrl-Z in Windows.

    It's int main() according to the latest C standard (C99), int main( void ) if you want to be explicit. This change also requires that you have a return statement at the end of main().

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ioil
    So the process, after it gets the SIGINT signal, breaks it out of the loop and prints part of the printf statement. Why only part . . . wondering if anyone knows what is going on inside the computer, thanks.
    I cannot seem to duplicate your problem. That said, it may make more sense to simulate EOF with CTRL+D or CTRL+Z rather than use CTRL+C.

    Quote Originally Posted by msh
    It's int main() according to the latest C standard (C99), int main( void ) if you want to be explicit. This change also requires that you have a return statement at the end of main().
    Yes, though a related change does not require one to have a return statement at the end of the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    CTRL+C typically terminates the currently running application (assuming you're running through the command prompt/shell), which is not the same as sending EOF.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    CTRL-C terminates the program. CTRL-Z prints EOF.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question: sizeof() problem in function
    By Xeyide in forum C Programming
    Replies: 3
    Last Post: 09-04-2009, 12:05 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. K&R question
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 08-18-2005, 07:14 PM
  4. Loop problem: Asks a question twice!!!
    By Rhodium in forum C Programming
    Replies: 5
    Last Post: 07-04-2003, 09:47 AM
  5. Template problem question
    By grscot in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2003, 12:31 PM