beginner's question

This is a discussion on beginner's question within the C Programming forums, part of the General Programming Boards category; I just started learning C and I ran into one problem. I am using "The C Programming Language" from Brian ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    beginner's question

    I just started learning C and I ran into one problem.

    I am using "The C Programming Language" from Brian Kernighan and Dennis Ritchie as a learning material so I am following the examples in that book.

    The problem is with the following programme:

    Code:
    #include <stdio.h>
    
    main()
    {
    	int c, nl;
    	
    	nl = 0;
    	while ((c=getchar()) !=EOF)
    		if (c == '\n')
    			++nl;
    	printf ("%d\n", nl);
    }
    Now, it should give me, as a result, the number of lines in which I typed something. The problem is, when I press Ctrl+C, the programme ends without printing the last line of code.

    Exactly the same problem I encountered with character count.

    The question is probably stupid, but as I said, I just started.

    Any ideas?

    P.S. I am using Geany as IDE. I tried compiling it outside of Geany, with

    Code:
    cc -c line.c
    cc -o line line.o
    and then running it with

    Code:
    ./line
    but no help.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Ctrl+C will end the program. If you want to send EOF, try Ctrl+D (*nix) or Ctrl+Z twice (Windows).

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2
    As I said, probably stupid question .

    Anyway, thanks a lot. Ctrl-d works just fine..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's Question on QT
    By unix7777 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2008, 04:53 PM
  2. beginner's question :D
    By kingliaho in forum C Programming
    Replies: 5
    Last Post: 10-17-2008, 05:20 PM
  3. beginner's question about C
    By Roberto Llovera in forum C Programming
    Replies: 5
    Last Post: 02-26-2004, 04:14 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21