Skip to content

Commit

Permalink
Prevent ClangFormat From Breaking Before Curly Braces
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelazaroff committed Dec 22, 2023
1 parent 8653ae9 commit 23b677a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions c/prevent-clangformat-from-breaking-before-curly-braces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Prevent ClangFormat From Breaking Before Curly Braces

For some unfathomable reason, C and C++ developers sometimes put line breaks before curly braces:

```c
int main()
{
for (int i = 0; i < 10; i++)
{
printf("%d\n", i);
}
}
```

It's also the default formatting style in the code formatter [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html).

Fortunately, it's configurable by setting the [`BreakBeforeBraces` option](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#breakbeforebraces) to `Attach` in your `.clang-format` file:

```
BreakBeforeBraces: Attach
```

Much more readable:

```c
int main() {
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
}
```

0 comments on commit 23b677a

Please sign in to comment.