Cheat Sheets
Functions · Study material
C Functions Cheat Sheet
| Task | Code |
|---|---|
| Prototype | double area(double r); |
| No parameters | int menu(void); |
| Change the caller's variable | void inc(int *p) { (*p)++; } · call inc(&x); |
| Read-only array parameter | int sum(const int *a, size_t n); |
| Keep a value between calls | static int count = 0; (inside the function) |
| Private to this file | static void helper(void); |
| Function pointer | int (*op)(int, int) = add; op(2, 3); |
| Callback parameter | void each(int *a, size_t n, void (*f)(int *)); |
| Variable arguments | int sum(int n, ...); + va_list |
| Several results | return a struct, or use output pointers |
| C23 attribute | Effect |
|---|---|
[[nodiscard]] | warn if the return value is ignored |
[[maybe_unused]] | silence "unused" warnings |
[[deprecated("use v2")]] | warn when the function is used |
[[noreturn]] | the function never returns |
Recursion checklist: a base case, progress towards it on every call, and a depth that fits the stack.