Reputation: 1195
Sorry for asking such a simple question, I'm still learning C and going through the basics first.
I'm creating a character counting program and yet when I execute the program and try to input "h" for example and then press enter a new line appears and nothing is outputted onto that line?
Code:
#include <stdio.h>
/* Copy input and count characters 2nd version */
main() {
double cc;
for(cc = 0; getchar() != EOF; ++cc);
printf("%.0f\n", cc);
}
Upvotes: 1
Views: 519
Reputation: 5456
Maybe (I'm not sure what exactly do you want) you have an extra ;
after the for()
, which mean an empty statement. So your program will run the empty statement (in other words, do nothing) until the end of input (if the input is terminal, you may need a CTRL+D
), and then print (once) the number of characters.
If you want your program to print the counter after every char in the input, remove that ;
, so the printf
will be in the loop.
Upvotes: 0
Reputation: 500457
Once you have finished entering characters, you have to signal the end of input stream by pressing Ctrl-D
. Otherwise your program will continue waiting for more input.
P.S. Why are you using a double
variable for the counter? An integer type would be more appropriate.
Upvotes: 5