Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Concepts and Tools

C coding style and conventions

Practical conventions for readable, safe C code: naming, layout, const, error handling, header organization, comments, and the warnings that enforce them.

C has no official style guide, and well-known projects disagree (the Linux kernel uses tabs and 8-column indents, GNU uses its own braces, many teams follow Google or LLVM style). What matters is that one project follows one style, enforced by a formatter, and that the style supports safe habits: code that is easy to read, hard to misuse and quiet under -Wall -Wextra.

This page lists conventions that most C programmers agree on, with reasons. Adopt the rules that fit your team, write them down, and let a tool such as clang-format apply the layout so reviews are about logic.

Try it

C A small program in good style

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Magic numbers, macros and naming compared

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Naming

  • Functions and variables: lower_snake_case (read_config, line_count). Names tell what, not how.
  • Types: PascalCase or snake_case_t. Avoid the _t suffix if you target POSIX, which reserves it.
  • Constants and macros: UPPER_SNAKE_CASE. Prefer enum or constexpr (C23) to #define for numbers.
  • Prefix public symbols with a module name (list_push, list_free) because C has no namespaces.
  • Do not use reserved names: identifiers beginning with an underscore and a capital letter or two underscores, and standard library names. See Reserved identifiers.
  • No single letters except short loop indexes (i, j).

Layout

  • One statement per line, braces on every if, for and while body, even one-liners; this prevents the "goto fail" class of bugs.
  • Indent consistently (2, 4 or 8 spaces; or tabs), never mix tabs and spaces.
  • Keep lines under about 80-100 characters and functions short enough to read on one screen.
  • Declare variables at the point of first use with the narrowest scope, and initialize them.
  • Put the * with the name (char *name) and declare one pointer per line; int *a, b; makes b a plain int.

Safe habits

  • const everywhere it is true: pointer parameters that are only read (const char *), tables, string literals. See const.
  • Use size_t for sizes and indexes and match the types in comparisons; never mix signed and unsigned silently. See size_t.
  • Check every return value that can fail: malloc, fopen, fclose, scanf, snprintf. Report errors to the caller instead of exiting in library code.
  • Pass sizes with buffers and prefer snprintf over sprintf and fgets over gets (removed in C11).
  • One owner per allocation, with a matching _free function, and set freed pointers to NULL when they stay in scope.
  • Avoid macros that look like functions. If you need one, parenthesize arguments and the whole expression, evaluate each argument once, and use static inline when possible. See Macro pitfalls.
  • Switch over enums without default so the compiler warns about missing cases, as status_text does above.
  • Do not hide pointers in typedefs (typedef struct Foo *FooPtr) unless the type is truly opaque.
  • main(void) and (void) prototypes for functions without parameters (required meaning in C23, important before).

Comments and headers

  • Comment why, not what. Document each function's contract: parameters, ownership, return values, error cases.
  • Every header has include guards (or #pragma once) and includes only what it needs. Never define variables or non-static functions in a header.
  • Include the matching header first in each .c file; it proves the header is self-sufficient.
  • Keep platform-specific code in a few clearly named files.

Tools that enforce style

Tool Job
clang-format Formats layout from a .clang-format file.
clang-tidy, cppcheck Static analysis and style checks.
-Wall -Wextra -Wshadow -Wconversion Compiler warnings; see Compiler flags.
Sanitizers, Valgrind Runtime checking.
.editorconfig Indentation and line endings across editors.