C Documentation
Concise, example-driven C reference. Read the concept, see the code, then practice it in a Coddy journey.
Getting Started
- What Is CC is a small, fast, compiled language from 1972 that still runs operating systems, databases, and nearly every embedded device. Here is what it is, where it runs, and why it is worth learning in 2026.
- Install C (GCC)Install a working C compiler on any system: MinGW-w64 on Windows, Apple's clang on macOS, and gcc on Linux - plus how to check the install worked and compile your first file.
- Compile and RunTurn a .c file into a running program: the gcc command, what preprocessing, compiling, and linking actually do, the flags worth using from day one, and how to read the errors when it fails.
- C SyntaxEvery C program has the same skeleton: includes, a main function, statements ending in semicolons, and blocks in braces. Here is what each part does and the rules the compiler actually enforces.
- CommentsC has two comment styles - single-line // and multi-line /* */ - with different histories and one nesting trap. Here is how to use both, plus what is worth commenting and what is not.
Variables & Types
- VariablesHow to declare a variable in C, the difference between declaration and initialization, naming rules, declaring several at once, and why an uninitialized variable is the most common beginner bug.
- Data TypesEvery C variable has a type that fixes its size and what it can hold. Here are all the basic types, their real sizes, signed versus unsigned, sizeof, limits.h, and what happens on overflow.
- ConstantsThree ways to name a value that never changes in C - the const keyword, #define macros, and enum constants - what each one actually is, and which to reach for when.
- OperatorsEvery C operator worth knowing - arithmetic, assignment, comparison, logical, and increment - plus the integer division trap, how modulo behaves with negatives, and a precedence table you can trust.
- BooleansC had no boolean type until C99. Zero is false, everything else is true, comparisons yield int - and stdbool.h gives you bool, true, and false on top. Plus the = vs == bug this design enables.
- Type CastingC converts between types constantly - sometimes because you asked with a cast, and more often on its own. Here are the promotion rules, the explicit cast syntax, and the conversions that silently lose data.
Control Flow
- if / elseHow C decides: the if statement, else, else if chains, nesting, and the ternary operator - plus the truthiness rule (0 is false, everything else is true) and the `=` vs `==` bug that silently breaks conditions.
- switchHow the C switch statement picks one branch from a list of constants - case labels, why break matters, deliberate and accidental fall-through, default, switching on chars and enums, and when switch beats an else-if chain.
- for LoopHow to repeat code with the C for loop - the three-part header, counting up and down, walking arrays with the sizeof trick, nested loops, infinite loops, and the off-by-one and unsigned bugs that catch everyone.
- while LoopHow the C while loop repeats until a condition changes - the condition-first rule, sentinel loops, reading input until EOF, while(1), and converting between for and while.
- do-while LoopC's body-first loop: how do-while guarantees one pass before testing, why it ends with a semicolon, the menu and retry patterns it was made for, and how it differs from a plain while loop.
- break & continuebreak leaves a loop or a switch; continue skips to the next pass. How each behaves in for, while, and do-while, how to escape nested loops with a flag or goto, and the misuses that make loops hard to follow.
Functions
- functionsHow to define and call functions in C - the return type, parameters, return values, void functions, why decomposition matters, and a full worked example built from small named pieces.
- Function ParametersC passes everything by value - functions get copies, never originals. What that means in practice, how pointers simulate pass-by-reference, why arrays behave differently, and how to return several results.
- Function PrototypesWhy C needs to see a function's shape before you call it - writing prototypes, fixing implicit-declaration errors, putting prototypes in header files, and the real difference between f(void) and f().
- recursionHow a C function calls itself - the base case that stops it, factorial and Fibonacci worked through, why naive Fibonacci is catastrophically slow, what stack overflow really is, and when a loop is the better answer.
- scopeWhere a C variable is visible and how long it lives - block scope, function parameters, file-scope globals, static local variables that survive between calls, static functions, shadowing, and why globals cause trouble.
Pointers
- PointersA pointer is a variable that stores a memory address. This page builds the idea from the ground up - the & and * operators, declaring and dereferencing pointers, why pointer types matter, and the swap() function that shows why pointers exist at all.
- Pointer ArithmeticAdding 1 to a pointer does not add 1 byte - it moves to the next object of that type. This page covers ptr+1, increment and decrement, pointer differences, comparisons, walking an array with a pointer, and the one-past-the-end rule.
- Pointers and ArraysIn C an array name turns into a pointer to its first element almost everywhere you use it. This page explains that decay, why arr[i] is literally *(arr+i), why array size must travel separately into functions, and how a pointer to an array differs from an array of pointers.
- Function PointersFunctions have addresses too, and a function pointer stores one. This page decodes the declaration syntax, shows how typedef makes it readable, and builds up to callbacks, qsort with a custom comparator, and dispatch tables.
- NULL PointersNULL is the address a pointer holds when it points at nothing. This page covers what NULL really is, why dereferencing it crashes, how it differs from 0 and from an uninitialized pointer, and the defensive patterns that keep null bugs out of your code.
Arrays & Strings
- ArraysHow to declare and initialize arrays in C, index them from zero, compute the length with sizeof, loop over the elements, and why reading past the end is undefined behavior rather than an error message.
- Multidimensional ArraysHow to declare, initialize, and loop over 2D arrays in C, what row-major layout actually means in memory, why passing a 2D array to a function requires the column count, and a worked matrix example.
- StringsC has no string type - a string is a char array ending in a '\0' byte. This page builds that mental model, then covers literals, printing, iterating, and why you cannot copy a string with =.
- String FunctionsA working tour of <string.h>: measuring with strlen, copying with strcpy and strncpy, joining with strcat, comparing with strcmp, searching with strchr and strstr - and how to size buffers so none of them overflow.
- String ConversionConverting between text and numbers in C: why atoi cannot report an error, how to use strtol and strtod correctly with endptr and errno, turning numbers into strings with snprintf, and single-digit tricks with '0'.
Dynamic Memory
- Dynamic MemoryWhy the heap exists, how malloc hands you memory whose size is decided at runtime, the p = malloc(n * sizeof *p) idiom, checking for NULL, freeing exactly once, and a dynamic array worked end to end.
- calloc and realloccalloc gives you zeroed memory and multiplies the size safely; realloc grows a block you already filled. This page covers both, the temporary-pointer idiom that keeps a failed realloc from leaking, and when calloc beats malloc plus memset.
- Memory LeaksWhat a leak really is, the three ways C programs produce them, the ownership discipline that prevents them, and how to find the rest with valgrind and -fsanitize=address - ending with a leaky program fixed step by step.
- Stack vs HeapWhere your data actually lives: automatic storage on the stack, dynamic storage on the heap, and static storage as the third region - with the classic dangling-pointer bug from returning a local, and a rule for choosing.
Structs, Unions & Enums
- structsHow to group related values into one type with a C struct - declaring it, accessing members with the dot operator, designated initializers, arrays of structs, nesting, and the copy-on-pass rule that surprises everyone.
- Structs & PointersHow to point at a struct in C - the arrow operator, why (*p).x needs its parentheses, passing structs by pointer to mutate them or avoid copies, malloc'ing a struct, and building a linked list node.
- typedefHow typedef gives an existing C type a new name - the typedef struct idiom with and without a tag, typedefs for function pointers and arrays, and the one case where a typedef hurts: hiding a pointer.
- unionsHow a C union stores several types in the same bytes - why its size is the largest member, why you must read the member you last wrote, and how a tagged union (enum plus union) makes it safe.
- enumsHow to define an enum in C - automatic numbering, explicit values, using enums in a switch so the compiler catches missing cases, converting an enum to a string, and the naming conventions that keep them readable.
Input & Output
- printfHow printf actually works in C - the format string model, the specifiers you use daily, width and precision for aligned output, printing floats sensibly, the return value, and why printf(user_input) is a security hole.
- scanfHow to read input with scanf in C - why the & is required, how whitespace is handled, the %s buffer overflow and its width fix, checking the return value, the leftover-newline bug, and fgets as the robust alternative.
- Format SpecifiersEvery C format specifier in one place - what %d, %s, %c, %f, %p and the rest mean, the length modifiers l, ll, h and z, width, precision and flags, and which ones differ between printf and scanf.
- File HandlingHow to read and write files in C - the fopen mode table, checking for NULL, writing with fprintf and fputc, reading with fgets, fscanf and fgetc, the canonical line-by-line loop, and when to use binary mode.
- Command-Line ArgumentsHow a C program reads its command line - the anatomy of argc and argv, iterating the arguments, what argv[0] holds, converting numbers with strtol instead of atoi, and a small calculator that puts it together.
The Preprocessor
- preprocessorThe preprocessor edits your source text before the compiler ever sees it - pasting in headers, substituting macros, and switching code in or out. Here is the whole directive family and how to look at its output.
- macrosObject-like and function-like macros with #define - why every argument and the whole body need parentheses, the multiple-evaluation trap, multi-line macros with do-while(0), and when a function or const is the better tool.
- Header FilesHow to split a C program across files: what belongs in a .h, what belongs in a .c, include guards that stop double inclusion, compiling several files together, and sharing globals with extern.
- Conditional CompilationCompile different code for different builds with #ifdef, #ifndef, #if, #elif and #else - debug switches, platform branches, defining macros from the command line with -D, and using #if 0 to disable code.
Standard Library
- Math FunctionsA tour of math.h - sqrt, pow, fabs, floor and ceil, round, fmod, the trig and log families, INFINITY and NAN - plus the -lm linker flag that causes the classic undefined reference error.
- Random NumbersHow to generate random numbers in C with rand() and RAND_MAX, why you seed with srand(time(NULL)) exactly once, mapping into a range with % and the small bias it introduces, random doubles, and reproducible sequences.
- Standard LibraryWhat each standard header gives you - stdio.h, stdlib.h, string.h, math.h, time.h, ctype.h, limits.h, stdbool.h, stdint.h and assert.h - with a short working example of each.
Errors & Debugging
- Segmentation FaultA segfault means your program touched memory it does not own. Here are the five causes that account for nearly all of them, each with a minimal example and its fix, plus how to find the exact line with gdb and AddressSanitizer.
- Common ErrorsThe mistakes every C programmer makes at least once - missing semicolons, = instead of ==, implicit declarations, scanf without &, comparing strings with ==, integer division, uninitialized variables and off-by-one - each as symptom, cause, and fix.
- Undefined BehaviorUndefined behavior is code the C standard places no requirements on - so anything may happen, including the optimizer deleting your checks. Here is what causes it, why "it works on my machine" proves nothing, and the flags that catch it.