# Format C/C++ Code Using Clang-Format

## Introduction

In a collaborative project, code formatting software, such as Clang-Format, have been widely used to make sure that the code written by different people have uniform style and format. I have just learned some basic usages of Clang-Format and documented them in this blog post.

## Format Configuration

In order to format our code, we need a configuration file .clang-format. This file could be generated from some pre-configured styles.

To generate .clang-format from style llvm, run the following command in the terminal.

Other candidate styles include google, chromium, mozilla, webkit, microsoft.

We could further modify the .clang-format file using our preferences. Below is my modified format file. More style examples could be found from Clang documentations.

## Run Code Formatting

Clang-Format could format a single file or all files with the same file extension. For example, to format .cpp extension, run the following command in the terminal.

To format all .h, .c, .hpp, .cpp, .cu files together, run the following command in the terminal.

Lei Mao

04-22-2019

08-22-2020