Why Is scanf()
Not Working in VS Code?
When using the Code Runner extension in VS Code, many beginners face this common problem:
Your program runs, but it skips the input or crashes when it reaches scanf()
or cin
.
That’s because Code Runner runs your code in the “OUTPUT” tab, which doesn’t support keyboard input. To fix this, we must make Code Runner use the terminal, which allows interactive input.
How to Make Code Runner Use the Terminal
Here’s a simple step-by-step guide to fix this issue in Visual Studio Code.
Step 1: Open VS Code Settings (JSON)
- Press
Ctrl + Shift + P
(Windows/Linux) orCmd + Shift + P
(Mac) - Type:
Preferences: Open User Settings (JSON)
- Click the option to open your
settings.json
file.
Step 2: Add Code Runner Terminal Settings
Inside the JSON file, add these lines:
"code-runner.runInTerminal": true, "code-runner.clearPreviousOutput": true
These settings will:
-
Make Code Runner use the integrated terminal (which supports input)
-
Clear the old output before running new code
Example Full Settings:
{ "editor.mouseWheelZoom": true, "files.autoSave": "afterDelay", "workbench.iconTheme": "material-icon-theme", "code-runner.runInTerminal": true, "code-runner.clearPreviousOutput": true }
⚠️ Make sure commas are placed correctly between each line.
Run Your Program with Input Support
-
Open any
.c
or.cpp
file. -
Click the Run Code button from Code Runner (▶️).
-
The program now runs in the Terminal, and
scanf()
will work!
You can now input values from your keyboard just like a normal console program.
Works for C, C++, Python, Java, and More
This method isn’t just for C. It works for any programming language where input is needed — such as:
-
Python (
input()
) -
Java (
Scanner
) -
C++ (
cin
) -
C (
)scanf()
Final Thoughts
Running your code in the VS Code terminal instead of the OUTPUT tab is crucial for working with user input.
With just one setting, you fix the issue and make your VS Code environment work properly for all interactive programs.
Got Stuck?
Let me know in the comments if it worked or if you want a full setup guide for C programming in VS Code (with MinGW/gcc)!