Sublime Forum

C build system asks for input before running a program, instead of during

#1

Hello, I have been using sublime text for a good while now, though only really for html and css work. After switching to Linux Mint as a Windows 10 refugee, I decided to use sublime text for C and Python editing as well.

Building C files has been producing some strange behaviours. I have been using the “C single file - run” build system. The build system seems to run, and looks like it works fine if there are no scanfs in a program. If I do put a scanf in a program, it seems to run everything in the wring order.

This is my code:

#include <stdio.h>

int main()
{
int var;

printf(“the program is now asking for input, the inputted number is expected to come after this colon: “);
scanf(”%d”, &var);

printf("\ni have entered %d, and now another input is asked for, which will be stored in the same variable: “, var);
scanf(”%d", &var);

printf("\n\nthe correct number is here: %d, so the code technically functions the same\n", var);

return 0;
}// end main

and the output looks like this:

3
1
the program is now asking for input, the inputted number is expected to come after this colon:
i have entered 3, and now another input is asked for, which will be stored in the same variable:

the correct number is here: 1, so the code technically functions the same
[Finished in 2.6s]

The code functionally works, but with my testing, I have deduced that the build system seems to ask for input, and only then run the program, and call the inputs in order. My question is, is there any way to have the scanfs call in real time, and not before the programs runs?

0 Likes

#2

The problem is in your code. stdout is newline buffered, meaning that until a newline is printed or the buffer fills up it won’t get flushed (ie. shown on screen). I suggest manually flushing the output.

0 Likes

#3

When I used code runner in VScode on windows, I didn’t need to manually flush the output, and it seems to run as I expected when i ran the program in the terminal (and typing ./a.out, idk if that is significant). Why does the code require manual flushing when building in sublime, but not in those other circumstances?

0 Likes

#4

Likely because those are running in a terminal, whereas ST is just capturing stdout.

0 Likes

#5

I see, thanks for the help!

0 Likes