To run and debug C in VSCode, first install a C compiler like g or MinGW-w64, the Microsoft C/C extension, and build tools. Then, create a C file and press F5 to generate launch.json for debugging and tasks.json for building. Use Ctrl Shift B to build the code, run the output executable in the terminal, and press F5 to debug with breakpoints. Ensure main() exists, use the -g flag in tasks.json for debug info, and verify executable paths and toolchain setup for smooth operation.
To run and debug a C program in VSCode, you need to set up your environment with the right tools and configuration files. Here's how to do it step by step.
Install Required Tools
Before running or debugging C code, make sure you have the following installed:
- C Compiler: Install g (on Linux/macOS) or MinGW-w64 (on Windows). You can verify installation by running g --version in the terminal.
- VSCode Extensions: Install the C/C extension by Microsoft from the Extensions marketplace.
- Build Tools: On Windows, consider installing MSYS2 or Visual Studio Build Tools if using MSVC.
Set Up launch.json and tasks.json
VSCode uses JSON files to define build and debug behavior. These files go in a .vscode folder in your project root.
To generate them:
- Create a C file (e.g., main.cpp).
- Press F5 to debug. VSCode will prompt you to create a launch.json file.
- Select C (GDB/LLDB) or C (Windows) depending on your system.
- Choose g .exe Build and Debug Active File if using MinGW on Windows, or equivalent.
This automatically creates both launch.json (for debugging) and tasks.json (for building).
Run and Debug Your Program
Once configured:
- Build: Press Ctrl Shift B to compile using the task defined in tasks.json.
- Run: After building, open the integrated terminal (Ctrl `) and run the output executable: ./a.out (Linux/macOS) or a.exe (Windows).
- Debug: Press F5 to start debugging. Set breakpoints by clicking left of the line numbers.
The debugger lets you inspect variables, step through code, and view the call stack.
Tips for Smooth Debugging
- Make sure your main.cpp has a valid int main() function.
- If debugging fails, check the full path to your executable in launch.json.
- Use -g flag in tasks.json to include debug info during compilation.
- On Windows with MinGW, ensure gdb is in your PATH.
Basically, once the toolchain is set up and config files are generated, running and debugging becomes straightforward with F5 and Ctrl Shift B.
The above is the detailed content of How to run and debug a C program in VSCode?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

std::any is a type-safe arbitrary type storage tool introduced by C 17. It can be used to save and retrieve any replicable types. It is suitable for scenarios such as configuring systems, plug-in interfaces, etc. that require heterogeneous data containers; it must include header files when using it, and the values ??are safely extracted through std::any_cast, and checked for types with .type() and typeid to avoid exceptions; it supports custom type storage, but does not support direct storage of move-only types, and can be solved through std::unique_ptr packaging; there is performance overhead and no implicit type conversion, which is suitable for dynamic type requirements of non-performance critical paths, but should not replace templates or polymorphic mechanisms. When using it, it must be ensured that the type matches before extraction is performed to ensure that

std::format is a modern formatting tool introduced by C 20. 1. Supports basic string formatting, such as std::format("Hello,{}!","World"); 2. Parameters can be referenced by position, such as {0}, {1}; 3. Provides digital conversion ({:#x}, {:#b}), fill ({:06}) and alignment ({:>8}); 4. Controls floating point accuracy ({:.2f}) and scientific notation ({:.2e}); 5. The formatting time needs to be passed into std::tm, such as {:%Y-%m-%d}; 6. Supports custom types, and specializes std::forma

Use std::transform combined with ::toupper to convert the string to uppercase, such as std::transform(str.begin(), str.end(), str.begin(),::toupper). This method is suitable for ASCII characters. Modify the original string. If you need to keep the original string, you can copy it first. It is recommended to use the ICU library in Unicode scenarios.

TolinkalibraryinC ,includeheaderswith#includeanduse-I,-L,and-lflags;forexample,g -I/path/to/includemycode.cpp-L/path/to/lib-lmylibrary-omyprogramlinkslibmylibrary.

The volatile keyword is used to prevent the compiler from optimizing variable operations and ensures direct access to memory every read and write. It is suitable for hardware registers, interrupt service programs, and signal processing scenarios.

IfaC classdefinesadestructor,copyconstructor,orcopyassignmentoperator,itshouldalsodefinetheothertwo(RuleofThree);withC 11,addmoveconstructorandmoveassignmentoperator(RuleofFive)toproperlymanageresourcesandavoidundefinedbehaviorfromshallowcopiesordo

Useconstforruntimeconstantswithtypesafety,likeconstintMAX_USERS=100;2.Useconstexprforcompile-timecomputation,suchasconstexprintBUFFER_SIZE=square(10);3.Avoid#defineduetolackoftypecheckingandscope,preferringconstorconstexprforbettercodequalityandmaint

TosetupaPowerShelldevelopmentenvironmentinVSCode,installVSCodeandPowerShell7 ,theninstalltheofficialPowerShellextensionbyMicrosoftforsyntaxhighlighting,IntelliSense,debugging,andscriptanalysis,configureworkspacesettingslikeenablingprofileloadingandfo
