C Cheat Sheet
Fundamental C syntax including pointers, arrays, structs, memory allocation, and standard library functions for systems programming.
2 PagesBeginnerApr 10, 2026
Basic Syntax
Variables, control flow, and I/O.
c
#include <stdio.h>int main(void) { int age = 30; double pi = 3.14159; char name[] = "Ada"; if (age >= 18) { printf("%s is an adult\n", name); } for (int i = 0; i < 5; i++) { printf("Count: %d\n", i); } return 0;}
Pointers & Arrays
Direct memory access and array decay.
c
int x = 42;int *p = &x; // p holds address of x*p = 100; // dereference: x is now 100int arr[5] = {1, 2, 3, 4, 5};int *ptr = arr; // array decays to pointerprintf("%d\n", *(ptr + 2)); // 3, same as arr[2]void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp;}
Memory Management
Manual heap allocation with malloc/free.
c
#include <stdlib.h>int *arr = malloc(5 * sizeof(int)); // allocate on heapif (arr == NULL) { // always check for failure return 1;}arr[0] = 10;arr = realloc(arr, 10 * sizeof(int)); // grow the blockfree(arr); // release memoryarr = NULL; // avoid dangling pointer
Standard Library Functions
Commonly used string.h and stdio.h functions.
- strlen(s)- returns the length of a null-terminated string
- strcpy(dst, src)- copies src into dst (unsafe if dst too small)
- strcmp(a, b)- returns 0 if strings a and b are equal
- memcpy(dst, src, n)- copies n bytes from src to dst
- sizeof(x)- compile-time operator returning size in bytes
- printf/scanf- formatted output/input with format specifiers like %d, %s, %f
- fopen/fclose- open and close file streams
Structs, Unions & Typedef
Aggregate types and naming them cleanly.
c
typedef struct { char name[32]; int age;} Person;Person p = { .name = "Ada", .age = 36 }; // designated init// Union: members share the same memorytypedef union { int i; float f; char bytes[4];} Value;Value v; v.f = 3.14f; // reading v.i now is implementation-defined
Function Pointers
Store and pass functions as callbacks.
c
#include <stdlib.h>int cmp_int(const void* a, const void* b) { return (*(const int*)a) - (*(const int*)b);}int main(void) { int arr[] = {5, 2, 8, 1}; qsort(arr, 4, sizeof(int), cmp_int); // typedef a callback signature typedef int (*Comparator)(const void*, const void*); Comparator fn = cmp_int; return fn(&arr[0], &arr[1]);}
File I/O
Read and write files with the stdio buffered API.
c
#include <stdio.h>FILE* f = fopen("data.txt", "w");if (!f) { perror("fopen"); return 1; }fprintf(f, "count=%d\n", 42);fclose(f);char line[256];f = fopen("data.txt", "r");while (fgets(line, sizeof(line), f)) { printf("%s", line);}fclose(f);
printf/scanf Format Specifiers
Common conversion specifiers for formatted I/O.
- %d / %i- signed decimal integer
- %u- unsigned decimal integer
- %f- floating point in decimal notation
- %e / %g- scientific / shortest of %e or %f
- %x- unsigned integer in hexadecimal
- %c- single character
- %s- null-terminated string
- %p- pointer address
- %zu- value of type size_t
- %ld / %lld- long / long long integer
Pro Tip
Always check the return value of malloc/calloc/realloc for NULL, and set freed pointers to NULL to avoid dangling-pointer bugs.
Was this cheat sheet helpful?
Explore Topics
#CCheatSheet#Programming#Beginner#BasicSyntax#PointersArrays#MemoryManagement#StandardLibraryFunctions#Functions#DataStructures#CheatSheet#SkillVeris