How to Configure C++ Code Formatting in Visual Studio Code

C++ code can be formatted in VS Code to align with Google, Microsoft, LLVM and Mozilla styles. Learn how.    

Written by Dhananjay Patel
Published on Dec. 13, 2024
Developer working in VS Code
Image: Shutterstock / Built In
Brand Studio Logo

Visual Studio Code (VS Code) is a popular open-source code editor that supports a wide range of programming languages, including C++

VS Code C/C++ Formatter Settings Guide

  1. Open the settings editor in VS Code.
  2. Configure the formatting style by typing C_Cpp.clang_format_fallbackStyle and selecting the desired style.
  3. Customize the formatting style by creating a .clang-format file.

One of the key features of VS Code is its ability to automatically format your code to make it more readable and consistent. In this article, we’ll examine how to configure C++ code formatting in VS Code using the built-in formatting options.

 

3 Steps to Configure C++ in VS Code

Code formatting is an important aspect of writing maintainable and readable code. In C++, there are several popular coding styles used by different organizations and communities, such as Google, Microsoft, LLVM and Mozilla. In this blog post, we will explore how to use the C/C++ extension for VS Code to automatically format C++ code according to these styles.

Step 1: Open the Settings Editor

To configure the C++ formatting options in VS Code, you’ll need to open the settings editor. You can do this by clicking on the gear icon in the lower-left corner of the VS Code window and selecting “Settings.” You can also use the keyboard shortcut “ctrl +” on Windows or “cmd +” on Mac to open the settings editor.

Step 2: Configure the Formatting Style

Once you have the settings editor open, you can search for the C++ formatting options by typing C_Cpp.clang_format_fallbackStyle into the search bar. This will show you the available formatting styles that you can use, such as “Google,” “LLVM” or “Mozilla.”

To configure the formatting style, simply click on the pencil icon next to the C_Cpp.clang_format_fallbackStyle option and select the desired style from the dropdown menu. Once you have selected the style, the C++ code in your project will be automatically formatted according to that style.

Step 3: Customize Additional Formatting Options

In addition to the built-in formatting styles, you can also customize additional formatting options by creating a .clang-format file in your project directory or in your home directory. This file can be used to specify a variety of formatting options, such as indentation, line length, and spacing.

More on Software EngineeringSTD::Optional in C++: A Guide

 

Additional C++ Formatting in VS Code

To create a .clang-format file, simply create a new file in your project directory or in your home directory and name it .clang-format. Then, open the file in a text editor and add your preferred formatting options. Here’s an example of what a .clang-format file might look like:

Google

BasedOnStyle: Google
IndentWidth: 4
TabWidth: 4
UseTab: Never
ColumnLimit: 80

Microsoft

The Microsoft C++ code formatting style is based on the C++ Core Guidelines and is used in many Microsoft projects. Here’s an example .clang-format file that configures the Microsoft style:

Language: Cpp
BasedOnStyle: Google
IndentWidth: 4
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: true
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true

LLVM

The LLVM project uses a code formatting style that is similar to the Google style, but with a few differences. Here’s an example .clang-format file that configures the LLVM style:

Language: Cpp
BasedOnStyle: Google
IndentWidth: 2
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ColumnLimit: 80
PenaltyBreakAssignment: 2
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
SpaceBeforeParens: Never
A tutorial on how to format C++ code in VS Code. | Video: Kenny Yip Coding

More on Software EngineeringA Guide to C++ Or, And and Not Logical Operators

Mozilla

The Mozilla C++ code formatting style is used in many Mozilla projects. Here’s an example .clang-format file that configures the Mozilla style:

Language: Cpp
BasedOnStyle: Google
IndentWidth: 2
UseTab: Never
TabWidth: 8
ColumnLimit: 99
PointerAlignment: Right
AccessModifierOffset: -2
BreakBeforeBraces: Allman
DerivePointerAlignment: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortBlocksOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakTemplateDeclarations: true

Frequently Asked Questions

There are three steps to configure C/C++ code formatting in VS Code:

  1. Open the settings editor in VS Code.
  2. Configure the formatting style by typing C_Cpp.clang_format_fallbackStyle and selecting the desired style.
  3. Customize the formatting style by creating a .clang-format file.

To format your C or C++ code for Google, you need to create a .clang-format file. Then, open the file in your text editor and add your preferred formatting options. Here is the formatting option for Google:

BasedOnStyle: Google
IndentWidth: 4
TabWidth: 4
UseTab: Never
ColumnLimit: 80
Explore Job Matches.