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.
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 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.
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
program source codeHow should I use fflush in C on OS/X? When I use it, it does not clear my buffer and terminates the program straight away.
Calling fflush(stdin); invokes undefined behavior. You should not use this to flush characters from the standard input buffer. Instead, you can read the characters upto the next linefeed and ignore them:
int c;
while ((c = getchar()) != EOF && c != '\n')
continue;
You can also use scanf() for this, but it is tricky:
scanf("%*^[\n]"); // read and discard any characters different from \n
scanf("%*c"); // read and discard the next char, which, if present, is a \n
Note that you cannot combine the 2 calls above because it would fail to read a linefeed non preceded by any other characters, as the first format would fail.
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.
This question already has answers here:
Why doesn't getchar() recognise return as EOF on the console?
(8 answers)
Closed 8 years ago.
I am a complete beginner in C, so sorry if this question sounds too trivial.
My understanding of getchar() and putchar() is that they process text streams one character at a time. Let's say I have this program that takes textual input from the user and displays it on the screen:
#include <stdio.h>
main(){
int c;
c = getchar();
while(c!= EOF){
putchar(c);
c=getchar();
}
}
Here's what I think is happening:
Suppose I run this program and I enter the word Hi. getchar reads the first character (namely H) and stores it in c. Then the program enters the while loop and puts H to the screen. Then it gets the next character (i) and prints it. Then comes the EOF and when getchar assigns the value to c, the while loop ends. So, according to this interpretation of what has happened, the program should end after printing all the characters and having reached the end of file.
When I run the program, however, after printing the string, the program waits to receive additional input, meaning it doesn't end, rather waits for the user to input more text streams.
Why does this happen and where do I get it wrong?
When you typed Hi and ENTER, no EOF is automatically inserted.
Instead, you need to press certain keys to generate EOF. On Unix system, press Ctrl + D, on Windows, press Ctrl + Z.