C Cheat Sheet
You've finished the C tutorial. This last page is your one-page revision sheet: every important syntax rule, specifier, operator and library function in compact tables. Keep it open while practising, and read it once before every exam or interview.
Each section links back to the full lesson if you need the details.
What You Will Learn
- Data types, sizes and format specifiers at a glance.
- Operator precedence in one table.
- Syntax of every control statement and loop.
- Functions, arrays, strings and pointers in short form.
- Structures, dynamic memory, files and preprocessor essentials.
- A one-screen program that uses most of C.
C Cheat Sheet Explained
How to use this sheet: read a table, then close your eyes and recall it. If you can't, open the linked lesson. Revision works best when you test yourself, not when you just re-read.
Memory trick: C in one line: "Types hold data, operators change it, control decides the path, functions organise it, pointers point to it, structures group it, files keep it."
Program Skeleton and Data Types
#include <stdio.h> /* headers */
#define MAX 100 /* macros */
int square(int x); /* prototypes */
int main(void) /* execution starts here */
{
int n = 5; /* declarations + statements */
printf("%d\n", square(n));
return 0; /* 0 = success */
}
int square(int x) { return x * x; }
| Type | Typical size | printf / scanf | Example |
|---|---|---|---|
| char | 1 byte | %c | char g = 'A'; |
| int | 4 bytes | %d | int n = 42; |
| unsigned int | 4 bytes | %u | unsigned u = 7u; |
| long / long long | 4–8 / 8 bytes | %ld / %lld | long long big = 9000000000LL; |
| float | 4 bytes | %f / %f | float f = 3.5f; |
| double | 8 bytes | %f / %lf | double d = 3.14159; |
| string (char array) | length + 1 | %s | char s[20] = "Hi"; |
| pointer | 8 bytes (64-bit) | %p | int *p = &n; |
| size_t (sizeof result) | 8 bytes (64-bit) | %zu | sizeof n |
Escape sequences: \n newline, \t tab, \\ backslash, \" quote, \0 null. Details: Data Types in C.
Operators (Highest to Lowest Precedence)
| Level | Operators | Associativity |
|---|---|---|
| 1 | () [] -> . postfix ++ -- | Left to right |
| 2 | prefix ++ --, unary + - ! ~ * &, (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–10 | & then ^ then | | Left to right |
| 11–12 | && then || | Left to right |
| 13 | ?: | Right to left |
| 14 | = += -= *= /= %= etc. | Right to left |
| 15 | , | Left to right |
Remember: int / int is int division; % works only on integers; && and || short-circuit. Details: Operator Precedence.
Control Statements and Loops
if (cond) { ... } else if (cond2) { ... } else { ... }
switch (n) { case 1: ...; break; case 2: ...; break; default: ...; }
for (i = 0; i < n; i++) { ... } /* known number of times */
while (cond) { ... } /* entry-controlled */
do { ... } while (cond); /* runs at least once */
break; /* leave loop/switch */ continue; /* next iteration */
result = (a > b) ? a : b; /* conditional operator */
Functions, Arrays and Strings
int add(int a, int b); /* prototype */
int add(int a, int b) { return a + b; }/* definition */
void swap(int *a, int *b); /* "call by reference" via pointers */
long fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); } /* recursion */
int a[5] = {1, 2, 3, 4, 5}; /* indexes 0..4 */
int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; /* 2 rows, 3 columns */
int len = sizeof a / sizeof a[0]; /* element count (not for parameters) */
char s[20] = "Hello"; /* ends with '\0' */
| string.h | Does | ctype.h | Does |
|---|---|---|---|
strlen(s) | Length (no '\0') | isdigit(c) | 0–9? |
strcpy(d, s) | Copy s into d | isalpha(c) | Letter? |
strcat(d, s) | Append s to d | isspace(c) | Space, tab, newline? |
strcmp(a, b) | 0 if equal, <0, >0 | toupper(c) | To upper case |
strstr(s, w) | Find w in s | tolower(c) | To lower case |
strncpy(d, s, n) | Copy at most n chars | isupper(c) | Upper case? |
Pointers and Structures
int x = 10, *p = &x; /* & address of, * value at */
*p = 20; /* changes x */
p + 1; /* next int (moves sizeof(int) bytes)*/
int **pp = &p; /* pointer to pointer */
int (*fp)(int, int) = add; /* function pointer */
const int *pc; /* value read-only */
int *const cp = &x; /* pointer fixed */
struct student { int roll; char name[20]; float marks; };
struct student s = {1, "Asha", 88.5f};
struct student *ps = &s;
s.marks; ps->marks; /* . for variables, -> for pointers */
typedef struct student Student;
union data { int i; float f; }; /* members share memory */
enum color { RED, GREEN, BLUE }; /* 0, 1, 2 */
Dynamic Memory, Files and Preprocessor
int *a = malloc(n * sizeof *a); /* garbage values, check NULL */
int *b = calloc(n, sizeof *b); /* zero-filled */
a = realloc(a, 2 * n * sizeof *a); /* resize (use a temp in real code) */
free(a); a = NULL; /* release, avoid dangling */
FILE *fp = fopen("data.txt", "r"); /* "r" "w" "a" "r+" "w+" "a+", add "b" */
if (fp == NULL) { perror("data.txt"); return 1; }
fprintf(fp, ...); fscanf(fp, ...); fgets(buf, sizeof buf, fp); fputs(s, fp);
fread(&rec, sizeof rec, 1, fp); fwrite(&rec, sizeof rec, 1, fp);
fseek(fp, 0, SEEK_END); ftell(fp); rewind(fp); fclose(fp);
#define PI 3.14159
#define SQUARE(x) ((x) * (x))
#ifndef MY_H
#define MY_H
#endif
__FILE__ __LINE__ __DATE__ __TIME__ __func__
Example 1: One Program, Most of C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME 20
typedef struct
{
char name[MAX_NAME];
int marks;
} Student;
int compare(const void *a, const void *b)
{
return ((const Student *) b)->marks - ((const Student *) a)->marks;
}
double average(const Student s[], int n)
{
int i, total = 0;
for (i = 0; i < n; i++)
total += s[i].marks;
return (double) total / n;
}
int main(void)
{
const char *names[] = {"Asha", "Ravi", "Meena", "Kabir"};
int marks[] = {78, 92, 65, 88};
int n = 4, i;
Student *list = malloc(n * sizeof *list); /* dynamic array */
FILE *fp;
if (list == NULL)
return 1;
for (i = 0; i < n; i++)
{
strcpy(list[i].name, names[i]);
list[i].marks = marks[i];
}
qsort(list, n, sizeof *list, compare); /* function pointer */
printf("Rank list:\n");
for (i = 0; i < n; i++)
printf("%d. %-6s %3d %s\n", i + 1, list[i].name, list[i].marks,
list[i].marks >= 75 ? "(distinction)" : "");
printf("Average: %.2f\n", average(list, n));
fp = fopen("rank.txt", "w"); /* save to a file */
if (fp != NULL)
{
for (i = 0; i < n; i++)
fprintf(fp, "%s %d\n", list[i].name, list[i].marks);
fclose(fp);
printf("Saved %d records to rank.txt\n", n);
remove("rank.txt");
}
free(list);
return 0;
}
Output
Rank list: 1. Ravi 92 (distinction) 2. Kabir 88 (distinction) 3. Asha 78 (distinction) 4. Meena 65 Average: 80.75 Saved 4 records to rank.txt
Code Explanation
- Preprocessor and types:
#define,typedefand a structure describe the data. - Dynamic memory:
malloccreates an array of 4 students; it is checked for NULL and freed at the end. - Strings:
strcpyfills each name. - Function pointers:
qsortcallscompareto sort by marks, highest first. - Arrays, loops and the conditional operator print the rank list; a function computes the average with a cast to avoid integer division.
- Files: the list is written with
fprintf, then the demo file is removed.
How to Revise C in One Week
| Day | Revise | Practise |
|---|---|---|
| 1 | Basics, data types, operators | Output questions |
| 2 | Control statements and loops | Patterns, prime, palindrome |
| 3 | Functions, recursion, arrays | Factorial, Fibonacci, sorting, matrices |
| 4 | Strings and pointers | String programs, pointer questions |
| 5 | Structures, unions, dynamic memory | Student records with malloc |
| 6 | Files, preprocessor, command line | File copy, record system |
| 7 | This cheat sheet + MCQs | Timed mock test |
Example 2: Common String and Memory Functions
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
char a[30] = "Study", b[] = "Fame", copy[30];
char *dyn;
int i;
strcat(a, b); /* a = "StudyFame" */
strcpy(copy, a);
for (i = 0; copy[i] != '\0'; i++)
copy[i] = (char) toupper((unsigned char) copy[i]);
printf("strcat : %s (length %zu)\n", a, strlen(a));
printf("toupper: %s\n", copy);
printf("strcmp : %s\n", strcmp(a, "StudyFame") == 0 ? "equal" : "different");
printf("strstr : %s\n", strstr(a, "Fame"));
dyn = malloc(strlen(a) + 1); /* +1 for '\0' */
if (dyn == NULL)
return 1;
strcpy(dyn, a);
printf("malloc copy: %s\n", dyn);
free(dyn);
printf("atoi(\"123\") + 1 = %d\n", atoi("123") + 1);
return 0;
}
Output
strcat : StudyFame (length 9)
toupper: STUDYFAME
strcmp : equal
strstr : Fame
malloc copy: StudyFame
atoi("123") + 1 = 124
- When copying a string into new memory, allocate strlen + 1 bytes for the
'\0'. strstrreturns a pointer to where the word starts, so printing it shows the rest of the string from there.
Important Points to Remember
- Every program: headers → main → return 0.
- Strings end with '\0'; allocate length + 1.
- Arrays index from 0; pass the length to functions.
- Check scanf, malloc and fopen return values; free and fclose what you open.
- . for structure variables, -> for pointers.
- Avoid undefined behavior: no out-of-bounds, no uninitialised variables, no double modification in one expression.
Common Mistakes Beginners Make
| Mistake | Fix |
|---|---|
if (x = 5) | if (x == 5) |
scanf("%d", x) | scanf("%d", &x) |
char s[5] = "Hello"; | Make room for '\0': char s[6] |
Missing break in switch | End each case with break |
Comparing strings with == | Use strcmp(a, b) == 0 |
Cheat Sheet Practice Questions
- Which format specifiers print a long long and a size_t?
- What is the precedence order of
&&,||and==? - What is the difference between
"w"and"a"mode? - How many bytes must you allocate to copy the string "Hello"?
- When do you use
->instead of.?
Show answers
%lldand%zu.==first, then&&, then||.- "w" creates or erases the file before writing; "a" adds to the end and keeps the old content.
- 6 (5 letters + '\0').
- When you have a pointer to a structure.
Coding Exercise
Final challenge: read words from a fixed list, store only those longer than 3 letters in a dynamically allocated array of strings, sort them alphabetically and print them. Free everything at the end.
Show solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int by_alpha(const void *a, const void *b)
{
return strcmp(*(char *const *) a, *(char *const *) b);
}
int main(void)
{
const char *words[] = {"pointer", "int", "array", "if", "struct", "loop", "for"};
int total = 7, kept = 0, i;
char **list = malloc(total * sizeof *list);
if (list == NULL)
return 1;
for (i = 0; i < total; i++)
if (strlen(words[i]) > 3)
{
list[kept] = malloc(strlen(words[i]) + 1);
if (list[kept] == NULL)
return 1;
strcpy(list[kept], words[i]);
kept++;
}
qsort(list, kept, sizeof *list, by_alpha);
printf("%d words longer than 3 letters:\n", kept);
for (i = 0; i < kept; i++)
{
printf(" %s\n", list[i]);
free(list[i]);
}
free(list);
return 0;
}
4 words longer than 3 letters: array loop pointer struct
This one exercise uses arrays of pointers, malloc, strings, qsort with a function pointer and free: congratulations if you solved it alone!
Quick Quiz: C Cheat Sheet (MCQ)
-
Format specifier for
unsigned int:A) %d B) %u C) %ud D) %lu
-
Which operator has higher precedence?
A)
&&B)||C)==D)= -
Which function resizes dynamic memory?
A) malloc B) calloc C) realloc D) resize
-
enum color { RED, GREEN, BLUE };gives BLUE the value:A) 1 B) 2 C) 3 D) 0
-
Which mode reads a binary file?
A) "r" B) "rb" C) "br" only D) "b"
Show answers
- B: %u.
- C: ==.
- C: realloc.
- B: 2.
- B: "rb".
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Specifiers | %d %u %ld %f %lf %c %s %p %zu |
|---|---|
| Precedence | () unary * / + − relational == && || ?: = |
| Loops | for, while (entry), do-while (exit) |
| Strings | End with '\0'; compare with strcmp |
| Pointers | & address, * value |
| Memory | malloc/calloc/realloc → free |
| Files | fopen → read/write → fclose |
Frequently asked questions
- What should I revise the night before a C exam?
- This cheat sheet, the important programs and the differences (while/do-while, struct/union, malloc/calloc, call by value/reference).
- Which format specifier prints sizeof?
- %zu, because sizeof returns a size_t.
- What should I learn after C?
- Data structures and algorithms in depth, then C++ or Java (object-oriented programming) or Python.
Remember
- Types hold, operators change, control decides, functions organise, pointers point, structures group, files keep.
- Test yourself; don't just re-read.
What Next?
Congratulations, you have completed the C tutorial! Keep practising with the C programs with output, test yourself in the MCQ section, and go back to the C tutorial home to revise any module.