0

I have been trying to run this program that contains scanf on Visual studio on my Mac but the program never runs. I have tried the scanf without the & too and it still doesn't output any prompt. Any solution?

#include <stdio.h>

int main()
{
    int testInteger;
    printf("Enter an integer: ");
    scanf("%d", &testInteger);  
    printf("Number = %d",testInteger);
    return 1;
}

When I run the code and stop it this is all I get

[Done] exited with code=null in 11.983 seconds

  • 1
    Try adding `fflush(stdout);` after the first `printf`. It is possible you are misunderstanding the behaviour. stdout is line buffered and may not actual show any output without a newline character in the string or an explicit `fflush`. So you may think it is doing nothing. Can you please confirm whether that's the case? Have you tried entering a number and pressing ENTER? – kaylum Feb 26 '22 at 10:40
  • "Visual Studio on Mac" sounds like "Visual Studio Code", which needs a lot of setup. Have you completed **all** of this ? https://code.visualstudio.com/docs/setup/mac – BoP Feb 26 '22 at 10:41
  • @kaylum I don't get any prompt to enter a number in the output area. I'm very new to VS code too –  Feb 26 '22 at 10:45
  • As I said, `printf` is line buffered so your printf which doesn't have a newline won't output anything unless you call `fflush(stout)`. Did you add that as I suggested? See: [Why does printf not flush after the call unless a newline is in the format string?](https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – kaylum Feb 26 '22 at 10:47
  • 2
    What does that mean "When I run the code and stop it"? How do you stop it? Did you try to type a number and press ? – Gerhardh Feb 26 '22 at 10:51
  • 1
    What happens if you just input a number after running the program? – जलजनक Feb 26 '22 at 10:58
  • "the program never runs" What makes you think so? How can it not run if you can stop it? – Yunnosch Feb 26 '22 at 10:58
  • Can you get a HelloWorld run if you try it the same way as you try it with the shown code? – Yunnosch Feb 26 '22 at 11:02
  • Possibly related: [Visual Studio Code: Take Input From User](https://stackoverflow.com/questions/36964949) – Steve Summit Feb 26 '22 at 12:06
  • Possibly related: [My C program won't print anything even though I tried everything I read on Internet](https://stackoverflow.com/questions/69417152) – Steve Summit Feb 26 '22 at 12:08
  • 1
    For SO to work best, you should respone to comments and answer questions. See [How-to-ask](https://stackoverflow.com/help/how-to-ask): "Post the question and respond to feedback - After you post, leave the question open in your browser for a bit, and see if anyone comments. If you missed an obvious piece of information, be ready to respond by editing your question to include it. " – Gerhardh Feb 26 '22 at 13:24
  • @Yunnosch Yes so far only the printf function works and not the scanf –  Feb 27 '22 at 13:28
  • @kaylum I tried the fflush(stdout); and now it prints the prompt but I cant seem to enter an input –  Feb 27 '22 at 13:33
  • @Gerhardh You know how we can stop a code from running in VS code? Thats what I mean –  Feb 27 '22 at 13:34
  • "so far only the printf function works and not the scanf " What makes you think so? What do you observe to indicate that the scanf does not work? I would store the return value (not the scanned value) into a variable and output it in a following printf() with newline or with a following flush. For what input do you observe the failure? What would you expect to observe in order to be convinced that the scanf does work? Does the second printf work? Does it work once you also used a newline or flush for that? – Yunnosch Feb 27 '22 at 13:51
  • I watched a video and I had to just check the box where it said to run in terminal. It works now –  Feb 27 '22 at 13:54
  • That is one of the answers in the question mentioned by Steve Summit. Somebody please propose that as a duplicate. – Yunnosch Feb 27 '22 at 13:56
  • @Yunnosch My point is that when my code has only a printf function the code runs and the expected output is given but when I run the scanf command nothing is being prompted and I can't input anything. So I saw a video that was more helpful and got my solution –  Feb 27 '22 at 13:56

2 Answers2

0

Add a fflush(stdout); just after the printf(). The problem is that, as you are printing a prompt without a new line, the prompt message is being buffered for output until a \n is output, or until enough data is output to the standard output stream. As none of these happens, you must flush the buffer yourself to get some output.

If you provide a number (even if not seeing the prompt) The program will output the prompt and the result line after the prompt. As in

$ a.out
34
Enter an integer: Number = 34$ _

(The final $ _ is the new prompt and the cursor, as you have not included any newlines in your output, the output gets bad in the screen, all in one single line)

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
0

A simple fix is to go into the Visual Studio Code settings via preferences. Click on "extensions" and look for "Run Code Configuration" and check the box next to "Run in Terminal".