This question already has answers here:
How to avoid pressing Enter with getchar() for reading a single character only?
(14 answers)
Closed 4 years ago.
Here is my code:
#include <stdio.h>
int main()
{
char a = getchar();
printf("char: %c", a);
}
I'm compiling this with gcc.
When running, the program waits for input even after one character is entered on console. Shouldn't it immediately exit and print the character? I'm running this on Ubuntu for Windows, if that can make any difference.
stdin is your default input stream. The default buffering mode of stdin is line buffered. So it waits for the \n to be entered. Then only there is something to read from the input stream and then getchar gets the input and works with it.
The operating system often buffers console input until a new line is entered, such that your program will not even receive the single character before. Probably nothing you can do about that.
Related
This question already has answers here:
Why doesn't getchar() wait for me to press enter after scanf()?
(10 answers)
Closed 6 years ago.
I've been trying to teach myself C off the website http://www.cprogramming.com/.
I've come to the code which requires an input number.
Here is the code:
#include <stdio.h>
int main()
{
int this_is_a_number;
printf( "Please enter a number: " );
scanf( "%d", &this_is_a_number );
printf( "You entered %d", this_is_a_number );
getchar();
return 0;
}
However when I run this, and try to enter a number into the prompt, the command window just closes. Any help would greatly be appreciated.
scanf reads the number, but leaves the newline character you inputted in the input stream.
So getchar fetches it immediately instead of waiting for extra input.
You can add another call to getchar. It's probably the simplest solution for your simple program.
Use another getchar(); to consume the newline character left by the scanf() call; so that, the second getchar(); would hold the prompt.
getchar();
getchar();
...
Normally, you wouldn't need this. But if you are running exe (typically Windows) and want to wait at the end (which appears to be the case here), this trick would do.
This question already has an answer here:
C's printf and fprintf(stdout,) are not printing
(1 answer)
Closed 6 years ago.
Just see the below program,
#include <stdio.h>
int main()
{
printf("hai");
while(1);
}
The above code is not printing hai and its just waitinG. But if I add
printf("hai\n");
its working.
Can I know what happening internally?
Writing printf("hai\n"); instead will cause, due to the newline character, the output buffer to be flushed, so you'll see the output before entering the infinite loop. There are other ways of flushing the output buffer, but appending the newline character is particularly simple.
Technically the behaviour of a tight loop like while(1); is undefined in C, but that is unlikely to be the cause of your problem.
printf("hai"); won't show something immediateley, you need to flush stdout's buffer with either printf("\n"); or fflush(stdout);
Your standart output stream (stdout) is buffered, so it will only flush (and display what is in it) when it is either manually forced or when it recieves a newline character ('\n').
You can change this behavior with the library function setbuf()
You can also manually flush the buffer (force it to immideatly display whats currently in it) by using fflush(stdout);
This question already has answers here:
printf not printing to screen
(3 answers)
Why does printf not flush after the call unless a newline is in the format string?
(10 answers)
Closed 7 years ago.
I have code like this below:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("hello");
while(1){
// whatever here
}
}
and the question is: why the first instruction is skipped? It runs only the loop, hello is never printed. I compiled it with gcc and g++ with the same result.
It does run, it's just that the output buffer is not flushed before your while.
Use printf("hello\n"); instead. The newline character will flush the buffer so writing the output immediately to your console.
Your assumption is wrong, your code does run, only stdout is not flushed, but buffered.
Use fflush(stdout) after printf("hello"), this forces stdout to be printed.
And, as #Bathsheba pointed out, also a newline character ("\n") within the printf forces it to flush, which is explained in this SO question.
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.
This question already has an answer here:
Canonical vs. non-canonical terminal input
(1 answer)
Closed 6 years ago.
Here is my code. I run it in ubuntu with terminal. when I type (a CtrlD) in terminal, the program didn't stop but continued to wait for my input.
Isn't CtrlD equal to EOF in unix?
Thank you.
#include<stdio.h>
main() {
int d;
while(d=getchar()!=EOF) {
printf("\"getchar()!=EOF\" result is %d\n", d);
printf("EOF:%d\n", EOF);
}
printf("\"getchar()!=EOF\" result is %d\n", d);
}
EOF is not a character. The EOF is a macro that getchar() returns when it reaches the end of input or encounters some kind of error. The ^D is not "an EOF character". What's happening under linux when you hit ^D on a line by itself is that it closes the stream, and the getchar() call reaches the end of input and returns the EOF macro. If you type ^D somewhere in the middle of a line, the stream isn't closed, so getchar() returns values that it read and your loop doesn't exit.
See the stdio section of the C faq for a better description.
Additionally:
On modern systems, it does not reflect any actual end-of-file character stored in a file; it is a signal that no more characters are available.
In addition to Jon Lin's answer about EOF, I am not sure the code you wrote is what you intended. If you want to see the value returned from getchar in the variable d, you need to change your while statement to:
while((d=getchar())!=EOF) {
This is because the inequality operator has higher precedence than assignment. So, in your code, d would always be either 0 or 1.