1

Cygwin64 bit

Command to compile:

gcc hello.c -o hello -ansi -pedantic-errors

Command to run

./hello

hello.c

#include<stdio.h>
int main() {
    /*setbuf(stdout, 0);        I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
    printf("hello world!\n");
    printf("hello world again!\r\n");
    /*fflush(stdout);           without fflush, the above strings are not showing*/

    while(1)
    {
    }
}

Questions:

  1. I don't want a fflush after every printf to let the terminal show the string in time, then how?

    ANSWER: setbuf(stdout, 0);

  2. Why is "\n" or "\r\n" not working in my case considering lots of posts pointed out a line break will fix the problem?

  3. Is it true that cygwin's terminal behaves differently than normal Linux's terminal? Since I don't have linux installed, anyone give a test for me?

  4. Or let me ask a more general question: On which kinds of terminals, the sentence "a new line will force flush" is true?

Thanks

milesma
  • 1,481
  • 14
  • 32
  • This might be a duplicate : http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – Jean-Michaël Celerier Aug 01 '13 at 03:14
  • @Jean-MichaëlCelerier : that mentioned "...will only display what's in the buffer after it reaches a newline", however, my case have "\n" and "\r\n" in the string. Is it true that cygwin's terminal behaves differently than normal Linux's terminal? Since I don't have linux installed, anyone give a test for me? – milesma Aug 01 '13 at 03:21
  • 1
    My experience of cygwin is that it comes close to feeling like Linux but never quite gets there no matter how hard you try. It could be that the kernel is not detecting that you are in a terminal session. Perhaps try a different terminal (`xterm`, maybe?), or even a different shell. See what happens when you login via `ssh`. If nothing you try solves the problem, it's entirely possible the cygwin just sucks. – paddy Aug 01 '13 at 03:28
  • 1
    "Is it true that cygwin's terminal behaves differently than normal Linux's terminal?" There are many different linux terminals and I don't think they all behave the same. But if you are talking about the kernel terminal, yes, I guess it must be pretty different. Ex. : http://en.wikipedia.org/wiki/Comparison_of_terminal_emulators – Jean-Michaël Celerier Aug 01 '13 at 03:39
  • @Jean-MichaëlCelerier: Thanks. but Cygwin is not in the list? – milesma Aug 01 '13 at 03:44
  • 1
    According to this page, default is now mintty. https://code.google.com/p/mintty/ (Edited ---- I was wrong when saying : Cygwin uses windows console by default, I think. ----) – Jean-Michaël Celerier Aug 01 '13 at 03:50
  • 1
    It works for me on Cygwin. It's hard to tell from your question just what does or doesn't work. Your comment says "I KNOW THIS WILL WORK", but it actually doesn't, right? Please show us an actual program that *doesn't* work and state clearly how it behaves. BTW, you *don't* need to print a `\r` character; just `'\n'` is all you need to specify end-of-line. – Keith Thompson Aug 01 '13 at 04:24
  • @KeithThompson, I've updated the post. What I was trying to say is: if I add "setbuf", then it will work. But I wonder why linebreak will not work on the condition of "no setbuf + no fflush + a loop after it + cygwin" – milesma Aug 01 '13 at 04:50
  • If I understand you correctly, it works with `fflush()` but doesn't work without `fflush()`. But your posted code still has the `fflush()`. Showing the actual program that fails is much clearer. (And it still works for me under Cygwin, even without the `setbuf` *or* the `fflush`.) – Keith Thompson Aug 01 '13 at 06:49
  • @KeithThompson: Did you put a while(1) loop at the end? For me, to make it clear, cygwin64 default installation + no setbuf + no fflush + with \n + a loop after printf; the string is not printed out. – milesma Aug 01 '13 at 06:54
  • To be clear *show us the failing program in your question*. I copied your code and commented out the `fflush`. Cygwin, 64-bit Windows 7. But it's a 32-bit Cygwin installation (I didn't realize until just now that there is a [64-bit Cygwin](http://cygwin.com/ml/cygwin-announce/2013-07/msg00030.html)). – Keith Thompson Aug 01 '13 at 07:01
  • @KeithThompson: I've updated the post, this is the exact code fail on my machine: cygwin64 default installation + no setbuf + no fflush + with \n + a loop after printf; – milesma Aug 01 '13 at 07:08
  • Thanks. That program works for me under xterm, mintty, and the Windows console (by "works" I mean it prints "hello world!" and "hello world again!", then hangs until I kill it). I suspect it's a Cygwin64 issue, but I haven't had a chance to try Cygwin64 yet. I suggest posting on the [Cygwin mailing list](http://cygwin.com/lists.html). – Keith Thompson Aug 01 '13 at 07:21
  • I tested this under `CYGWIN_NT-6.1-WOW64 CP-2 1.7.20(0.266/5/3) 2013-06-07 11:11 i686 Cygwin` and everything works as expected. The strings appear on the console immediately. – alk Aug 01 '13 at 07:31
  • I've posted an expanded version of my most recent comment as an answer. – Keith Thompson Aug 01 '13 at 07:31

4 Answers4

2

It seems that in Cygwin, stdout isn't identified as a terminal (but as a pipe), so it isn't line-buffered by default.

Based on this answer, perhaps linking with the Cygwin DLL would help.

Community
  • 1
  • 1
ugoren
  • 15,127
  • 2
  • 28
  • 59
  • Seen from @LidongGuo's test, we are almost sure that it is about Cygwin. I won't fix the Cygwin issue at the moment, so quite happy to accept the answer. – milesma Aug 01 '13 at 06:28
1

I know nothing about cgywin. but here I do a test in Linux.

I try the code below, and compile by : gcc -std=c90 filename.c

Without fflush It print all words before loop,so I think the newline flush the buffer! It just work!

I use SUSE gcc .

#include<stdio.h>
int main() {
    /*setbuf(stdout, 0);        I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
    printf("hello world!\n"); /*without fflush, not shown*/
    printf("hello world again!\r\n"); /*without fflush, not shown*/
  /* fflush(stdout);*/

    while(1)
    {
    }
}
Lidong Guo
  • 2,627
  • 2
  • 14
  • 27
1

That program works for me under 32-bit Cygwin. Specifically, when I compile and execute this program:

#include<stdio.h>
int main() {
    /*setbuf(stdout, 0);        I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
    printf("hello world!\n");
    printf("hello world again!\r\n");
    /*fflush(stdout);           without fflush, the above strings are not showing*/

    while(1)
    {
    }
}

it produces this output:

hello world!
hello world again!

and then hangs until I kill it with Ctrl-C.

I get the same behavior invoking the program from bash under the Windows console, mintty, and xterm (I doubt that the terminal would make any difference).

I'm using 32-bit Cygwin under 64-bit Windows 7. You say you're using 64-bit Cygwin, which was just announced a few days ago; I haven't tried it yet.

I suspect an issue with 64-bit vs. 32-bit Cygwin. I suggest you post to the Cygwin mailing list.

Here's a cleaned-up version of your program that should exhibit the same issue (please verify that the comments are correct):

#include <stdio.h>
int main(void) {
    /* Adding setbuf(stdout, 0) here makes the output appear */
    printf("hello world!\n");
    /* Adding fflush(stdout) here makes the output appear */
    while(1) {
        /* nothing */
    }
}
Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
  • I've verified 32-bit Cygwin on Windows 8 64 bit, the above code can print out "hello world!". However, I can not install gcc properly on 64-bit Cygwin on Windows 8 64 bit. I'm not sure whether this is another issue or not. Again, on my machine, the 64-bit Cygwin on Windows 7 64 bit can not show "hello world!" – milesma Aug 04 '13 at 22:31
1

I've just had this problem. I've got some code I've been working on a while and had to reinstall Cygwin on my PC. I installed the 64-bit version this time, it was x86 previously.

Built my code, ran it in the bash shell and no output.

What I didn't realize is that I have a Cygwin1.dll from the previous 32-bit version of cygwin still in my bin folder. I renamed this and ran my program and the output worked.

You may have a different issue, but if you've got different dll versions in use your bin other folders, it may cause this problem.

Phil_12d3
  • 388
  • 8
  • 18