0

I added compiler shortcut to notepad++ using nppexec. But it's console is working in a bit weird way. It always prioritize functions that require input.

for example:

printf("First input:\n");
scanf("%d", &a);
printf("You typed %d");

The console should print "First input" and get my input, and print "You typed (input)"

But instead, it always requires me to type all the inputs for the scanfs first.

So the console log is like :

3
First input:
You typed 3

Compiled .exe file works in correct order, but this is kinda annoying.

Does anyone know solution to correct this console output order?

By the way,

printf("First input:\n");
scanf("%d", &a);
printf("Second input:\n");
scanf("%d", &b);
printf("%d, %d", a, b);

Console log of this code is

1
2
First input:
Second input
1,2

1 Answer 1

1

Please add fflush(stdout); after each printf. Unfortunately, it is something that is not changed in pipes' implementation in Windows through many years...

As NppExec Manual says (the section "4.5. Console output redirection"):

NppExec uses pipes to redirect child process'es output to NppExec's Console window and to redirect user's input from NppExec's Console window to child process'es input. Such approach has several known limitations:

  • no output may be shown until child process ends (if this child process performs a lot of operations without explicit flushing of its output);

  • no output may be shown while child process waits for initial input (this may happen with some interpreters, but does not happen with cmd.exe);

Also, the NppExec Manual says (the section "4.8.2. Collateral scripts"):

printf("%d\n", i);

fflush(stdout); // important! otherwise the output may be buffered inside a pipe

The comment about a possible buffering inside a pipe relates to NppExec - because NppExec uses pipes to redirect the console process'es output and input. As I stated before, and I am still stating now, this "feature" of buffering in pipes is not something infused by or incorrectly handled by NppExec - it is a core "feature" of pipes as they were implemented by Microsoft. This is known for years - and still it has not been fixed. So do use fflush() whenever a program is expected to be run without a real console window (e.g. when it is run in NppExec).

4
  • Does adding that fflush function affect compile output(exe file)? Or is there a way to just directly start compiled exe file other than opening in nppexec console? Maybe I'm asking for too much :P But I really want to add one-click compile & execute function in Npp.
    – WieeRd
    Jun 5, 2019 at 9:44
  • You can use npp_run YourApp.exe to start YourApp.exe in its own console window. If you want a console window to remain after YourApp.exe has finished, use e.g. npp_run cmd /c YourApp.exe & pause
    – DVV
    Jun 5, 2019 at 10:49
  • To get more information on what NppExec can do, type help in NppExec's Console. And if that is not enough, type help all.
    – DVV
    Jun 5, 2019 at 10:51
  • Thanks a lot! I really didn't have any clue on this one.
    – WieeRd
    Jun 6, 2019 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.