how to run program in c in visual studio code

govind patel 41 Reputation points
2025-12-22T02:02:26.8866667+00:00

Hi, I am using visual studio code version 1.107.1 (user setup). for learning programing in "C" . I need help to setup studio code, to create workspace, file and run the debug . I have setup c/c++ , c/c++run extension. I have download gcc compiler and verified in cmd. Is there a simple instruction to set up and start coding in c. ?


Moved from: Community Center | Discuss the Q&A site | Get started on Q&A

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. AI answer

    2025-12-22T02:02:38.9133333+00:00

    To run a C program in Visual Studio Code, follow these steps:

    1. Install the C/C++ Extension: Ensure you have the C/C++ extension installed in Visual Studio Code. You mentioned you have this set up, which is great.
    2. Set Up a Workspace: Create a new folder for your C projects. Open Visual Studio Code and select File > Open Folder... to open your newly created folder.
    3. Create a C File: Inside your workspace folder, create a new file with a .c extension (e.g., hello.c). You can do this by right-clicking in the Explorer pane and selecting New File.
    4. Write Your Code: Write your C code in the newly created file. For example:
         #include <stdio.h>
         
         int main() {
             printf("Hello, World!\n");
             return 0;
         }
      
    5. Configure Build Tasks: You need to set up a build task to compile your C code. Press Ctrl + Shift + B to open the build tasks menu. If prompted, select Create tasks.json file from template and choose Others. Modify the generated tasks.json file to include the following:
         {
             "version": "2.0.0",
             "tasks": [
                 {
                     "label": "build",
                     "type": "shell",
                     "command": "gcc",
                     "args": [
                         "-g",
                         "${file}",
                         "-o",
                         "${fileDirname}/${fileBasenameNoExtension}"
                     ],
                     "group": {
                         "kind": "build",
                         "isDefault": true
                     },
                     "problemMatcher": [
                         "$gcc"
                     ],
                     "detail": "Generated task by gcc"
                 }
             ]
         }
      
    6. Build Your Program: Press Ctrl + Shift + B again to build your program. This will compile your C code and create an executable in the same directory.
    7. Run Your Program: Open the terminal in Visual Studio Code (View > Terminal or `Ctrl + ``) and run your program by typing:
         ./hello
      
      (Replace hello with the name of your executable if different.)
    8. Debugging: To debug your program, you will need to set up a launch configuration. Go to the Run view (left sidebar) and click on create a launch.json file. Choose C/C++: g++ build and debug active file. This will create a launch.json file where you can configure debugging settings.
    9. Start Debugging: Set breakpoints in your code by clicking in the gutter next to the line numbers. Then, press F5 to start debugging your program.

    By following these steps, you should be able to set up Visual Studio Code for C programming and run your code successfully.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

    4 people found this answer helpful.
  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more