4

I'm having problem with the printf function in C. It's just not printing the output, although buffering is disabled:

setbuf(stdout, NULL);  

and

setvbuf(stdout, NULL, _IONBF, 0);

also I'm using fflush(stdout);, but it still doesn't work.
This is the exact code:

int setup(){
    //...
    printf("Setup successful\n");
    fflush(stdout);
    return 0;
}
int main(int argc, char *argv[]){
    setbuf(stdout, NULL);
    setvbuf(stdout, NULL, _IONBF, 0);
    setup();
    //...
)

If the info helps; I'm on Linux (raspberry Pi).
Thanks in advance!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
itzFlubby
  • 2,269
  • 1
  • 9
  • 31
  • 1
    You'll need to show [mcve] of your code if you expect an answer. – dbush Jul 22 '18 at 12:50
  • Both variants should work (either fflush(stdout) or setbuf(stdout, NULL)). Do you compile it correctly (e.g. with gcc -o printtest printtest.c) and call it from the local directory like so: ./printtest ? (If you omit the ./ then you are not calling your program from the local directory instead you are calling something else that is in the path) – Stephan Schlecht Jul 22 '18 at 13:12
  • 1
    "The function setvbuf() returns 0 on success. It returns nonzero on failure (mode is invalid or the request cannot be honored). It may set errno on failure." – Stargateur Jul 22 '18 at 22:37
  • 2
    Do you set the stream to unbuffered at the beginning of execution / before any other printf calls? -"The setvbuf() function may only be used after opening a stream and before any other operations have been performed on it." The same holds for setbuf(). – Dillon Davis Jul 23 '18 at 20:24
  • Can you show your code to verify whether it's printf fault or you execution flow doesnot reach there. fflush(stdout) should definitely flush your output. – Deadpool Jul 24 '18 at 15:00
  • The two functions are directly executed on startup, setvbuf() returns 0. The print-function is executed by a function which is called by the main function. As if this wasn't strange enough, sometimes the printing works even without calling setbuf / setvbuf immediately. – itzFlubby Jul 24 '18 at 18:37
  • 3
    Posting a [mcve] would help. It is unclear why a complete compilable code is not posted with a question with a bounty. – chux - Reinstate Monica Jul 25 '18 at 02:05
  • 1
    If this didn't have a bounty, it would be closed because it lacks an MCVE ([MCVE]). We cannot diagnose the problem without seeing your main program. The call to `setbuf()` or `setvbuf()` must be the first operation on the file stream. – Jonathan Leffler Jul 25 '18 at 11:46
  • 3
    @itzFlubby Don't describe the code, **show** the code. Describing code says "this is *basically* what I'm telling the computer to do". Problem is, computers don't do "basically" what you tell them. They do **exactly** what you tell them. So tell us **exactly** what you're telling the computer to do, i.e. show the code as a [mcve] that others can compile and run that replicates your problem. – dbush Jul 25 '18 at 17:09
  • I added the exact code now. Sorry. – itzFlubby Jul 27 '18 at 12:08
  • I can't reproduce the issue with this code. If you copy and paste the above code *as is*, compile it and run it, does it reproduce the issue on your side? – dbush Jul 27 '18 at 12:13
  • Strangely it does. I use the IDE Geany to make the program. If I run the program via the IDE, it behaves this way (not always when I execute the program from the terminal). But after I ran the program, the terminal also doesn't flush anymore, if you understand what I mean. – itzFlubby Jul 27 '18 at 12:21

1 Answers1

6

I’ve tried reproducing your setup as closely as possible. I therefore installed Raspbian (The Raspberry Pi operating system) in a VirtualBox image and used Geany to create, compile and execute a C file. Here is the code in its entirety:

#include <stdio.h>

int main() {
    printf("Setup successful\n");
}
  1. Save this file as test.c:

    test.c saved

  2. Next, click on “Build” (the brick icon):

    build test.c

  3. And finally, run it (click on the paper plane icon):

    enter image description here

As you can see, this code compiles correctly, executes, and prints the message. No explicit flushing is necessary (printf to stdout automatically flushes when encountering a newline character). This behaviour is standardised and correctly implemented by the tools installed by Raspbian so it’s reliable.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • First of all respect for your great effort. Does the same output appear if you run an endless loop directly after the printing?. Like `printf("test\n"); while(1){}` – itzFlubby Jul 27 '18 at 15:54
  • @itzFlubby Yes: the buffer is flushed immediately after encountering the `'\n'`. The output is a single “test” on the terminal. — Is this not the case for you? – Konrad Rudolph Jul 27 '18 at 16:04
  • No, it then doesn't flush. – itzFlubby Jul 28 '18 at 20:05
  • Could it have something to do that the program is compiled with the `-g` flag for debugging? – itzFlubby Jul 29 '18 at 17:24
  • @itzFlubby I’m afraid not, `-g` won’t change the behaviour of this. – Konrad Rudolph Jul 29 '18 at 18:10
  • So you indirectly answered me that there is a specific problem with my system (Maybe a false setting, corrupt package, ...) and showed clearly that there should be no problem with the "normal" out-of-the-box-system. This is why I will mark your answer as correct! – itzFlubby Jul 30 '18 at 06:03
  • @itzFlubby Apologies for not being able to help better, I know this must be frustrating. But in summary you’re right: this problem must be elsewhere. – Konrad Rudolph Jul 30 '18 at 08:26