-1

I can not figure out why the dialog messages get displayed before the "printf" statement.

If I add a '\n' to the "printf" than it acts like normal, but without it the dialog boxs ends up being displayed before the "printf".

Here is the test program:

/* Build: gcc -o test `sdl2-config --libs --cflags` test.c */

#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>

void MessageBox(void);
void clean(void);

int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_EVERYTHING);
    atexit(clean);
    SDL_DisplayMode mode;
    int id = 0;
    SDL_GetCurrentDisplayMode(id, &mode);
    char smode[20];
    sprintf(smode, "%ix%i@%iHz", mode.h, mode.w, mode.refresh_rate);
    printf("%s", smode);

    MessageBox();

    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
            "Missing file",
            "File is missing. Please reinstall the program.",
            NULL
            );
    return(0);
}

void MessageBox() {
    const SDL_MessageBoxButtonData buttons[] ={ /* .flags, .buttonid, .text */
        {0, 0, "no"},
        {SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "yes" },
        { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "cancel" },
        };

    const SDL_MessageBoxColorScheme colorScheme = { /* .colors (.r, .g, .b) */
        {   /* [SDL_MESSAGEBOX_COLOR_BACKGROUND] */
            { 0, 0, 0 },
                /* [SDL_MESSAGEBOX_COLOR_TEXT] */
            { 0, 255, 0 },
                /* [SDL_MESSAGEBOX_COLOR_BUTTON_BORDER] */
            { 255, 255, 255 },
                /* [SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND] */
            { 0, 0, 0 },
                /* [SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED] */
            { 255, 0, 255 }
        }
    };
    const SDL_MessageBoxData messageboxdata = {
        SDL_MESSAGEBOX_INFORMATION, /* .flags */
        NULL,                       /* .window */
        "example message box",      /* .title */
        "select a button",          /* .message */
        SDL_arraysize(buttons),     /* .numbuttons */
        buttons,                    /* .buttons */
        &colorScheme                /* .colorScheme */
    };
    int buttonid;
    if(SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) {
        SDL_Log("error displaying message box");
    }
    if(buttonid == -1) {
        SDL_Log("no selection");
    } else {
        SDL_Log("selection was %s", buttons[buttonid].text);
    }
}

void clean() {
    SDL_Quit();
}
Joe
  • 109
  • 1
  • 8
  • Try `fflush(stdout);` in-between. – too honest for this site Feb 03 '16 at 16:51
  • 3
    See [Why does printf not flush after the call unless a newline is in the format string?](http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – Serikov Feb 03 '16 at 16:53
  • I'm more looking for why this behavior is occurring. – Joe Feb 03 '16 at 16:53
  • 1
    Useful general hint: If someone gives you advice, but does not explain why, it might be a good idea to do some research on your own. That way you get more details, learn something new and very likely will memorise the information much better. – too honest for this site Feb 03 '16 at 17:11
  • Thank you to Serikov, I did fallow up on that link and it lead me to the information I was looking for. I agree Olaf. – Joe Feb 03 '16 at 17:13
  • when compiling, always enable all the warnings, then fix those warnings. The compiler will raise: `warning: unused parameter 'argc' [-Wunused-parameter]` and `warning: unused parameter 'argv' [-Wunused-parameter]`. those can be easily corrected by changing the `main()` signature to: `int main( void )` – user3629249 Feb 03 '16 at 23:11
  • I keep 2 version, debug and final. I do no include the warnings in the final version. I only place the build command for the final version at the top because i change the debug flags consistently. I do agree with you though user3629249 – Joe Feb 07 '16 at 12:59

1 Answers1

2

the reason for the seemingly mixed output order is because in C, stdout is internally buffered.

There are only a few ways that the stdout buffer is actually flushed to the terminal.

  1. a fprintf( stdout,... has the format string end in '\n'
  2. similar format string considerations for printf()
  3. a string item that is being output via %s contains a \n
  4. the function fflush( stdout ) is executed
  5. the program tries to input anything from stdin
  6. the program exits
  7. the buffer is completely filled
  8. the setbuf() function is called at the beginning of the program with setting the stdout buffer to length 0
  9. the puts() function is called.
  10. the putc() function is called with a char parameter of '\n'
user3629249
  • 16,402
  • 1
  • 16
  • 17