While loop bugs in C [closed] - c

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.

Related

How "Symbolic Constants in C " are read by Conditional Loops in C? [closed]

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.

Character count C program not displaying count [duplicate]

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.

Getchar() infinite loop [closed]

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.

How to use fflush in c on OS/X [closed]

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.

How to get the number of characters or lines by the CHARACTER COUNTING and LINE COUNTING programs in C? [closed]

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.

Resources