Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I’m starting to learn from "The C Programing Language" and one of the codes in the book is not working for me. This code suppose to count the number of characters using getchar().
Here is my code:
#include <stdio.h>
int main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%1d\n", nc);
return 0;
}
I try to run it and write some characters but when I press ENTER, it only starts a new line. It's like it’s never getting out of the loop.
A newline is not an EOF. You’re confusing EOF and EOL.
When your press ENTER, getchar() receives a newline: \n, and your program counts it just like any other character.
Try pressing CTRL + D (Linux terminal) or CTRL + Z (Windows terminal) to send an empty input to your program, thus ending it.
You can also write your input to a file, and give this file to your program as input, like this:
./your_program < your_file
When your input comes from a file, an EOF is automatically sent to your program when reaching the end of the file. That’s because there is not more output to get from the file.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I am trying to use the symbolic constant in the condition or test of a conditional loop. Here is what I tried:
#include <stdio.h>
#define TEST 0
void main() {
int c;
while ((c = getchar()) != TEST)
putchar(c);
}
I want to exit the program when I enter 0 as input. But it keeps copying my input even if I enter a 0.
Can you guys help me understand why this happens?
If you want the program to stop when you type 0, you should compare the return value of getchar() to the character constant '0' instead of the number 0. Note that you should also check for EOF to avoid an infinite loop at end of file:
#include <stdio.h>
#define TEST '0'
int main() {
int c;
while ((c = getchar()) != EOF && c != TEST)
putchar(c);
return 0;
}
Note however that if the program reads from a terminal, because the input is buffered both by the standard stream and by the terminal, the '0' byte will be be made available to the program until you hit the enter key.
Also note that the program will store on the first 0 byte, neven if this byte is part of a number such as 1024 or a word Hell0.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I just started writing C programs and I am taking help of The C Programming book written by Dennis Ritchie. When I tried to run the program for COUNTING characters or new lines, i was expecting some numbers as solutions but it didn't happen, instead it just allowed me to enter characters, with no value( of the number of lines/ characters) in return.
I am new to programming.
I would appreciate some help to get me through.
character counting
#include <stdio.h>
main( )
{
long nc;
nc=0;
while (getchar( ) != EOF)
++nc;
printf( "%1d\n", nc );
}
As you can see in the line
while (getchar( ) != EOF)
Your program is expecting an EOF (End Of File) before printing the counter.
Therefore you should type your text then Ctrl + D (EOF in a *nix command shell) or Ctrl + Z (Windows) to cut your input.
Then your counter will be printed.
Regards
Seems like you are confused with sending input to your program. Your code reads characters one by one from so called STDIN until it reads EOF (End-Of-File) marker. Usually STDIN - is a keyboard, so whatever you type appears to be read by getchar(). If you wish to send EOF - press Ctrl+D (unix systems). Another alternative - use prefilled text file and use it as STDIN for your program via redirection:
$ ./a.out < my_input.txt
This approach works well both on *nix and win systems.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm reading the book and I followed the small program in the book, there is a small counting program in which I can't have results.
#include <stdio.h>
int main() {
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
When I run this and type in some characters, there is no result, and the program is still executing, I can type in characters but still no result.
Are there anything wrong in the code?
And I'm running it in Xcode.
The code is fine, if you have the required header files included:
#include <stdio.h>
To exit the while loop, you should send a 'EOF' to the program.
There's a case you can reference What is EOF in the C programming language?.
On Linux systems and OS X, the character to input to cause an EOF is Ctrl-D. For Windows, it's Ctrl-Z.
First of all, if in case you forgot, you need to include the stdio.h header file and secondly, there is nothing wrong with the codes.
while(getchar() != EOF)
++nc
The lines of code above checks for end of file and if it returns false, it increments nc and its only when the while loop encounters an end of file before it prints nc. End of file in linux is Ctrl+D and so after inputting your characters, type Ctrl+D and you will have your count since thats the end of file or say input.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I just started writing C programs and I am taking help of The C Programming book written by Dennis Ritchie. When I tried to run the program for COUNTING characters or new lines, i was expecting some numbers as solutions but it didn't happen, instead it just allowed me to enter characters, with no value( of the number of lines/ characters) in return.
I am new to programming.
I would appreciate some help to get me through.
character counting
#include <stdio.h>
main( )
{
long nc;
nc=0;
while (getchar( ) != EOF)
++nc;
printf( "%1d\n", nc );
}
As you can see in the line
while (getchar( ) != EOF)
Your program is expecting an EOF (End Of File) before printing the counter.
Therefore you should type your text then Ctrl + D (EOF in a *nix command shell) or Ctrl + Z (Windows) to cut your input.
Then your counter will be printed.
Regards
Seems like you are confused with sending input to your program. Your code reads characters one by one from so called STDIN until it reads EOF (End-Of-File) marker. Usually STDIN - is a keyboard, so whatever you type appears to be read by getchar(). If you wish to send EOF - press Ctrl+D (unix systems). Another alternative - use prefilled text file and use it as STDIN for your program via redirection:
$ ./a.out < my_input.txt
This approach works well both on *nix and win systems.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The program should give output in the following way:
Enter a character
g
Successfully entered!
g
#include <stdio.h>
int main() {
int c,d ;
printf("Enter a character\n");
c = getchar();
printf("Successfully entered!\n");
putchar(c);
return(0);
}
But when it is run, it waits for the user to enter the input before asking for it and then prints out in the following way:
g
Enter a character
Successfully entered!
g
You may put a fllush after the printf before getchar
printf("Enter a character\n");
fflush(stdout);
c = getchar();
fflush is meant to be called on an output stream. This is an excerpt from the C standard:
int fflush(FILE *ostream);
ostream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.