-
k & r question
Hello, I recently purchased K & R's book on C. I have a couple of questions. Here is the code that I have questions about:
Code:
#include <stdio.h>
/* count characters in input; 2nd version */
main()
{
double nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}
my question is, what is this supposed to do? I mean I know it is supposed to count the input, but when I run this program if I type something in, what will it do? I get nothing and wasn't sure if I had missed something or not. I have read this part numerous times and just can't wrap my head around it. I know it is something simple and I am probably brain dead, but can anyone explain this?
I understand what the code does, I just don't know the result...
Thanks for any help,
/asenchi
-
This code will count the number of entered characters, untill receiving EOF.
Depending on your OS, you can send EOF by using ctrl+d in UNIX and ctrl+z in windows.
HTH,
-
UNIX
Thanks a lot, that helped a lot.
/asenchi
-
I 've heard too much about K & R book - the book written by the creators of C, that it is very good, perfect and blah..blah.
However this program says the opposit ( note: i don't judge a whole book by only one program, but at least the basicest things should be right ).
-
curious
How would you write this, if I catch your implication?
/asenchi
-
Well, to give a right answer to your question, i should know the purpose of writing this program.
For example i don't see any reason for nc to be of type double. Even if they wanted to intoduce type double, they shouldn't have written this example to do it.
But i think it was written to show how the charater '\n' influences a program.
However, this isn't the real reason for telling that. basicaly i 'm talking about function main, which is just:
main()
without telling what it will return or anything.
I prefer a thousand times function main to be like:
void main()
instead of that - although this is also "wrong", and even better, to have at the end of the function the statement exit(0);
-
I didn't know you could increment a double like that...
-
This program was an example of character counting. It is the 2nd version of a program that used a while statement rather than the for statement you see here.
I have read a little on the different ways to write function main(). I will do some more reading on that. Also you should know that this is only the first Chapter which is a tutorial to kind of show some aspects of C.
Thanks
/asenchi
-
> This program was an example of character counting.
Well.... i don't really like it... If the user wants to see the number of characters he entered, he now sees the double number of the characters he entered. It's just my opinion...
>Also you should know that this is only the first Chapter which is a tutorial to kind of show some aspects of C.
Personally, i believe that the books and the teachers should use int main() in their first lesson - first programm. I don't say that they have to explain about the different kinds of main to the students, but just use int main() in every example - program.
P.S: Do you find that book good ?( does it explain good what it writes etc ) ?
-
>>he now sees the double number of the characters he entered. <<
What are you talking about??
Using that program, entering the word "four" followed by EOF gives the output 4, just as expected. What is "double" about that? The only reason I see to use a double is to allow for a greater number of characters than other variables can hold, but personally I think an unsigned long int would have better (or a size_t variable).
>>main()
This is a conversation that is held all too often, and is discussed in the FAQ.
-
somethings...
I just bought it 2 days ago, but so far it is pretty good. There are somethings that I don't understand, they really don't lay it out for you. It does take some research sometimes (reason for finding this board).
I do like how it is cut and dry though. I would rather have to figure some things out myself, however this was recommended as _THE_ C book, and for a beginner (which I am, in all programming) people might want to look somewhere else. I eyed the C Primer (can't find a link right now), that looks good too.
So you are saying everytime that someone uses the function main() they should express it as:
thanks for your help,
/asenchi
-
Hammer,
>Using that program, entering the word "four" followed by EOF gives the output 4, just as expected.
OK....... but what happens if the user enters some characketers like:
R
A
N
D
O
M
? Then it won't print the result the user ecpected(6), but 12.
>What is "double" about that?
Absolutely nothing. It's one of the worst examples to use double. They could just use "long".
asenchi,
I would also like to find that book and read it, just to have a taste of it.
>So you are saying everytime that someone uses the function main() they should express it as:
Like most people say here, it should be expressed as
Code:
int main( /*void*/ )
{
.....
return 0;
}
( you forgot the return statement )
-
>>but what happens if the user enters some characketers like
>>Then it won't print the result the user ecpected(6), but 12.
But 12 is the expected result. A new line is a valid character, just like any other white space character (space, tab)
>>It's one of the worst examples to use double. They could just use "long".
Just because you see a different way of doing things doesn't necessarily mean the original way is wrong. Sure, offer alternatives, but this small example is hardly the "worst example".
>>>So you are saying everytime that someone uses the function main() they should express it as
Why not just read the FAQ link I already posted? It answers your question correctly.
-
>But 12 is the expected result. A new line is a valid character, just like any other white space character (space, tab)
Ok, i know that a new line is a valid character. But i think that the user would expect to see the number of characters he entered without the newline character. But ok, it's just how i view it.
>Just because you see a different way of doing things doesn't necessarily mean the original way is wrong.
E.... i said that it would be the worst if with this example they would intoduce the type double.
Now, it is just... awful. But, ok, this is the way i see it, you don't have to agree.
-
i know
I did visit that link in the FAQ. However, I was wondering about money?'s way of doing it. And as you can see he did it just a bit different... I didn't see any examples of putting void in a /* */ in the FAQ... I am new, just gathering info...
/asenchi