#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
The output of above on my machine is
hello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-err
I had to kill the program to stop it.
Is it the correct and expected behavior.
Or it is wrong.This was an interview question hence I am posting here.
This is the expected output:
The program loops forever because of the while (1) loop.
stdout is line-buffered (by default), so it only flushes to the console when a new-line character ('\n') is printed. As you aren't printing any new-line characters, you never see any of the hello-out text.
However stderr is not line-buffered (by default), so it updates the console on every new fprintf() call.
stdout is buffered on most machines. You won't see any output from any fprintf calls to stdout unless you print a newline or call fflush().
So yes, it's expected behaviour, most of the time.
While everyone is right that you have an infinite loop, stderr is not buffered so you get it immediately, stdout is line buffered so it is deferred until you get a newline, they do not mention that stdout does not have infinite storage. I think the buffers are 1k or so by default (see setbuf). If you wait long enough you will get a very long burst of hello-out sequences. It is entirely possible that the last hello-out might be truncated part of the way through the string "hello-out".
[...]hello-outhello-outhellhello-errhello-err
^^^^
Related
Terminal is not synchronized with the program.
This is my simple code to see if it works:
#include <stdio.h>
int main(void){
puts("Hello World!");
system("pause");
return 0;
}
but in the local terminal appears
Press a key to continue...
before
Hello World!
This means that it is not synchronized with the program. How can I solve?
The problem is probably that the output buffer is not getting flushed before the function system is executed.
In order to explicitly flush the output buffer, you can add the line
fflush( stdout );
immediately before the call to system.
On most platforms, the standard output stream is line-buffered, so it should not be necessary to flush the output buffer, because puts automatically adds a newline character to the string, which should cause a line-buffered stream to be automatically flushed. However, according to the information you provided, the standard output stream does not seem to be line-buffered in Eclipse. It seems to be fully-buffered instead.
When I run the first code and press ctrl-c immediately there will be no 45 written out to the file. But when I run the second code, I do get 45.
I couldn't come to the reason why this behavior happens in the below code? If stdout is line buffered shouldn't output come after I enter a character? What am I missing?
First code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp=stdout;
fp=fopen("myfile","w");
fprintf(fp,"%d",45);
getchar();
// return 0;
}
Second code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp=stdout;
fprintf(fp,"%d",45);
getchar();
// return 0;
}
PS: I'm using GCC and the platform is Linux.
I dont think getchar has anything to do with the buffering you are observing in the first code. If you dont press any character and wait for a while ( ie stay inside getchar() ), you'll most probably see an empty file. This happens irrespective of whether you've stalled the program with getchar, sleep or a while(1).
Now you press Ctrl-C for which your program has no handler registered and hence your program terminates abruptly. Whether buffers/streams are flushed on abrupt program termination is implementation defined. Read the full story here
Why is data not being flushed to file on process exit?
For example, on my system ( Windows 7, Code Blocks which uses gcc and mingw framework internally ), I can see 45 indeed being flushed into the file irrespective of using getchar or anything when I terminate it abruptly. ie on my system, the implementation is flushing data to the file on abrupt program termination.
So, the bottom line is, what happens when you do things in a non-normal fashion is implementation defined!
But the second case is indeed a curious one where getchar seems to be flushing the stdout buffer. I shall update on that as soon as I find the exact cause of why getchar is/seems-to-be flushing stdout buffer
Whats causing confusion is getchar(), seems like when it is called it is flushing stdout.
If you replace getchar() with something like sleep(10) then you 45 won't be printed.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
FILE *fp=stdout;
//fp=fopen("myfile","w");
fprintf(fp,"%d",45);
sleep(10);
//getchar();
// return 0;
}
The first case you are writing to a physical file. fopen, fclose are buffered I/O depends on the implementation, data may not be committed immediately until the buffer is full. Calling fclose() will cause the buffer to be written to disk.
The second case you are writing to STDIO and data written to STDIO normally show up immediately.
#include<stdio.h>
#include <unistd.h>
int main(){
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
On compiling this programme in gcc and on executing it only prints hello-err and not hello-out.Why is that so?Can someone please explain the reason behind it?
If you add a '\n' to your message it will (or should), ie. "hello-out\n".
The reason being is that stdout is buffered in order to be more efficient, whereas stderr doesn't buffer it's output and is more appropriate for error messages and things that need to be printed immediately.
stdout will usually be flushed when:
A newline (\n) is to be printed
You read in from stdin
fflush() is called on it
EDIT: The other thing I wanted to add before my computer crashed...twice...was that you can also use setbuf(stdout, NULL); to disable buffering of stdout. I've done that before when I've had to use write() (Unix) and didn't want my output to be buffered.
It doesn't always print out the output to stdout because by design stdout is BUFFERED output, and stderr is unbuffered. In general, the for a buffered output stream, the stream is not dumped until the system is "free" to do so. So data can continue buffering for a long while, before it gets flushed. If you want to force the buffer to flush you can do so by hand using fflush
#include<stdio.h>
#include <unistd.h>
int main(){
while(1)
{
fprintf(stdout,"hello-out");
fflush(stdout); /* force flush */
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
Update: stdout is linebuffered when connected to a terminal, and simply buffered otherwise (e.g. a redirect or a pipe)
You forgot newlines (noted \n) in your strings. Or you need to call fflush(NULL); or at least fflush(stdout); before sleep(1);
And fprintf(stdout, ...) is the same as printf(...)
You need to output newlines or to call fflush because (at least on Linux) the stdout FILE buffer is line-buffered. This means that the C library is buffering data, and will really output it (using the write Linux system call) when the buffer is full enough, or when you flush it either with a new line, or by calling fflush. Buffering is needed because system calls are costly (calling write for every byte to be output is really too slow). Read also the man page of setbuf
I am trying to make a small program that includes an infinite loop to wait for signal input from the user. I wanted to print out a message about the current working directory before beginning the infinite loop. The message works on its own, but when I put the infinite loop into the code the message does not print out (but the terminal does loop infinitely). The code is:
#include <stdio.h>
int MAX_PATH_LENGTH = 100;
main () {
char path[MAX_PATH_LENGTH];
getcwd(path, MAX_PATH_LENGTH);
printf("%s> ", path);
while(1) { }
}
If I take out while(1) { } I get the output:
ad#ubuntu:~/Documents$ ./a.out
/home/ad/Documents>
Why is this? Thank you!
When you call printf, the output doesn't get printed immediately; instead, it goes into a buffer somewhere behind the scenes. In order to actually get it to show up on the screen, you have to call fflush or something equivalent to flush the stream. This is done automatically for you whenever you print a newline character* and when the program terminates; it's that second case that causes the string to show up when you remove the infinite loop. But with the loop there, the program never ends, so the output never gets flushed to the screen, and you don't see anything.
*As I just discovered from reading the question itsmatt linked in a comment, the flush-on-newline only happens when the program is printing to a terminal, and not necessarily when it's printing to a file.
Because you don't have a new-line character at the end of your string. stdout is line-buffered by default, which means it won't flush to console until it encounters a new-line character ('\n'), or until you explicitly flush it with fflush().
Perhaps the output is not getting flushed. Try:
printf("%s> ", path);
fflush(stdout);
Because the stdout hasn't been flushed.
Call
fflush(stdout);
before your loop.
Because the output is not flushed.
Add
fflush(stdout);
before the while loop will solve the problem.
I'm just learning C with Kernighan and Ritchie's book; I'm in the basics of the fourth chapter ("Functions and Program Structure"). The other day I became curious about the sleep() function, so tried to use it like this:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf(" I like cows.");
sleep(5);
return 0;
}
The problem is the output of the program, it looks like it does the sleep() first and then the printf(), in other words, it waits five seconds and then prints the string. So I thought, maybe the program gets to sleep() so fast that it doesn't let printf() have his work done like I want, that is print the string and then sleep.
How can I show the string and then put the program to sleep?
The compiler is GCC 3.3.5 (propolice) in OpenBSD 4.3.
printf() writes to stdout (the default output stream) which is usually line buffered. The buffer isn't flushed by the time sleep is called so nothing is displayed, when the program exits all streams are automatically flushed which is why it prints right before exiting. Printing a newline will usually cause the stream to be flushed, alternatively you could use the fflush function:
int main(void)
{
printf(" I like cows.\n");
sleep(5);
return 0;
}
or:
int main(void)
{
printf(" I like cows.");
fflush(stdout);
sleep(5);
return 0;
}
If you are printing to a stream that is not line buffered, as may be the case if stdout is redirected or you are writing to a file, simply printing a newline probably won't work. In such cases you should use fflush if you want the data written immediately.
Your problem is that printf (and anything else that uses the stdio library to write to stdout (standard output)) is buffered - line buffered if it goes to the console, and size buffered if it goes to a file. If you do a fflush(stdout); after the printf, it will do what you want. You could try just adding a newline ('\n') to your string, and that would do the right thing as long as you don't redirect standard output to a file.
I'm not 100% sure, but I think stderr isn't buffered, which can cause confusion because you might see output you made to stderr before output you previously made to stdout.
Buffering means that all the output is stored in a place (called buffer) and is output after a certain amount of data is present in it. This is done for efficiency reasons.
Some (most?) implementations clear the buffer after a newline when writing to the console, so you can also try
printf(" I like cows.\n");
instead of the call to fflush()
I implemented time encounter as following;
for (int i = 1; i <= 60; i++) {
printf("%02d", i);
fflush(stdout);
sleep(1);
printf("\b\b");
}