Types, operators and expressions

C Program Structure and Data Types

C is a small, statically typed, compiled language in which the programmer works close to the machine: every object has a type that fixes how many bytes it occupies and how those bytes are interpreted.

10 min readQuick revision

Operators, Precedence and Side Effects

An expression in C computes a value and may also change the state of the program, for example by assigning to a variable or printing. The value depends on how the expression is grouped (precedence and associativity); the state changes depend on when each side effect happens (sequencing).

11 min readQuick revision

Bitwise Operators

The bitwise operators work on the individual bits of integer values. They are how C programs pack flags into a word, extract fields from hardware registers and network headers, compute masks for hashing, and implement fast multiplication or division by powers of two.

8 min readQuick revision

Type Conversions and Integer Arithmetic

C lets you mix types freely in an expression: char with int, int with unsigned, int with double. Before any arithmetic happens, the compiler silently converts the operands to a common type, following fixed rules. Most of the time the result is what you expect.

9 min readQuick revision

The C Preprocessor

Before a C file is compiled, it passes through the preprocessor, which works on tokens rather than on C expressions or types.

7 min readQuick revision

Control flow

Control Flow: Selection and Loops

Control-flow statements decide which statement runs next. C has selection statements (if, if-else, switch), iteration statements (while, do-while, for) and jump statements (break, continue, goto, return).

7 min readQuick revision

Functions, scope, lifetime and storage classes

Functions and Parameter Passing

A function packages a computation behind a name and a list of parameters. Functions make programs readable, let code be reused, and are the mechanism behind recursion.

8 min readQuick revision

Recursion

Recursion

A function is recursive when it calls itself, directly or through other functions. Recursion expresses a problem in terms of smaller instances of the same problem: the factorial of n is n times the factorial of n-1; a binary tree is a root with two smaller binary trees.

6 min readQuick revision

Tracing Recursion and Counting Calls

For a short recursive C function there are four things to determine: what it prints, what it returns, how many times it is called (or how many times some line runs), or what a variable holds afterwards. Each has a reliable mechanical method.

7 min readQuick revision

Pointers and dynamic memory

Pointers

A pointer is a value that holds the address of an object (or function), together with the type of what it points to.

7 min readQuick revision

Dynamic Memory Allocation

Fixed-size automatic arrays have compile-time sizes, but a variable-length automatic array (VLA) can have a runtime size. Automatic objects have block-based lifetimes, not merely “until function return”; a VLA object lives from its declaration until execution leaves its scope.

7 min readQuick revision

Arrays and strings

Arrays and Their Relation to Pointers

A C array is a fixed number of objects of one type stored contiguously in memory. Arrays and pointers are closely tied: in most expressions an array name turns into a pointer to its first element, and the subscript a[i] is defined through pointer arithmetic.

6 min readQuick revision

Strings and the String Library

C has no separate string type. A string is a sequence of characters terminated by the null character '\0' (value 0), stored in an array of char. Unbounded traversal functions such as strlen need a null terminator within accessible storage.

7 min readQuick revision

Structures, unions, enums and typedef

Structures, Unions and Enumerations

A structure groups values of different types into one object, such as a student record with a name and marks, or a list node with a key and a pointer to the next node. A union lets several members share the same storage, so that the object holds one of them at a time.

7 min readQuick revision

Defined, undefined, unspecified and implementation-defined behaviour