Bug 577383 - fflush(stdout) insufficient to show output before scanf with MinGW C compiler
Summary: fflush(stdout) insufficient to show output before scanf with MinGW C compiler
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-debug (show other bugs)
Version: 10.4.0   Edit
Hardware: PC Windows 10
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: cdt-debug-inbox@eclipse.org CLA
QA Contact: Jonah Graham CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-11-20 22:12 EST by Eric Sokolowsky CLA
Modified: 2021-11-20 22:12 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Sokolowsky CLA 2021-11-20 22:12:18 EST
I have the following C program in CDT on Windows, using MinGW (gcc 6.3.0, gdb 7.6.1) as the compiler:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
	int a = 0;
	int count = 0;
	//setbuf(stdout, NULL);
	//setvbuf(stdout, NULL, 0, _IONBF);
	char buf[50];
	for (;;)
	{
		memset(buf, 0, sizeof(buf));
		printf("%d Enter a word: ", ++count); fflush(stdout);
		scanf("%49s", buf);
		printf("%d word is \"%s\"\n", ++count, buf); fflush(stdout);
		if (strcmp(buf, "done") == 0)
			break;
	}
	printf("%d enter a number:", ++count); fflush(stdout);
	scanf("%d", &a);
	printf("%d The number is %d\n", ++count, a); fflush(stdout);
	while (1) {}
	return EXIT_SUCCESS;
}

When run, I get the following output (I enter "a", "b", "done", and "321" each on separate lines):

1 Enter a word: 2 word is "41-thread-select"
3 Enter a word: 4 word is "1"
a
5 Enter a word: 6 word is "a"
b
7 Enter a word: 8 word is "b"
done
9 Enter a word: 10 word is "done"
321
11 enter a number:12 The number is 321

I see a few problems here. There is spurious input when the program starts: "41-thread-select" and "1", and fflush does not force the output buffer to appear. Neither the calls to setvbuf nor setbuf make any difference in the output.

These problems do go away if the external console is used. This program can be used to test CDT and the internal console once conpty support is completed.