ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I write the code below but dont get desired answer. I want to count the characters but when I run it and type something, a new line begins and nothing happen...
Code:
#include <stdio.h>
main()
{
int nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%d",nc);
}
While cmnorton's link is probably a good read and the EOF pitfall is certainly a thing to keep in mind, your code is OK in that respect.
I would recommend changing the printf statement a little though:
Code:
printf("%d\n",nc);
The solution to your 'problem' is quite simple: your code is waiting (looping) until a End-Of-File (EOF) comes in from the standard input. And by pressing enter, you're not sending a EOF, but newline character.
Try pressing CTRL-d after typing a few characters. CTRL-d is the way to send EOF.
Or pipe some data from a file or commandline into your program. This will send EOF automatically:
While cmnorton's link is probably a good read and the EOF pitfall is certainly a thing to keep in mind, your code is OK in that respect.
I would recommend changing the printf statement a little though:
Code:
printf("%d\n",nc);
The solution to your 'problem' is quite simple: your code is waiting (looping) until a End-Of-File (EOF) comes in from the standard input. And by pressing enter, you're not sending a EOF, but newline character.
Try pressing CTRL-d after typing a few characters. CTRL-d is the way to send EOF.
Or pipe some data from a file or commandline into your program. This will send EOF automatically:
I understood your explanation.
I've thought about it that process is still in the loop. cause any code after the loop didn't execute. But I didn't know why...
Thank you even if I can't get result I know the solution now.
But when I write something in input and press Ctrl-D nothing happen but something like this: hello^D...
But when I write something in input and press Ctrl-D nothing happen but something like this: hello^D...
You need to have at least one newline (i.e.: press enter) before ctrl-d. Or pressing ctrl-d twice also worked on my computer, but then the number is printed right after the string I typed.
If that does not work, then your terminal (or shell?) probably does no recognize ctrl-d as EOF. Try another terminal (or shell?) (bash, xterm, konsole, whatever...)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.