1.5.2 Character Counting

M

Matt

Alright, so I'm a little confused here...what exactly does this do?
I've run it and it doesn't display anything, so I've typed some things
into it, to see if it'd do something then, but to no avail. Can
somebody explain to me what this does?:

#include <stdio.h>

/* count characters in input; 2nd version */
main()
{
double nc;

for(nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}

It's confusing the way they say it. Any help is GREATLY appreciated!
 
J

Jason

Matt said:
Alright, so I'm a little confused here...what exactly does this do?
I've run it and it doesn't display anything, so I've typed some things
into it, to see if it'd do something then, but to no avail. Can
somebody explain to me what this does?:

#include <stdio.h>

/* count characters in input; 2nd version */
main()

int main(void)
{
double nc;

for(nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);

return 0;
}

It's confusing the way they say it. Any help is GREATLY appreciated!

It reads the number of characters in a file and prints that number out.
The file, in this case, is stdin.

(Iif you have access to a Unix system)
Try executing the program like this:

$ cat FILENAME | ./PROGRAM

where FILENAME is some file, and PROGRAM is your a.out (or whatever you
are calling this).

-Jason
 
J

Jason

Matt said:
How would I do that on PC?(Windows XP SP2)

I'm not knowedgeable about XP console stuff. Try simply running the
program and typing in some data. Then press CONTROL-Z to send EOF. It
should show how many chars you typed.
 
K

Keith Thompson

Matt said:
How would I do that on PC?(Windows XP SP2)

How would you do what?

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

<OT>
Jason recommended:

$ cat FILENAME | ./PROGRAM

A simpler way to do this is

$ ./PROGRAM < FILENAME

The same thing should work under a Windows command prompt (but you'll
probably need to change ./PROGRAM to .\PROGRAM).

This is way off-topic for comp.lang.c; if you have any more
Windows-specific questions, you'll have better luck in a
Windows-specific newsgroup (if the on-line help doesn't answer
your questions).
</OT>
 
P

Peter Shaggy Haywood

Groovy hepcat Matt was jivin' on 19 Apr 2005 14:57:52 -0700 in
comp.lang.c.
1.5.2 Character Counting's a cool scene! Dig it!
Alright, so I'm a little confused here...what exactly does this do?
I've run it and it doesn't display anything, so I've typed some things
into it, to see if it'd do something then, but to no avail. Can
somebody explain to me what this does?:

#include <stdio.h>

/* count characters in input; 2nd version */
main()
{
double nc;

Liar, liar! Pants on fire! K&R2 has no such thing in section 1.5.2.
There the real nc is a long, not a double.
for(nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}

It's confusing the way they say it. Any help is GREATLY appreciated!

As I said before in another thread, and still not trying to be mean
or cause offence, if you can't understand such a simple program when
its purpose is clearly explained on the same (and a little on the
previous) page, then programming is probably not the best thing for
you.
This program counts the number of characters in its input. Clear?
You type in any characters at all, or redirect/pipe input from another
source such as a file (if your system allows it), and at the end it
outputs the number of characters typed/piped/read. How do you let the
program know that you want to end the input when you are entering it
manually from a keyboard? Well, that's a FAQ. I'm going to tell you
just this once, but in future please consult the FAQ list (see URL
below). It depends. It depends on the system. For example, on
DOS/Windows you type Ctrl + z followed by enter.
The FAQ can be found at http://www.eskimo.com/~scs/C-faq/top.html.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Stroustrup section 1.5.4, word counting 3
What's wrong ? 21
EOF question 41
Using getchar and putchars 3
question 20
cannot understand the character handling Program 17
basic c i/o and EOF 28
basic c i/o and EOF 1

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top