2

I read and understood C executes a program sequentially, from top to bottom. So, for instance, this easy function:

int main(void)
{
    int i;

    i = -1;
    while (++i < 5)
        printf("%i\n", i);
    printf("end\n");
    return (0);
}

will return:

0
1
2
3
4
end

But, I had a couple of cases yet where this has not happened, and I'm not able to understand why... For instance here I would expect to have a "!" before each of the if/else result (so before "o" or "."), but all the "!" are printed at the end:

int     g_lines = 12;
int     g_columns = 34;
char    g_vacio = '.';
char    g_obstaculo = 'o';
char    g_lleno = 'x';

void print_map(void)
{
    int     line;
    int     col;

    line = 0;
    while (line <= g_lines - 1)
    {
        col = 0;
        while (col < g_columns)
        {
            printf("!");
            if (col % 2 == 0)
                write(1, &g_lleno, 1);
            else
                write(1, &g_vacio, 1);
            col++;
        }
        write(1, "\n", 1);
        line++;
    }
}

Output:

x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

What's the reason why the "!" is printed just at the end?

I decided to exchange printf("!") with ````write(1, "!", 1)`````and now, surprisingly, the result is how expected.

        while (col < g_columns)
        {
            write(1, "!", 1);
            if (col % 2 == 0)
                write(1, &g_lleno, 1);
            else
                write(1, &g_vacio, 1);
            col++;
        }

now the output is:

!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.
!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.!x!.

Can anybody explain why?

Bauz
  • 21
  • 3
  • I had a doubt just after posting the comment, so I decided to replace ````printf```` with ````write```` and now the behaviour is the one I expected. I'll edit the question to try to find a reason for that. – Bauz Mar 07 '20 at 16:58
  • 7
    Buffers. `printf` keeps data in a buffer until it is full and then calls `write` to output it. You are calling `write` directly in some cases, bypassing the buffer. – William Pursell Mar 07 '20 at 16:58
  • 1
    Yes, dupe. Deleted my answer and voted to close as dupe of https://stackoverflow.com/questions/1242974/write-to-stdout-and-printf-output-not-interleaved – Nikos C. Mar 07 '20 at 17:04
  • Using `fwrite` instead of `write` would also solve the problem. It's not a good idea to mix the standard IO functions and the POSIX ones. – HolyBlackCat Mar 07 '20 at 17:05

0 Answers0