Quick Lessons

Code Style Guide for C Programming

C source code formatted to a consistent style guide

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

  1. Consistent style makes C code easier to read and review.
  2. Function braces go on their own line in this style.
  3. Leave a blank line after declarations and remove trailing whitespace.
  4. Document every function with a short header comment.
  5. Automate formatting with tools like clang-format.

Watch & learn

A brief look at c++ code style guides | Modern Cpp series Ep. 85Mike Shah · YouTube

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.

Quick Lessons#c programming#code style#clean code#betty#clang-format

Comments

No comments yet. Start the conversation.

Comments are reviewed before they appear. Be kind; one link max.

Learn it hands-on at AL Academy

Free practical programs in software, automation and IT, with labs and certificates.

Open AL Academy ↗
Keep reading

Related articles