In VSCode, install the C/C++ extension from the extensions menu. If you don't know how to find extensions, in the menu bar, go View > Extensions.
Open some folder to work in. For example, go File > Open, then choose a folder.
The contents of your directory should appear in the explorer.
Create a new file in the root director called .clang-format
Paste in the following configuration, and save.
---
# options: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
BasedOnStyle: LLVM
IndentWidth: 4
# Must be 80 characters or less!
ColumnLimit: 80
# does (int) x instead of (int)x
SpaceAfterCStyleCast: true
# spaces, not tabs!
UseTab: Never
# if (x) doStuff() is not allowed, bad style
AllowShortIfStatementsOnASingleLine: false
#
AlignTrailingComments: true
SpacesBeforeTrailingComments: 3
# change the next line to All for Alistair's textbook style
AlwaysBreakAfterReturnType: TopLevelDefinitions
# #define SHORT_NAME 42
# #define LONGER_NAME 0x007f # does nice spacing for macros
AlignConsecutiveMacros: Consecutive
# use \n instead of \r\n
UseCRLF: false
In settings (CTRL-,
or CMD-,
), you can turn on "Format On Save", by searching for it.