1

Im currently learning c, and I downloaded all necessary files for c to work on Vscode, if i run a code that doesnt have scanf, it runs normally, however, when I run a code that has scanf in it, it simply says "Running"

These are the extentions I downloaded

This is the code:

#include <stdio.h> int main() { int num; printf("Insert number"); scanf("%d", &num); printf("%d",num); return 0; } 

When i compile and run, it says this:

[Running] cd "c:\Users\LENOVO\Desktop\Programação\C" && gcc Exemplo.c -o Exemplo && "c:\Users\LENOVO\Desktop\Programação\C"Exemplo

but nothing happens. Can someone help me?

10
  • 13
    C stdio is buffered. Add \n to your printfs, or flush or disable the buffer. Commented Nov 27, 2023 at 16:04
  • 5
    ... and that has nothing in particular to do with VSCode. Commented Nov 27, 2023 at 16:10
  • 1
    Apart from you should always check the return value of scanf for the case that no number could be scanned at all (-> invalid input) – though this doesn't catch all possible input errors. I'd rather fetch entire input lines (fgets) and apply sscanf to the buffer read. Makes overall input handling (including error handling!) simpler. Commented Nov 27, 2023 at 16:28
  • 2
    @Gereon: 7.21.1/3: "Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment." There should be no need to explicitly flush the stream. Commented Nov 27, 2023 at 17:17
  • 1
    @JohnBollinger But of course it does --- the advised action "Add \n to your printfs, or flush or disable the buffer" is sufficient anywhere, but only necessary if you are in a hostile environment such as vscode. Commented Nov 27, 2023 at 18:48

2 Answers 2

1

Instead of running the code from the IDE (such as picking Start Debugging or Start Without Debugging), open a terminal (either within VSCode or a cmd shell), navigate to the directory where the code was built, and run the program from the command line:

> cd c:\Users\LENOVO\Desktop\Programação\C > Exemplo 

or just

> c:\Users\LENOVO\Desktop\Programação\C\Exemplo 

I just played with it in VSCode on my Mac and got the same behavior -- it looks like there's an issue when trying to run an interactive C program from the IDE.

Sign up to request clarification or add additional context in comments.

Comments

0

The scanf() is likely not the issue here. The problem is more likely that the VSCode stdout redirection is such that the stdout stream is not flushed as it should be when scanf() is called so the printf() prompt text is not output, pending a newline or a fflush().

This is probably peculiar to the execution environment provided by VSCode, but I have seen this in other IDEs and non-hosted embedded systems. You can work around it by explicitly flushing stdout. Whilst that should not be necessary, but does no harm.

printf("Insert number: "); fflush( stdout ) ; scanf("%d", &num); 

4 Comments

This does not explain the problem. C 2018 7.21.3 7 says “… As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.” Since the streams are presented to the user, they cannot be determined not to refer to an interactive device and therefore should not be fully buffered…
… The alternatives are unbuffered (which should show the printf output immediately) or line buffered (which should show the printf output when the scanf starts per 7.21.3 3, “… Furthermore, characters are intended to be transmitted as a block to the host environment … or when input is requested on a line buffered stream that requires the transmission of characters from the host environment.”). So something in OP’s environment interfered with proper operation of the streams.
@EricPostpischil. I am a bit lost now. Do your comments apply to my answer before I edited it (which I did following your comment against the question), or as it is now?
@EricPostpischil "Being presented to the user" is not the same thing as "being attached to an interactive device". What constitutes an interactive device is implementation-defined.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.