💻

Getting Started with C in VS Code

Aug 18, 2024

C Programming in Visual Studio Code

Creating a New C File

  • Click on the New File button or go to File > New File.
  • Name the file main.c and press Enter.
  • Visual Studio Code will suggest installing C/C++ extensions if not already installed.

Installing C/C++ Extensions

  • Go to the Extensions tab.
  • Search for C/C++.
  • The top extension from Microsoft is recommended.
  • Click Install if it's not already installed.
  • This extension enables:
    • Running and debugging C/C++ programs.
    • Browsing code.
    • IntelliSense capabilities.
  • Reload Visual Studio Code if prompted.

Writing a Simple C Program

  • Navigate to Explorer and open main.c.
  • Write a simple program to add two numbers and display the result.

Running the C Program

  • Using Terminal: Open a new terminal and run using GCC.
  • Using Build Task:
    • Click on Terminal > Run Build Task.
    • Select gcc.exe as the build task.
    • A successful build will create main.exe.

Running Executable Files

  • Execute compiled programs using command line:
    • In PowerShell: Type main.exe and press Enter.
    • Input the first and second numbers when prompted.
  • Alternatively, run in Command Prompt: main.exe.

Debugging the C Program

Setting Breakpoints

  • Click on a line number to toggle breakpoints in the code.
  • View breakpoints in the debug panel.

Running Debugger

  • Click on Run and Debug.
  • Choose C++ GDB as your debugger (requires MinGW).
  • Execution will pause at breakpoints.
  • View variable values and step through the code:
    • Step Over: Execute the next line.
    • Continue: Run until the next breakpoint.

Example Debugging Steps

  • Start with uninitialized variables (showing garbage values).
  • Input numbers when prompted during debugging.
  • Monitor the sum calculation and output.

Conclusion

  • Visual Studio Code provides a robust environment to write, run, and debug C programs.