-
newbie with an old book
I am taking Comp 1200 (introduction to C programming for engineers) my freshman year here at AU. I have the old book called "The C Programming Language" by Kernighan and Ritchie. I have come across a code example that doesn't seem to work right. Here it is:
#include <stdio.h>
/*count the characters in input*/
main()
{
double nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("#%.0f\n", nc);
}
I thought that this program was supposed to tell me how many characters I input, but it doesn't seem to do anything. Am I missing something here?
-
I fixed the code, it didn't work becose EOF is a file define and you should use '\n' for strings or EOL I used '\n' so it's easy to see the code and how it works =)
#include <stdio.h>
/*count the characters in input*/
main()
{
double nc;
for (nc = 0; getchar() != '\n'; ++nc)
;
printf("#%.0f\n", nc);
}
Hope this helps. =)
Tazar The Demon :cool:
-
Hey, thanks. So EOF only works for files? I guess that makes sense, but why do they use EOF in the book?
-
you can simulate EOF from the keyboard on most systems its ctrl and z
-
This is what happens when I run the program with EOF in the code and hit ctrl-z. :
$ a.exe
alfdjaslfj
[1]+ Stopped a.exe
I am using the cygwin bash shell. Am I doing something wrong?
Thanks for the help. I really do appreciate it.
-
control-d is how you do it unix, try control-d I'm not sure how far cygwin
has gone to simulate unix.
-
Yea exact same behavior with linux and bash.
Control-z suppends the program so you can restore
it with % or fg.
-
For future referrence, it may be a good idea to use a while loop instead of the for that you use in your code. By the way, the book may be old but it is by far the best c resource that I own.
-
Control-D does work. I should have known since that is what I use all the time to log out. I didn't say it wasn't a good book; I just said that it was old. It's not the book we are actually using for the class. It was recommended to me, and I thought it might help. I have enjoyed it so far. Thanks a lot for all of your help.
-
> "The C Programming Language" by Kernighan and Ritchie.
I would suggest you get the slightless less old 2nd edition with a big ANSI-C stamped on the front cover.
-
I do have the second edition. I'm sorry for not clarifying that, but it is still old (13 years).
I'd like to thank you all again, and I am sure that I will post here later on. Hopefully, I will learn enough quickly so that I will be able to help others as well.
-
if you don't have the ANSI-C version, then you're not gonna wind up helpin anyone (no offense), because the language has evolved so much since then.
-
Keep at it C++ is one of the most powerfull and most used languages of all time so learn and most of all have fun =) if you need help just ask on this board and somebody will help you this board rocks becose of the great people that use it and help others out will they need it hope to see you here more =)
Tazar The Demon :cool:
-
but he wants to learn C, not C++ - so there! :p hehehe