-
November 8th, 2023, 09:12 AM
#1
fflush(stdout)
If I want to write code for C89 It's necessary to use fflush(stdout) during message print to output ?
E.g. with printf("message") .
-
November 8th, 2023, 10:50 AM
#2
Re: fflush(stdout)
No (for windows).
printf() will display on stdout without needing to flush the output stream. Have you got a specific circumstance where you think you need fflush(stdout)? fflush() is usually used with a file stream when needed.
Last edited by 2kaud; November 11th, 2023 at 04:54 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 11th, 2023, 02:33 AM
#3
Re: fflush(stdout)
By default, stdin and stdout are line buffered for interactive programs.
A line buffered output stream would be implicitly flushed
- when the internal buffer is full (possibly BUFSIZ bytes (or chars?))
- when a newline is seen
- when the stream is closed (or the program exits)
You almost certainly need fflush for cases like this.
Code:
#include <stdio.h>
#include <unistd.h>
int main(void) {
printf("Countdown begins\n");
for ( int i = 0 ; i < 10 ; i++ ) {
printf(".");
sleep(1);
fflush(stdout);
}
printf("\nboom!\n");
return 0 ;
}
Without the call to fflush, you see no output until it's all over.
You MAY need a fflush in this case.
Code:
#include <stdio.h>
int main(void) {
int a;
printf("Type an int > ");
//fflush(stdout);
scanf("%d",&a);
printf("you typed %d\n",a);
return 0 ;
}
It works fine for me (on Linux), but YMMV.
The standard is suggestively unhelpful in this matter.
 Originally Posted by c99
The input and output dynamics of interactive devices shall take place as specified in
7.19.3. The intent of these requirements is that unbuffered or line-buffered output
appear as soon as possible, to ensure that prompting messages actually appear prior to
a program waiting for input.
But then goes on to say
 Originally Posted by c99
What constitutes an interactive device is implementation-defined.
So if you're running in an actual terminal, it may be OK.
If you're trying to capture the output say using redirection or screen/script/tee/expect, expect weirdness.
https://linux.die.net/man/1/screen
https://linux.die.net/man/1/script
https://linux.die.net/man/1/tee
https://linux.die.net/man/1/expect
Like this.
Without the fflush, this is what I see.
Code:
$ gcc foo.c
$ ./a.out
Type an int > 23
you typed 23
$ ./a.out | tee wibble
42
Type an int > you typed 42
$ cat wibble
Type an int > you typed 42
There is no prompt when duplicating stdout using 'tee'.
And now with the fflush
Code:
$ gcc foo.c
$ ./a.out
Type an int > 42
you typed 42
$ ./a.out | tee wibble
Type an int > 42
you typed 42
$ cat wibble
Type an int > you typed 42
There is a prompt when duplicating stdout using 'tee'.
-
November 11th, 2023, 04:53 AM
#4
Re: fflush(stdout)
Without the call to fflush, you see no output until it's all over.
Not with Windows and VS. This displays a . every second on Windows:
Code:
#include <windows.h>
#include <stdio.h>
int main() {
printf("Countdown begins\n");
for (int i = 0; i < 10; i++) {
printf(".");
Sleep(1000);
}
printf("\nboom!\n");
return 0;
}
Similarly for mixed display/input:
Code:
#include <stdio.h>
int main(void) {
int a;
printf("Type an int > ");
scanf("%d", &a);
printf("you typed %d\n", a);
return 0;
}
also for Windows shows as expected:
Code:
Type an int > 123
you typed 123
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 11th, 2023, 10:50 AM
#5
Re: fflush(stdout)
In other words, you've observed implementation specific behaviour.
> Similarly for mixed display/input:
What happens when you redirect stdout to a pipe?
-
November 11th, 2023, 12:15 PM
#6
Re: fflush(stdout)
What happens when you redirect stdout to a pipe?
Windows doesn't have tee wibble. But using | more gives the same with/without fflush(stdout)
Code:
#include <stdio.h>
int main(void) {
int a;
printf("Type an int > ");
fflush(stdout);
scanf("%d", &a);
printf("you typed %d\n", a);
return 0;
}
Code:
>testc | more
123
Type an int > you typed 123
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|