Cover Image for How to run a C program in Visual Studio Code
241 views

How to run a C program in Visual Studio Code

Running a C program in Visual Studio Code (VS Code) is a straightforward process. Here’s a step-by-step guide on how to set up and run a C program in VS Code:

  1. Install Visual Studio Code:
    If you haven’t already, download and install Visual Studio Code from the official website: https://code.visualstudio.com/
  2. Install the C/C++ Extension:
    VS Code doesn’t have built-in C/C++ support, so you’ll need to install the C/C++ extension. Open VS Code, go to the Extensions view by clicking on the square icon in the sidebar or pressing Ctrl+Shift+X, and search for “C/C++.” Install the extension provided by Microsoft.
  3. Install a C Compiler:
    You’ll need a C compiler to compile and run your C programs. If you don’t have one installed, consider using GCC (GNU Compiler Collection), which is widely used and supported. You can download GCC for Windows from MinGW (Minimalist GNU for Windows) or use a package manager on macOS or Linux to install it.
  4. Create a C Program:
    Open VS Code, and create a new C source code file. You can do this by clicking on “File” > “New File” and saving it with a .c extension (e.g., my_program.c). Write your C code in this file.
  5. Configure Build Task:
    VS Code allows you to configure build tasks for your C program. To do this, create a tasks.json file in your project directory. Here’s a sample tasks.json file for compiling a C program using GCC:
C
 {
     "version": "2.0.0",
     "tasks": [
         {
             "label": "build",
             "type": "shell",
             "command": "gcc",
             "args": [
                 "-g",
                 "${file}",
                 "-o",
                 "${fileDirname}/${fileBasenameNoExtension}"
             ],
             "group": {
                 "kind": "build",
                 "isDefault": true
             }
         }
     ]
 }

This task configuration assumes you’re using GCC and creates an executable in the same directory as your source file.

  1. Build and Run the Program:
    To compile and run your C program, press Ctrl+Shift+B (Windows/Linux) or Cmd+Shift+B (macOS). This will trigger the build task defined in your tasks.json file. If your program compiles successfully, you should see the executable in the same directory as your source file.
  2. Run the Executable:
    Open the integrated terminal in VS Code (Ctrl+Backtick or View > Terminal) and navigate to the directory containing your executable. Then, run your program by typing ./executable_name (replace executable_name with the actual name of your compiled program).

You should now see the output of your C program in the terminal.

That’s it! You’ve successfully set up and run a C program in Visual Studio Code. You can now write, compile, and execute C programs within the VS Code environment.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS