Code Style Guide for C Programming

TL;DR
A practical style guide for C that fixes the most common style-checker errors: brace placement, indentation of conditionals, blank lines after declarations, trailing whitespace, tab stops, return statements, unnecessary braces and missing function documentation, plus tools like clang-format to automate it.
On this page
Writing clean and consistent C code is essential for readability, collaboration, and passing style checks. Below are common issues, how to fix them, and examples formatted like a terminal for clarity.
1. Open Braces { Should Be on the Next Line
Error Messages:
- ERROR: that open brace { should be on the next line
- ERROR: open brace ‘{’ following function declarations go on the next line
Incorrect:
int main() {
// code
}
Correct:
int main()
{
// code
}
Fix with sed:
find . -path './.git' -prune -o \( -name '*.c' -o -name '*.h' \) -type f -exec sed -i ':a;N;$!ba;s/\)\s*{/)\n{/g' {} +
-–
2. Suspect Code Indent for Conditional Statements
Incorrect:
if (condition) {
do_something();
}
Correct:
if (condition)
{
do_something();
}
Fix:
find . -path './.git' -prune -o \( -name '*.c' -o -name '*.h' \) -type f -exec indent -kr -i8 -ts8 -sob {} +
-–
3. Missing a Blank Line After Declarations
Incorrect:
int a;
int b;
Correct:
int a;
int b;
Fix:
find . -path './.git' -prune -o \( -name '*.c' -o -name '*.h' \) -type f -exec sed -i '/;\s*$/G' {} +
-–
4. Trailing Whitespace
Fix:
find . -path './.git' -prune -o \( -name '*.c' -o -name '*.h' \) -type f -exec sed -i 's/[ \t]*$//' {} +
-–
5. Statements Should Start on a Tabstop
Incorrect:
if (condition)
{
do_something();
}
Correct:
if (condition)
{
do_something();
}
-–
6. Parentheses Required on Return Statements
Incorrect:
return value;
Correct:
return (value);
Fix:
find . -path './.git' -prune -o \( -name '*.c' -o -name '*.h' \) -type f -exec sed -i 's/return\s\+\([^;]\+\);/return (\1);/' {} +
-–
7. Braces {} Not Needed for Single Statements
Incorrect:
if (condition) {
do_something();
}
Correct:
if (condition)
do_something();
-–
8. Missing Function Documentation
Before:
int add(int a, int b)
{
return (a + b);
}
After:
/**
* add - Adds two integers.
* @a: First integer
* @b: Second integer
*
* Return: Sum of a and b
*/
int add(int a, int b)
{
return (a + b);
}
-–
9. Additional Recommendations
To keep your code clean automatically, consider using tools like clang-format. You can also integrate formatting into your editor to avoid style errors completely.
Consistent formatting isn’t just about passing checks — it makes your code easier to read, debug, and maintain.
Key takeaways 5
- Consistent style makes C code easier to read and review.
- Function braces go on their own line in this style.
- Leave a blank line after declarations and remove trailing whitespace.
- Document every function with a short header comment.
- Automate formatting with tools like clang-format.
Watch & learn
Frequently asked questions
Why does code style matter in C?
Consistent style makes code easier to read, review and maintain, and many schools and projects enforce it with style checkers that reject non-compliant code.
How do I remove trailing whitespace in C files?
Configure your editor to strip it on save, or run a command such as sed -i 's/[[:space:]]*$//' file.c.
What is clang-format?
clang-format is a tool that automatically formats C and C++ code according to a configurable style, so formatting stays consistent without manual effort.
Learn it hands-on at AL Academy
Free practical programs in software, automation and IT, with labs and certificates.
Related articles

How to Become a Truly "Unremarkable" Developer: The Insider's Guide
Having spent years working with developers, I’ve gathered some priceless "tips" to help you master the art of being an outstandingly bad developer.

Overview of Unix Commands for C Development
Useful Unix commands for C projects: converting spaces to tabs, fixing DOS line endings, enforcing code style and compiling and debugging.

Understanding C Programming
C is an imperative (procedural) language used for system programming. All code must be inside functions.

Comments
No comments yet. Start the conversation.