Introduction
A .c file holds a program written in the C programming language, as
plain text. A compiler turns each .c file into an object file, and a
linker joins those into a program or library.
A .h file is a header: declarations of functions, types and constants
that several .c files share by including it. C++ and Objective-C also use
.h headers. On systems where file names are case sensitive, a capital
.C usually means C++ rather than C.
Structure
A C source file is plain text, today usually UTF-8 or plain ASCII, with no header or
signature. Before compiling, the preprocessor handles the lines that start with
#: #include pastes in a header, #define makes a
macro, and #if keeps or drops blocks of text. What remains is a series of
declarations and function definitions, one of which is usually main.
The pages below are a reference to the language. They give clause numbers from C17 (ISO/IEC 9899:2018), and mark what C23 changed.
- File structure – translation units,
.cand.hfiles, the phases of translation, encoding, line endings and include guards - Lexical structure – tokens, comments, keywords by standard, identifiers, constants, string literals, trigraphs and digraphs
- Preprocessor – every directive, macros, predefined macros and conditional inclusion
- Types – the basic types and their minimum ranges, qualifiers,
typedefand conversions - Declarations – declarator syntax, initialisers and designated initialisers
- Storage and linkage – storage classes, linkage, scope, lifetime,
inlineand thread-local storage - Structs, unions and enums – layout, bit-fields and flexible array members
- Arrays and pointers – decay, variable length arrays, pointer arithmetic, null pointers and
restrict - Functions – definitions, prototypes, variadic functions and
main - Expressions – the operator table with precedence and associativity, lvalues and sequence points
- Statements – blocks, labels,
if,switch, loops and jumps - Undefined behaviour – undefined, unspecified and implementation-defined behaviour, and strict aliasing
- Standard library – every header and the standard that added it
- Versions – K&R C, C89, C90, C95, C99, C11, C17 and C23: what each added
Line continuation in the viewer
The viewer follows the second translation phase: a backslash right before a line end
(a line feed, or a carriage return and line feed) joins the next line to the current
one. A // comment that ends in a backslash therefore goes on over the next
line, whatever number of backslashes precede it; a backslash followed by a space does
not join anything. A string, a character constant or an #include name
carries on across the join. A # at the start of a continued line is not a
directive, because the line belongs to the logical line before it, so a macro body such
as #define S(x) \ followed by #x is coloured as one
directive. The same rule applies to C++, CUDA, GLSL and HLSL, which have the C
preprocessor, and not to Java or C#.
Not described
Known, not yet covered
- Reference: the standard library page lists the headers and what each provides, but not the functions, types and macros inside them (
printf,qsort,setlocaleand the rest have no entry). - Reference: compiler extensions are mentioned only where a page meets them. There is no page on GNU C and Clang extensions (statement expressions, computed
goto, nested functions, inlineasm,__int128,__attribute__outside struct layout) or on Microsoft’s (__declspec,__int64), and the_FloatNtypes of the floating-point extension are not described (the decimal types are). - Reference: there are no pages for POSIX, the operating-system interfaces or any other library outside the C standard.
- Viewer: code that
#if 0or any other condition removes is coloured as if it were live; the scanner does not evaluate conditions or expand macros, and a macro body is coloured as ordinary code. - Viewer: a name is a type only if it is a built-in type word or ends in
_t(size_t,uint8_t). A type made bytypedefor astructtag, such asfooinstruct foo, is left plain, and so is_Float128. The attribute names inside[[ ]](nodiscard) are plain, and__attribute__and__asm__are coloured like function calls. - Viewer: trigraphs (
??=) are not recognised as the characters they stand for; the words in the argument of#pragma(as inomp parallel for) are coloured as C. - Viewer: the scanner does not check the program: no declaration, type or syntax errors are reported. A
.hfile is read as C++ when a line begins withclass,namespace,template<orpublic:,private:,protected:; other C++ headers and Objective-C headers are coloured as C. Only the first 1 MiB of a file is read and coloured.
Also used for
- C++/CLI source code – C++ with the CLR extensions for .NET: ref classes, handles, gcnew, properties.
Links
Software
- GCC – the GNU compiler collection, the usual C compiler on Linux (Linux, Windows, macOS)
- Clang – the LLVM project’s C compiler, the default on macOS (Linux, Windows, macOS)
- Visual Studio – Microsoft’s development environment, with the MSVC compiler (Windows)
- Visual Studio Code – free code editor with C extensions (Windows, macOS, Linux)
- CLion – JetBrains’ development environment for C and C++ (Windows, macOS, Linux)
- Xcode – Apple’s development environment, which builds C with Clang (macOS)
- Vim – text editor with C syntax support (Windows, macOS, Linux)
Developers
- libclang – interface to Clang’s parser, with Python bindings (C)
- pycparser – a parser for C source (Python)
- tree-sitter-c – C grammar for the Tree-sitter parser used by editors
Reference
- ISO/IEC JTC1/SC22/WG14 – the committee that maintains the C standard, with its working drafts
- C reference – cppreference.com’s reference to the language and library