Search code examples
cgcceclipse-cdt

Eclipse CDT unexpected output after compiling C Program


While Trying this code on eclipse CDT with GCC 5.1.0 Compiler All the strings were printed after the user input .. and while compiling it on Visual Studio and Code Blocks IDEs even with the windows CMD The program worked just fine as expected ..


‪#‎include‬ <stdio.h>
static char string[128] = "";
int main() {
printf("Type a string: ");
scanf("%s",string);
printf("The String is %s", string);
return 0;
}

Eclipse Output:

enter image description here


Visual Studio Output:

enter image description here

Thanks ,,,


Solution

  • OK, I see now. I think the issue is that whenever you want to be certain that something is printed by a given point in the code, you need to flush stdout at that point.

    Otherwise, streamed contents can be queued and delivered in an implementation-dependent way (usually in small batches)

    The C standard library's printf(), when outputting to stdout and encountering a newline \n, provides an implicit flush, so you don't need to call flush() yourself. Whereas with C++'s std::cout, only std::endl has this property; \n is not guaranteed to.

    Deliberately flushing stdout in C can be done like so: fflush(stdout);

    See also: Why does printf not flush after the call unless a newline is in the format string?