2

For reasons of self-improvement / broadening my horizons I have decided to learn some of the fundamentals of C using the K&R 'C Programming Language Book'.

I have been following along with the exercises, using Bloodhsed DEV C++ 4.9.9.2 as my IDE.

I came across the following exercise - counting characters in an input:

main()
{
    double nc;
    for(nc = 0; getchar() != EOF; ++nc);
    printf("&.0f\n", nc);
}

This code complies and runs without error.

However, when I enter a string in the console window and press enter I do not get any output i.e. a number that shows how many characters in the string. For example, if I type 'test' in the console window I am expecting 4 to appear as output.

I am sure the issue is simply the way I have set up my IDE? Or am I missing something more fundamental?

Any help much appreciated. Having come from a VB background I am really excited about learning a different language like C and getting to grips with pointers!

Edit

A related answer to my question is also given here and is very helpful: Why doesn't getchar() recognise return as EOF on the console?

Community
  • 1
  • 1
Alex P
  • 12,249
  • 5
  • 51
  • 70
  • With simple programs like this, if they don't work the first time, just retype the whole thing. It's really difficult to see typos when you don't know what you're looking for. – Skilldrick Dec 20 '09 at 11:04
  • You should be aware that DevC++ is buggy and no longer being developed. If you want a good, free, modern IDE for Windows, try Code::Blocks at http://www.codeblocks.org –  Dec 20 '09 at 11:50
  • Or Visual Studio Express. Or MinGW. Or Cygwin. – paxdiablo Dec 20 '09 at 11:54
  • @ Neil Butterworth - Thanks for that tip Neil. I do have Visual Studio 2005 and have managed to compile and run C code using Visual C++. Given that I have VS 2005 and am used to that IDE, is there any particular advantage to using codeblocks for C programming? – Alex P Dec 20 '09 at 11:57
  • @Alex If you are happy with VS, then no. Either VS or CB will be a great imrovement on DevC++. –  Dec 20 '09 at 12:01

3 Answers3

10

You won't get any output until the standard input stream is closed (with either CTRLZ under Windows or CTRLD under various UNIX flavors).

The newline is just another character.

You may also want to get a more recent C tutorial book. main() is no longer one of the mandated main prototypes for ISO C, you should be using one of:

int main(void) {}
int main(int argc, char *argv[]) {}

On top of that, your code as shown won't compile since you don't terminate the printf format string. Use:

printf("%.0f\n", nc);

instead of:

printf("&.0f\n, nc);

Why you're using a double is also a mystery unless you want to process really large files - I'd be using an int or a long for that.

Here's the code I'd be using:

#include <stdio.h>
int main (void) {
    long nc;
    for (nc = 0; getchar() != EOF; ++nc)
        ;
    printf("%ld\n", nc);
    return 0;
}

which produces the following session:

pax> countchars
Hello there.<ENTER>
My name is Bob.<ENTER>
<CTRL-D>
29

pax>
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • @paxdiablo - Fantastic help! I fixed the typo in my printf line and CTRL Z works for me. Also, I agree using double is not necessary and int or long will suffice (this point is made in K&R). – Alex P Dec 20 '09 at 11:36
  • @paxdiablo - Can I get your advice please? You mention getting a more recent C tutorial book as main() is no longer a mandated prototype for ISO C. I want to learn C more as an 'academic' exercise (e.g. what are pointers etc.) rather that building programs to solve real world problems. In that case, will K&R satisfy that goal? – Alex P Dec 20 '09 at 11:42
  • You can certainly learn the basics from K&R, that's not a problem. I think it would be *better* to learn the language as per the standard but it's by no means *necessary*. Run with K&R for a bit and change only if you start finding too many discrepancies. – paxdiablo Dec 20 '09 at 12:06
1

Replace:

printf("&.0f\n, nc);

to

printf("%.0f\n", nc);
Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
Pavel Radzivilovsky
  • 18,794
  • 5
  • 57
  • 67
0

The code you had given is correct but seems it doesn't work. In my case also it was not working, so i just replaced EOF with '\n' and it has worked for me but for only one line, as after press enter it gives the output and program ends. Please find the complete code below:

#include <stdio.h>
/* Count characters in input; version 2*/

int main(void)
{
    double nc;
    for (nc = 0; getchar() != '\n'; ++nc);
    printf("%.0f \n", nc);
    return 0;
}
WedaPashi
  • 3,561
  • 26
  • 42
Vino
  • 892
  • 2
  • 16
  • 26