C Cheatsheet

Variables Data Types Input/Output Strings Numbers Arrays Pointers Control Flow Functions Structs Unions Enums File I/O Memory Preprocessor Main Function

Variables

Variables
int x = 5;
double d = 3.14;
char c = 'A';
char s[] = "hello";
int arr[3] = {1, 2, 3};

Data Types

Types
int a = 1;        // Integer
double b = 2.3;   // Double
float c = 3.14f;  // Float
char d = 'A';     // Character
char s[] = "hi";  // String (char array)

Input / Output

I/O
#include 
printf("Hello, World!\n");
int x;
scanf("%d", &x);
printf("x = %d\n", x);

Strings

String
#include 
char s[10] = "hi";
strcpy(s, "hello");
strlen(s);
strcmp(s, "hi");
strcat(s, "!");

Numbers & Math

Math
#include 
double r = sqrt(16);
double p = pow(2, 3);
int a = abs(-5);

Arrays

Array
int arr[5] = {1, 2, 3, 4, 5};
int arr2[] = {10, 20, 30};
int arr3[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Pointers

Pointer
int x = 10;
int *p = &x;
int **pp = &p;
printf("Value of x: %d\n", x);
printf("Value of p: %p\n", p);
printf("Value of pp: %p\n", pp);

Control Flow

Flow
if (x > 0) {
    printf("Positive\n");
} else if (x < 0) {
    printf("Negative\n");
} else {
    printf("Zero\n");
}

for (int i = 0; i < 5; i++) {
    printf("%d ", i);
}

while (x < 10) {
    printf("%d ", x++);
}

do {
    printf("%d ", x--);
} while (x > 0);

Functions

Function
int add(int a, int b) {
    return a + b;
}

void printMessage(char *msg) {
    printf("%s\n", msg);
}

int main() {
    int result = add(5, 3);
    printMessage("Hello, World!");
    return 0;
}

Structs

Struct
struct Point {
    int x;
    int y;
};

struct Student {
    char name[50];
    int age;
    float gpa;
};

struct Point p1 = {1, 2};
struct Student s1 = {"John", 20, 3.5};

Unions

Union
union Data {
    int i;
    float f;
    char str[20];
};

union Data d1;
d1.i = 10;
printf("Integer: %d\n", d1.i);
d1.f = 3.14;
printf("Float: %f\n", d1.f);
strcpy(d1.str, "Hello");
printf("String: %s\n", d1.str);

Enums

Enum
enum Color {
    RED,
    GREEN,
    BLUE
};

enum Color c1 = RED;
printf("Color: %d\n", c1);

File I/O

File
#include 
FILE *fptr;
fptr = fopen("example.txt", "w");
fprintf(fptr, "Hello, World!\n");
fclose(fptr);

fptr = fopen("example.txt", "r");
char ch;
while ((ch = fgetc(fptr)) != EOF) {
    printf("%c", ch);
}
fclose(fptr);

Memory

Memory
int *p = (int *)malloc(sizeof(int));
*p = 10;
printf("Value: %d\n", *p);
free(p);

int *arr = (int *)calloc(5, sizeof(int));
for (int i = 0; i < 5; i++) {
    printf("%d ", arr[i]);
}
free(arr);

int *arr2 = (int *)realloc(arr, 10 * sizeof(int));
for (int i = 0; i < 10; i++) {
    printf("%d ", arr2[i]);
}
free(arr2);

Preprocessor

Preprocessor
#include 
#define PI 3.14159
#define SQUARE(x) ((x) * (x))

int main() {
    printf("PI: %f\n", PI);
    printf("Square of 5: %d\n", SQUARE(5));
    return 0;
}

Main Function

Main
#include 

int main() {
    printf("Hello, World!\n");
    return 0;
}

Bitwise Operators

Bitwise
int a = 5, b = 3;
int and = a & b;   // 1
int or = a | b;    // 7
int xor = a ^ b;   // 6
int not = ~a;      // -6
int shl = a << 1;  // 10
int shr = a >> 1;  // 2

Command Line Args

Args
int main(int argc, char *argv[]) {
    for (int i = 0; i < argc; i++) {
        printf("Arg %d: %s\n", i, argv[i]);
    }
    return 0;
}

Function Pointers

Pointer
int add(int a, int b) { return a + b; }
int (*fptr)(int, int) = add;
printf("%d\n", fptr(2, 3)); // 5

Typedefs

Type
typedef unsigned long ulong;
ulong a = 10;
typedef struct { int x, y; } Point;
Point p = {1, 2};

Macros with Args

Macro
#define SQUARE(x) ((x)*(x))
printf("%d\n", SQUARE(5)); // 25

Inline Functions

C99+
inline int max(int a, int b) { return a > b ? a : b; }
printf("%d\n", max(2, 3)); // 3

Variable Length Arrays

C99+
int n = 5;
int arr[n];
for (int i = 0; i < n; i++) arr[i] = i;

Static & Extern

Storage
static int x = 5; // File or function scope
extern int y;     // Defined elsewhere

Volatile & Const

Qualifier
volatile int v;
const int c = 10;

Assert & Error

Error
#include 
assert(x > 0);
#include 
if (fopen("nofile.txt", "r") == NULL) {
    perror("Error");
    printf("errno: %d\n", errno);
}

Dynamic Strings

String
char buf[100];
sprintf(buf, "Value: %d", 42);
snprintf(buf, sizeof(buf), "%.2f", 3.14159);

Linked List Node

Data Struct
struct Node {
    int data;
    struct Node *next;
};
struct Node n1 = {1, NULL};
struct Node n2 = {2, &n1};

Memory Alignment

C99+
#include 
struct S { char c; int i; };
size_t off = offsetof(struct S, i);

C99/C11 Features

C99/C11
_Bool b = 1;
static_assert(sizeof(int) == 4, "int is not 4 bytes");
restrict int *p;
// C11: _Alignof, _Atomic, _Thread_local

Common Pitfalls

Warning
// Dangling pointer
int *p = malloc(sizeof(int));
free(p);
// *p = 10; // undefined behavior

// Buffer overflow
char buf[4];
strcpy(buf, "toolong"); // overflow

// Uninitialized variable
int x;
printf("%d\n", x); // undefined