How to use the C / C++ Formatter
Format C or C++ code in three steps:
1
Paste or upload your code
Paste C or C++ code into the input panel or upload a .c, .cpp, .cc, .h, or .hpp file using the upload button.
2
Choose language, indent size, and brace style
Toggle between C and C++ mode. Select 2 or 4 space indentation. Choose K&R, Allman, or GNU brace placement style.
3
Format, copy, or download
Click 'Format C' or 'Format C++', then copy the result or download it as the appropriate source file extension.
When to use this tool
Use the C/C++ formatter for code that needs consistent style before sharing or committing:
- →Reformatting C code from embedded systems or microcontroller projects that lost consistent brace alignment
- →Converting between coding styles — K&R for Linux kernel style, Allman for ANSI/MISRA C compliance
- →Quickly formatting C++ snippets from Stack Overflow or documentation for use in a project
- →Standardising header files (.h/.hpp) before inclusion in a shared library or SDK
- →Preparing competitive programming solutions in C++ for readable submission before the deadline
- →Tidying C++ examples for blog posts, textbooks, and technical documentation
Frequently asked questions
Q:What is the difference between K&R, Allman, and GNU brace styles?
K&R style (Kernighan & Ritchie, used by Linux kernel) puts the opening brace on the same line as the control statement. Allman style (ANSI C, common in Windows/MISRA environments) puts the opening brace on a new line at the same indentation level. GNU style is similar to Allman but indents the opening brace one extra half-indent, which is the convention enforced in GNU project source code.
Q:Do preprocessor directives like #include and #define get indented?
No — preprocessor directives are always placed at column zero (no leading indentation), following the convention used in virtually all C and C++ codebases. This matches the behaviour of clang-format and is required for correct preprocessing by most compilers and build tools.
Q:Does C++ mode handle templates, namespaces, and STL types differently?
Yes — in C++ mode, additional keywords (class, template, namespace, typename, constexpr, noexcept, override, final, nullptr, and more) and common STL types (std::string, std::vector, std::map, std::shared_ptr) are recognised and highlighted. C mode only highlights C standard keywords and types.
Q:Can it format header files with both declarations and implementations?
Yes — the formatter works on any valid C or C++ source text, including header files (.h, .hpp) that contain class declarations, inline function definitions, template implementations, and preprocessor guards (#ifndef, #pragma once). All are indented correctly.
Q:Is this suitable for embedded C code or MISRA C compliance projects?
The formatter handles standard C syntax including embedded patterns like interrupt service routines, register manipulation, bitfield struct declarations, and function pointer typedefs. For MISRA C compliance teams, Allman style is recommended. The formatter does not enforce MISRA rules themselves — use a dedicated MISRA checker for that.
Q:How does it compare to running clang-format locally?
This formatter is a fast, browser-based beautifier that handles the most common indentation needs without any installation. clang-format is far more powerful and handles edge cases like trailing commas, alignment of consecutive assignments, and complex template formatting. Use this tool for quick formatting of snippets; use clang-format in your build pipeline for strict project-wide enforcement.