C Notes
Comprehensive C programming cheatsheet covering syntax, data types, operators, control flow, pointers, strings, file I/O, memory management, and common patterns.
This cheatsheet provides a compact reference for all essential C programming concepts. Keep it handy during coding, revision, or interview preparation.
Data Types and Sizes
// Integer types
char c = 'A'; // 1 byte (-128 to 127)
short s = 32767; // 2 bytes (-32768 to 32767)
int i = 2147483647; // 4 bytes
long l = 2147483647L; // 4 or 8 bytes
long long ll = 9223372036854775807LL; // 8 bytes
// Unsigned variants (no negative values, double positive range)
unsigned char uc = 255;
unsigned int ui = 4294967295U;
unsigned long long ull = 18446744073709551615ULL;
// Floating point
float f = 3.14f; // 4 bytes, ~7 digits precision
double d = 3.14159265; // 8 bytes, ~15 digits precision
// Boolean (C99)
#include <stdbool.h>
bool flag = true;| Type | Size | Range |
|---|---|---|
| char | 1B | -128 to 127 |
| unsigned char | 1B | 0 to 255 |
| short | 2B | -32,768 to 32,767 |
| int | 4B | -2.1B to 2.1B |
| long long | 8B | -9.2×10¹⁸ to 9.2×10¹⁸ |
| float | 4B | ±3.4×10³⁸ |
| double | 8B | ±1.7×10³⁰⁸ |
Format Specifiers
printf("%d", intVar); // int
printf("%ld", longVar); // long
printf("%lld", llVar); // long long
printf("%u", unsignedVar); // unsigned int
printf("%f", floatVar); // float/double
printf("%.2f", doubleVar); // 2 decimal places
printf("%e", doubleVar); // scientific notation
printf("%c", charVar); // character
printf("%s", stringVar); // string
printf("%p", (void*)ptr); // pointer address
printf("%x", intVar); // hexadecimal
printf("%o", intVar); // octal
printf("%%"); // literal %
printf("%05d", num); // zero-padded, width 5
printf("%-10s", str); // left-aligned, width 10
printf("%zu", sizeof(x)); // size_tOperators (by Precedence, High to Low)
| Precedence | Operators | Associativity | ||
|---|---|---|---|---|
| 1 | () [] -> . | Left to Right | ||
| 2 | ! ~ ++ -- + - * & (type) sizeof | Right to Left | ||
| 3 | * / % | Left to Right | ||
| 4 | + - | Left to Right | ||
| 5 | << >> | Left to Right | ||
| 6 | < <= > >= | Left to Right | ||
| 7 | == != | Left to Right | ||
| 8 | & | Left to Right | ||
| 9 | ^ | Left to Right | ||
| 10 | ` | ` | Left to Right | |
| 11 | && | Left to Right | ||
| 12 | ` | ` | Left to Right | |
| 13 | ?: | Right to Left | ||
| 14 | = += -= etc. | Right to Left | ||
| 15 | , | Left to Right |
Control Flow
Pointers Quick Reference
Strings
#include <string.h>
char str[] = "Hello"; // Modifiable, 6 bytes
char *ptr = "Hello"; // Read-only literal
strlen(str); // Length (5, excludes '\0')
strcpy(dest, src); // Copy (unsafe)
strncpy(dest, src, n); // Copy with limit
strcat(dest, src); // Concatenate
strcmp(s1, s2); // Compare (0 if equal)
strstr(haystack, needle); // Find substring
strchr(str, 'c'); // Find character
strtok(str, delimiters); // Tokenize
sprintf(buf, "%d", num); // Format to string
snprintf(buf, size, fmt, ...) // Safe formatArrays
Structures and Unions
// Structure
struct Point {
int x;
int y;
};
struct Point p1 = {10, 20};
p1.x = 30;
// Pointer to struct
struct Point *pp = &p1;
pp->x = 40; // arrow operator
// typedef
typedef struct { int x, y; } Point;
Point p = {1, 2};
// Union (shared memory)
union Data { int i; float f; char c; };
union Data d;
d.i = 65; // d.c is now 'A'Dynamic Memory
#include <stdlib.h>
int *p = (int*)malloc(n * sizeof(int)); // Uninitialized
int *p = (int*)calloc(n, sizeof(int)); // Zero-initialized
p = (int*)realloc(p, new_size); // Resize
free(p); p = NULL; // Free and nullify
// Always check!
if (p == NULL) { /* handle error */ }File I/O
Preprocessor
#include <stdio.h> // System header
#include "myheader.h" // Local header
#define PI 3.14159 // Constant macro
#define MAX(a,b) ((a)>(b)?(a):(b)) // Function macro
#define STRINGIFY(x) #x // Stringification
#define CONCAT(a,b) a##b // Token pasting
#ifdef DEBUG
// Compiled only if DEBUG is defined
#endif
#ifndef HEADER_H
#define HEADER_H
// Include guard
#endif
#pragma once // Modern include guardCommon Patterns
Common Library Functions
// stdlib.h
abs(x) // Absolute value
atoi(str) // String to int
atof(str) // String to double
strtol(str,&end,base) // Safe string to long
rand() % n // Random 0 to n-1
srand(seed) // Seed random
qsort(arr,n,size,cmp) // Sort array
bsearch(key,arr,n,size,cmp) // Binary search
exit(status) // Terminate program
system(cmd) // Execute shell command
// math.h (link with -lm)
sqrt(x) pow(x,y) fabs(x)
ceil(x) floor(x) round(x)
sin(x) cos(x) tan(x)
log(x) log10(x) log2(x)
// ctype.h
isalpha(c) isdigit(c) isspace(c)
isalnum(c) isupper(c) islower(c)
toupper(c) tolower(c)Compilation Commands
gcc file.c -o output // Basic compile
gcc -Wall -Wextra -g file.c // With warnings + debug
gcc -O2 file.c // Optimized
gcc -std=c11 file.c // C11 standard
gcc -lm file.c // Link math library
gcc -fsanitize=address file.c // With ASanSummary
This cheatsheet covers the most essential C programming syntax and patterns. Keep it as a quick reference while coding or before interviews. Remember: C trusts the programmer – always validate inputs, check allocations, and free memory. Compile with -Wall -Wextra to catch common mistakes at compile time.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for C Programming Cheatsheet – Complete Quick Reference Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, interview, preparation, cheatsheet, c programming cheatsheet – complete quick reference guide
Related C Programming Topics