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.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an assignment where i need to find out what scanf("%*[^\n]"); does in a c program. I know that [^\n] means that the input is read until \n and that %* puts the input in the buffer and discards it. I don't understand what usage you can get out of it, because in my understanding it just reads the input till \n and discards it then.
scanf(“%*[^\n]”); usage in a c programm?
It is somewhat common to see yet fgets() is a better approach. Recommend to not use scanf() until you know why you should not use it. Then use it in a limited way.
I don't understand what usage you can get out of it
Example usage: code attempts to read numeric text with scanf("%d", &x); but "abc\n" is in stdin so function returns 0 and data in stdin remains. scanf("%*[^\n]"); clears out (reads and discards) the "abc" in preparation for a new line of input.
int x;
int count;
do {
puts("Enter number");
count = scanf("%d", &x); // count is 0, 1 or EOF
if (count == 0) {
scanf("%*[^\n]"); // Read and discard input up, but not including, a \n
scanf("%*1[\n]"); // Read and discard one \n
}
} while (count == 0);
if (count == EOF) puts("No more input");
else puts("Success");
Variations like scanf(" %*[^\n]"); and scanf("%*[^\n]%*c"); have their own corner problems (1st consume all leading white-space, even multiple lines, 2nd fails to read anything if the next character is '\n').
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 3 years ago.
Improve this question
The first ScanF takes both inputs when I type a character and an integer is expected.
see image below:
As you can see when I type "55ape" for ScanNum_A it sets ScanNum_B = 0.
Why does this happen?
It happens because
ape is still left in the input stream and your second scanf tries to read it but fails as it was expecting integer but found chars.
You can clear the input buffer after your first scanf as below.
//first scanf
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
//second scanf
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 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 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 7 years ago.
Improve this question
In c what does this do after gets
int c;
while ((c = getchar()) != EOF && c != '\n');
I saw that many of you are telling me its while loop and all, why so much complication to it? Why cant we just use this code which I have given below?
gets(name);
if(name == '\n'|| name == EOF)
gets(name);`
First of, the gets function is not really secure and you might want to use fgets instead.
Anyway, your piece of code is used to clear the buffer. When you read from the user input, all the things that the user will type is going to be stored in a buffer, and then the program will read from it. That why sometimes you need to clear the buffer so you don't read other things that you didn't want.
Well, this piece of code
int c;
while ((c = getchar()) != EOF && c != '\n');
is used to clear the buffer, as mentioned in #napnac's answer. It is mainly used instead of fflush (stdin);, which is UB. But note that this is successful only if the input buffer happens to contain data ending in a newline.
Otherwise, you can use fflush (stdin);, which is not recommended. You can also use the flushinp function provided by the curses library. It throws away any typeahead that has been typed by the user and has not yet been read by the program