C Cheatsheet (Very Easy)

Each topic is shown in 3 parts: Core concept, Simple code, and Output.

1. Declaration vs Definition

Core concept: Declaration tells type/name exists. Definition creates storage or function body.

Remember: Declare many times, define once.

extern int x;   // declaration
int x = 10;     // definition

Output: No output. This is compile/link behavior.

2. Basic Types and Format Specifiers

Core concept: Use correct format specifier in printf/scanf for each type.

Common: %d int, %f float, %c char, %s string.

int age = 20;
float pi = 3.14f;
printf("%d %.2f\n", age, pi);

Output: 20 3.14

3. Pointers

Core concept: Pointer stores memory address of another variable.

Symbols: & gives address, * accesses value at address.

int a = 5;
int *p = &a;
printf("%d\n", *p);

Output: 5

4. Arrays and Strings

Core concept: Arrays store same-type values in contiguous memory. String is char array ending with \0.

int arr[3] = {10, 20, 30};
char name[] = "C";
printf("%d %s\n", arr[1], name);

Output: 20 C

5. Struct, Union, Enum

Core concept: struct groups fields, union shares memory, enum gives named constants.

struct Student { int id; char grade; };
enum Day { MON, TUE, WED };
struct Student s = {1, 'A'};
printf("%d %c %d\n", s.id, s.grade, WED);

Output: 1 A 2

6. Storage Classes

Core concept: Storage class controls lifetime, scope, and linkage.

  • auto: default local variable
  • static: keeps value between function calls
  • extern: variable defined in another file
  • register: compiler hint for CPU register
void counter() {
  static int c = 0;
  c++;
  printf("%d\n", c);
}

Output: Calling twice prints 1 then 2.

7. Dynamic Memory

Core concept: Heap memory is allocated at runtime with malloc/calloc/realloc and released using free.

int *a = (int*)malloc(3 * sizeof(int));
a[0] = 7; a[1] = 8; a[2] = 9;
printf("%d %d %d\n", a[0], a[1], a[2]);
free(a);

Output: 7 8 9

8. File Handling

Core concept: Use fopen, fprintf/fscanf, and fclose for files.

FILE *fp = fopen("note.txt", "w");
fprintf(fp, "Hello C");
fclose(fp);

Output: Creates file note.txt with text Hello C.

9. Preprocessor and Macros

Core concept: Preprocessor runs before compilation. Macros are text replacements.

#define SQUARE(x) ((x) * (x))
printf("%d\n", SQUARE(4));

Output: 16

Tip: Always use parentheses in macros.

10. Function Pointers and qsort

Core concept: Function pointer stores function address. Useful in callbacks like qsort.

int cmp(const void *a, const void *b) {
  return (*(int*)a - *(int*)b);
}
int arr[] = {3,1,2};
qsort(arr, 3, sizeof(int), cmp);

Output: Array becomes sorted: 1 2 3.

11. Bitwise Operators

Core concept: Bitwise operations work directly on binary bits.

int a = 6;   // 110
int b = 3;   // 011
printf("%d\n", a & b);  // 2

Output: 2

  • & AND
  • | OR
  • ^ XOR
  • << left shift
  • >> right shift

12. Complexity and Safety Checklist

Core concept: Good C code is not only correct, but also safe and efficient.

Topic Quick Rule
Array accessAlways check bounds
Heap memoryEvery malloc needs one free
InputPrefer fgets over gets
PointersInitialize to NULL when possible
Time complexityState O(n), O(log n), O(n log n) clearly