Reputation:
Any idea why the following code doesn't print the amount of characters in the input? I've taken this straight from the K&R book. Learning C at the moment and this is really confusing, looks to me like I'm never reaching EOF. If that's the case then why would this be used as an example?
#include <stdio.h>
main()
{
double nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%d\n", nc);
}
Upvotes: 7
Views: 2462
Reputation: 28837
Your nc
is a double — use printf("%lf", nc)
to print doubles.
Try this one instead
#include <stdio.h>
int main(void)
{
int nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%d\n", nc);
return 0;
}
Upvotes: 6
Reputation: 347
First, as mentioned previously you need %lf instead of %d to display the double, otherwise it will just display zero all the time (d is for a signed integer). Then if you want to test this manually on a windows based OS type in some characters press Enter and then pressl CtrlZ on the next line all by itself (this will simulate the EOF). The printed result will be one more than the number of characters (it includes the CtrlZ). Also as SoapBox indicated when looking for an EOF typically the thinking back then was Unix where you pipe a file into the program via standard input, this would also work.
Upvotes: 0
Reputation: 15767
I'd like to clarify the answers given so far because they seem to use phrases like "send EOF", "received EOF", "EOF character", etc. As per comments (thanks) to this answer, "send EOF" and "received EOF" are legitimate terms, but please don't think that it's a character.
EOF is not a character at all. It is the value that getchar() (or fgetc/getc) returns if the stream is at "end-of-file" or a read error occurs. It is merely a special value outside the range of character values that getchar() will return that indicates the condition of error or end-of-file.
It is defined by the C standard as being negative, whereas getchar returns characters as an unsigned char converted to int.
Edit: On doing some research which I should've done before the paragraph I wrote that used to be here, I've realised some of my assumptions were completely wrong. Thanks to the commenter for pointing this out.
Once a stream (such as stdin) is in end-of-file condition, this condition can be cleared again with clearerr() and getchar() may read more data from stdin.
Upvotes: 5
Reputation: 176635
The program looks correct, your problem is that you have to send EOF
(end-of-file) to the command line after your input since the standard input is treated as a file. I think in a linux terminal you can press Ctrl+D to send EOF
... I'm not sure how to do it in other OS's. To test this program you might want to change EOF
to '\n'
which will cause it to stop when you press enter.
Upvotes: 12
Reputation: 109002
The program keeps reading data from stdin
until EOF
is received, this is done by pressing Ctrl-D at the beginning of a line on a Unix console or Ctrl-Z at the beginning of a line in a Windows console. After you signal EOF
in this way, the printf
function will be executed displaying the number of characters read.
Upvotes: 7
Reputation: 20609
This code reads characters from the standard input. When you run this program normally, standard input comes from the user. In this case, there is no way to send EOF to the program.
This code will work if you redirect a file to the standard in (./myprogram < tempfile.txt).
Upvotes: 1