C code prints happening with a line delay

Hi :wave:
So , I have C code on optimizing matrix multiplication and I’ve put getchar() not to do all the functions if I’m not interested. The compiled code on my laptop prints a statement, asks if the user wants to run that function and does it if it gets a ‘y’. As intended to do.

But when I compile it using RISC-V toolchain and upload it on Milkv-duo this happens. it first asks for the getchar()==‘y’ , then prints the statement it should have done before. It’s ok to me as I’ve written the code, but doesn’t make sense to a 3rd party user. U guys have any idea why is this happening and what’s the fix to it ?

Here’s a snippet of the code:

        printf("Input size of matrix, n = ");
        scanf("%d", &n);

        ref = 0;

        double *A  = (double*)_aligned_malloc(n * n * sizeof(double), 64 /*sizeof(double)*/); //  64 is cache line size
        double *B  = (double*)_aligned_malloc(n * n * sizeof(double), 64 /*sizeof(double)*/);
        double *C1 = (double*)_aligned_malloc(n * n * sizeof(double), 64 /*sizeof(double)*/);
        double *C2 = (double*)_aligned_malloc(n * n * sizeof(double), 64 /*sizeof(double)*/);

        if(A == NULL || B == NULL || C1 == NULL || C2 == NULL){
            printf("Memory Allocation Error\n\n");
            return(-1);
        }

        unsigned int seed = time(NULL);
        printf("\nseed = %u\n", seed);

        srand(seed);
        fill(A, n);
        fill(B, n);

        fflush(stdin);
        printf("\n\nDo you want to run matrix_mult_index (y/n)? ");
        if(getchar() == 'y'){
            ref = 1;
            t0 = clock();
            matrix_mult_index(n, A, B, C1);
            t1 = clock();
            printf("\n\tExecution time of matrix_mult_index = %0.2f s", (float)(t1-t0)/CLOCKS_PER_SEC);
        }

        fflush(stdin);
        printf("\n\nDo you want to run matrix_mult_ptr_no_reg (y/n)? ");
        if(getchar() == 'y'){
            if(++ref > 2) ref = 2;
            t0 = clock();
            matrix_mult_ptr_no_reg(n, A, B, ref == 1 ? C1 : C2);
            t1 = clock();
            printf("\n\tExecution time of matrix_mult_ptr_no_reg = %0.2f s", (float)(t1-t0)/CLOCKS_PER_SEC);
        }

Hi!

I noticed you do fflush of stdin. Why don’t you try this then?

It actually does what you wanted, I guess.

1 Like

thanks
yea I’m wondering how it was working properly on windows :slight_smile:

1 Like

It’s because of the way Linux handles buffering.

See c - Why does printf not flush after the call unless a newline is in the format string? - Stack Overflow

Whenever you print to stdout without a newline suffix, you should flush or disable the buffering altogether.

As for why it doesn’t happen on Windows, who knows. I guess they don’t care adhere to the C standard. But don’t expect Windows to adhere to any POSIX related standard, or any standard really. You might be better off testing in WSL.

2 Likes

ah I see , thanks for your enlightening explanation :pray:

1 Like