Hi all!
I have some biginner question:
I've started to learn C from the Kernighan&Ritchie book, using Pelles C compiler. Why this code (from the book) dont work:

#include <stdio.h>
/* count characters in input; 1st version */
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}

It should count chars from the input and then print the result (I hope). But when I compile it and start the script all I can do is write some input but it doesnt print anything...
Hope somebody can help me.

Thanks

Bosko

Recommended Answers

All 7 Replies

Hi all!
I have some biginner question:
I've started to learn C from the Kernighan&Ritchie book, using Pelles C compiler. Why this code (from the book) dont work:

#include <stdio.h>
/* count characters in input; 1st version */
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}

It should count chars from the input and then print the result (I hope). But when I compile it and start the script all I can do is write some input but it doesnt print anything...
Hope somebody can help me.

Thanks

Bosko

Input in C is buffered, and you have to force EOF before you can see the output. Pelles C is a windows compiler, right? You can force EOF by typing ctrl+z. If that doesn't work, let me know and I'll try to think of something else. :)

Also read this.

Input in C is buffered, and you have to force EOF before you can see the output. Pelles C is a windows compiler, right? You can force EOF by typing ctrl+z. If that doesn't work, let me know and I'll try to think of something else. :)

I'm using Pelles in XP. Dont quite understant where to type ctrl+z: if I type in console I get ^z symbol!

I'm using Pelles in XP. Dont quite understant where to type ctrl+z: if I type in console I get ^z symbol!

That's it! You found it. :) Just make sure that you type it on a line of its own or windows gets goofy. You want something like this.

C:\>myprogram.exe
here are some characters
^Z
25
C:\>

Don't forget to hit enter either. Otherwise the characters won't even get to your program because of how windows does console buffering too. ;)

Press enter key after you see the ^Z symbol and the program should work out to be fine...

Press enter key after you see the ^Z symbol and the program should work out to be fine...

THANK YOU FOR HELP!
Now it's working!

THANK YOU FOR HELP!
Now it's working!

You are welcome... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.