Home Swift UNIX C Assembly Go Web MCU Research Non-Tech

Formatting C/C++ Code in VS Code: Presets, Key-Value Pairs, and .clang-format Configuration

2025-05-07 | C | #Words: 1878 | 中文原版

Microsoft’s official C/C++ extension for VS Code offers features like syntax highlighting, library recognition, and—most importantly for this article—code formatting. Code formatting automatically adjusts your code to follow a specific standard, making it easier to read. In many large projects, formatting code to meet project standards is required before submission (though some projects use custom formatters, which we won’t discuss here).

However, the default formatting may not meet your needs. While you could spend time writing a ClangFormat configuration file (since VS Code uses ClangFormat under the hood), most people don’t need such depth—they only want to tweak a few parameters. For that reason, we’ll cover the configuration file method last.

Settings Location

First, you need to know where to adjust the formatting settings. Go to the extension’s settings and look for the “Formatting” section:

Formatting section in VS Code C/C++ extension settings

Here, you’ll find three ways to configure code formatting:

  1. Presets: Includes Visual Studio (default), LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, GNU, and none.
  2. Key-Value Pairs: A more granular approach using {key: value, ...} to adjust specific parameters (builds on presets).
  3. .clang-format Configuration File: VS Code uses this if it exists; otherwise, it falls back to the extension’s settings.

Below, we’ll explain the differences and details of each method.

Demo Code

We’ll use the following messy code to demonstrate formatting changes—it’s short and clearly shows the impact of different settings:

#include <iostream>

int main() {
for (int i = 0; i < 5; ++i) {std::cout << i << " ";
}return 0;
}

1. Presets

We’ll only cover the most commonly used presets—feel free to experiment with the others on your own.

Visual Studio (Default)

The default preset is Visual Studio. To format the entire document:

  1. Right-click in the editor.
  2. Select “Format Document” (as shown below):
Format Document option in VS Code right-click menu

Formatted result:

#include <iostream>

int main()
{
    for (int i = 0; i < 5; ++i)
    {
        std::cout << i << " ";
    }
    return 0;
}

Key features:

LLVM

Using the LLVM preset:

#include <iostream>

int main() {
  for (int i = 0; i < 5; ++i) {
    std::cout << i << " ";
  }
  return 0;
}

Key features:

Even though Clang and its tools are developed by LLVM, LLVM has its own formatting defaults—this preset is not recommended for most projects.

GNU

The GNU preset has a unique style:

#include <iostream>

int
main ()
{
  for (int i = 0; i < 5; ++i)
    {
      std::cout << i << " ";
    }
  return 0;
}

Key features:

A Note on Indentation

Let’s dive deeper into indentation—relevant here because GNU is closely tied to Linux, and VS Code lacks a Linux-specific preset:

Google

The Google preset is similar to LLVM:

#include <iostream>

int main() {
  for (int i = 0; i < 5; ++i) {
    std::cout << i << " ";
  }
  return 0;
}

Key features:

That covers the main presets—let’s move to more customizable options.

2. Key-Value Pairs

The extension’s settings provide a sample key-value pair configuration:

{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false }

To improve readability, we’ve formatted it with line breaks:

{ 
    BasedOnStyle: LLVM, 
    UseTab: Never, 
    IndentWidth: 4, 
    TabWidth: 4, 
    BreakBeforeBraces: Allman, 
    AllowShortIfStatementsOnASingleLine: false, 
    IndentCaseLabels: false, 
    ColumnLimit: 0, 
    AccessModifierOffset: -4, 
    NamespaceIndentation: All, 
    FixNamespaceComments: false
}

This configuration builds on the LLVM preset and overrides specific parameters. Let’s test it:

Formatted result:

#include <iostream>

int main()
{
    for (int i = 0; i < 5; ++i)
    {
        std::cout << i << " ";
    }
    return 0;
}

Compared to the default LLVM preset, the indentation is now 4 spaces (controlled by these lines):

IndentWidth: 4, 
TabWidth: 4, 

To move braces back to the same line as the statement, change BreakBeforeBraces to Attach:

BreakBeforeBraces: Attach, 

Updated result:

#include <iostream>

int main() {
    for (int i = 0; i < 5; ++i) {
        std::cout << i << " ";
    }
    return 0;
}

We won’t cover all parameters here (we don’t want to repeat the official documentation). For a complete list of options, see Clang-Format Style Options.

3. .clang-format Configuration File

The .clang-format file (YAML format) is the most flexible option—place it in your project’s root directory:

Creating a .clang-format file in the project root directory

The configuration is similar to the key-value pair method but uses YAML syntax. For example:

---
Language: Cpp
BasedOnStyle: LLVM
UseTab: Never
IndentWidth: 3
TabWidth: 3
BreakBeforeBraces: Attach
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 0
AccessModifierOffset: -4
NamespaceIndentation: All
FixNamespaceComments: false
---

Formatted result (note the 3-space indentation):

#include <iostream>

int main() {
   for (int i = 0; i < 5; ++i) {
      std::cout << i << " ";
   }
   return 0;
}

Multi-Language Support

You can configure formatting for different languages in a single .clang-format file. Example from the official documentation:

---
Language: Cpp
# Force pointers to the type for C++.
DerivePointerAlignment: false
PointerAlignment: Left
---
Language: JavaScript
# Use 100 columns for JS.
ColumnLimit: 100
---

This is incredibly useful for large projects with mixed languages.

For all available settings, refer to the official ClangFormat documentation.

I hope these will help someone in need~

References

While GNU and Linux have a complex relationship (and GNU has made significant contributions), Linux’s coding style documentation is far more accessible. As Linus Torvalds writes in the Linux guide:

First off, I’d suggest printing out a copy of the GNU coding standards, and NOT read it. Burn them, it’s a great symbolic gesture.

Coincidentally, the two Linus quotes we referenced are nearly adjacent in the document:

Adjacent Linus quotes in the Linux kernel coding style documentation

Coding styles are subjective—different projects use different standards, and no style is inherently “better” than others. Large companies like Google and Apple also have their own guidelines. If we had to recommend one guide to read first, it would be the Linux kernel’s—it’s concise, includes examples, and explains the “why” behind the rules. The others are easier to understand once you have a foundational grasp of coding style principles.

Slide from CppCon talk on code indentation