Tom's wiki

C

The C programming language. The father of most modern programming languages.

Learn

Notes

#todo Sort out.

Best practices

Standards

Name Year Description
K&R C 1978 The original version named after B.Kernighan and D.Ritchie
ANSI C 1989 The first language specification published by ANSI
C99 1999 The most common version at the moment
C11 2011 A major version introduced Unicode and multithreading support
C23 2023 Not yet released

💡 Hint
You can force the compiler to use a specific standard with the -std flag.

Preprocessor

Directive Description
#include includes the contents of a file during compilation
#define replaces a token by an arbitrary sequence of characters
#if, #elif, #else, #endif tests whether a condition is true
#ifdef, #ifndef tests whether a name is (not) defined
#error stops compilation with an error
#embed (C23) includes the contents of a binary file
#pragma once (non-standard) tells the compiler to include a file only once

Modifiers

Type qualifiers:

Keyword Description
const marks a variable as readonly; for a pointer marks the value it points to
restrict tells the compiler that this memory block will only be accessed by a single pointer
register tells the compiler that this value should be put into a register (so its address cannot be taken with &)
volatile tells the compiler that this value might change outside the program

Storage-class specifiers:

Keyword Lifetime Visibility
auto block scope block scope
static entire program block scope (inside block) / file scope (outside block)
extern entire program entire program

Defaults: auto for block scope objects, extern for file scope objects.

Stdlib

https://en.wikipedia.org/wiki/C_standard_library

Implementations:

Included in the ANSI standard:

Header file Contents Examples
<stdio.h> input and output printf(), fopen()
<stdlib.h> utility functions malloc(), free(), exit()
<ctype.h> character type tests isalpha(), isdigit(), isspace()
<string.h> string functions strlen(), strcmp(), strcpy()
<math.h> mathematical functions log(), pow(), sqrt()
<assert.h> diagnostics assert(), static_assert()
<stdarg.h> variable argument lists va_start(), va_arg(), va_end()
<signal.h> interrupt signals signal()
<time.h> date and time functions time(), clock()
<limits.h> limits of number types INT_MIN/INT_MAX

Compilers

Tooling

Code style

See also