Loading page…
Essential C syntax, memory management, and common patterns for systems programming.
| Type | Size (bytes) | Range | Format |
|---|---|---|---|
| char | 1 | -128 to 127 | %c |
| unsigned char | 1 | 0 to 255 | %c |
| short | 2 | -32,768 to 32,767 | %hd |
| int | 4 | ±2.1×10⁹ | %d |
| long | 8 | ±9.2×10¹⁸ | %ld |
| long long | 8 | ±9.2×10¹⁸ | %lld |
| float | 4 | ±3.4×10³⁸ | %f |
| double | 8 | ±1.7×10³⁰⁸ | %lf |
| void* | 8 | — | %p |
| size_t | 8 | 0 to 2⁶⁴−1 | %zu |
() [] -> . — function call, subscript, member access! ~ ++ -- + - * & (type) sizeof — unary operators* / % — multiplicative+ - — additive<< >> — bitwise shift< <= > >= — relational== != — equality& — bitwise AND^ — bitwise XOR| — bitwise OR&& — logical AND|| — logical ORmalloc — allocate uninitialized memory
int* arr = (int*)malloc(n * sizeof(int));
if (!arr) { perror("malloc"); exit(1); }calloc — allocate zero-initialized memory
int* arr = (int*)calloc(n, sizeof(int));
realloc — resize existing allocation
int* tmp = (int*)realloc(arr, new_n * sizeof(int));
if (!tmp) { free(arr); return NULL; }
arr = tmp;free — deallocate memory
free(arr); arr = NULL; // prevent dangling pointer
gets() is banned. Use fgets(buf, size, stdin).NULL after free().char* s = "hello"; s[0] = 'H'; crashes.sizeof on pointers: sizeof(arr) on a function parameter gives pointer size, not array size.| Function | Header | Purpose |
|---|---|---|
| malloc/free | <stdlib.h> | Dynamic memory allocation |
| memcpy/memmove | <string.h> | Copy memory blocks |
| memset | <string.h> | Fill memory with byte |
| memcmp | <string.h> | Compare memory blocks |
| strlen | <string.h> | String length (excludes \0) |
| strcmp/strncmp | <string.h> | String comparison |
| strdup | <string.h> | Duplicate string (POSIX) |
| printf/sprintf | <stdio.h> | Formatted output |
| scanf/sscanf | <stdio.h> | Formatted input |
?: — ternary conditional= += -= *= — assignment, — comma (sequence)| fopen/fclose |
| <stdio.h> |
| File I/O |
| qsort | <stdlib.h> | Quicksort |
| bsearch | <stdlib.h> | Binary search |
| assert | <assert.h> | Debug assertions |