Structures, Files & More
C Cheat Sheet & Exam Prep
Everything from CSE 101 on one page: syntax, format specifiers, operators, must-know programs and the mistakes that cost marks.
Use this page for last-minute revision. Every row links back to the full lesson.
Program skeleton
C
#include <stdio.h>
int main(void) {
// declarations
// input
// processing
// output
return 0;
}Data types and format specifiers
| Type | Size* | printf |
scanf |
|---|---|---|---|
char |
1 | %c |
%c (use " %c") |
int |
4 | %d |
%d |
unsigned int |
4 | %u |
%u |
long long |
8 | %lld |
%lld |
float |
4 | %f |
%f |
double |
8 | %f / %lf |
%lf |
| string | — | %s |
%s (no &) |
| pointer | 8 | %p |
— |
size_t |
8 | %zu |
%zu |
* typical 64-bit sizes; check with sizeof. See Variables & Data Types.
Control flow at a glance
C
if (cond) { } else if (cond2) { } else { }
switch (n) { case 1: ...; break; default: ...; }
for (int i = 0; i < n; i++) { }
while (cond) { }
do { } while (cond); // runs at least once
break; // leave loop/switch
continue; // skip to next iterationPointers in one table
| Expression | Meaning |
|---|---|
int *p = &x; |
p stores the address of x |
*p |
the value at that address |
p + 1 |
address of the next int |
a[i] ≡ *(a + i) |
array indexing is pointer arithmetic |
p->m ≡ (*p).m |
member through a struct pointer |
int **pp = &p; |
pointer to pointer |
void f(int *x) |
call by reference |
Must-practise programs
| Topic | Programs |
|---|---|
| Basics | swap two numbers (with and without a temp), simple interest, area, temperature conversion |
| Decisions | largest of three, leap year, grade calculator, vowel or consonant, calculator with switch |
| Loops | factorial, Fibonacci, prime check, primes in a range, Armstrong number, palindrome number, sum of digits, GCD/LCM |
| Patterns | right triangle, pyramid, diamond, Floyd's triangle, Pascal's triangle |
| Arrays | max/min, second largest, reverse, linear search, frequency count, remove duplicates |
| 2D arrays | matrix add, multiply, transpose, diagonal sums |
| Strings | length, reverse, palindrome, count vowels, without library functions |
| Recursion | factorial, Fibonacci, power, sum of digits, Tower of Hanoi |
| Pointers | swap with pointers, array via pointer, dynamic array with malloc |
| Structs & files | student records, topper, save and read records from a file |
Armstrong number (asked very often)
A number is an Armstrong number if the sum of its digits, each raised to the number of digits, equals the number: 153 = 1³ + 5³ + 3³.
C
#include <stdio.h>
int isArmstrong(int n) {
int digits = 0, sum = 0;
for (int t = n; t > 0; t /= 10) digits++;
for (int t = n; t > 0; t /= 10) {
int d = t % 10, p = 1;
for (int i = 0; i < digits; i++) p *= d;
sum += p;
}
return sum == n;
}
int main(void) {
printf("Armstrong numbers below 10000: ");
for (int i = 1; i < 10000; i++)
if (isArmstrong(i)) printf("%d ", i);
printf("\n");
return 0;
}Output
Armstrong numbers below 10000: 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474
Mistakes that cost marks
| Mistake | Fix |
|---|---|
if (x = 5) |
if (x == 5) |
scanf("%d", x) |
scanf("%d", &x) |
scanf("%f", &d) for a double |
%lf |
for (i = 0; i <= n; i++) on an array of n |
i < n |
5 / 2 expecting 2.5 |
5.0 / 2 or a cast |
str1 == str2 |
strcmp(str1, str2) == 0 |
missing break in switch |
add break; after each case |
char s[5] = "Hello"; |
needs 6 bytes for '\0' |
using gets |
fgets(buf, size, stdin) |
malloc without free |
free every block exactly once |
Theory questions to prepare
- Features of C and the structure of a C program.
- Compilation stages: preprocessor, compiler, assembler, linker.
- Difference between
whileanddo-while; betweenbreakandcontinue. - Call by value vs call by reference with an example.
- Recursion vs iteration.
- Storage classes with scope, lifetime and default values.
- Structure vs union.
mallocvscalloc; what a memory leak is.- File opening modes.
- Macro vs function.
Final check: mixed revision
4 questions · pick an answer to check it instantly
- 1
Which loop always executes at least once?
The condition of ado-whileis checked after the body. - 2
What does
printf("%d", 17 % 5 * 2);print?%and*share a precedence level and run left to right: 17 % 5 = 2, then 2 × 2 = 4. - 3
Which statement correctly allocates an array of
ndoubles?malloctakes a byte count, so multiply the element count by the element size. - 4
Which is an Armstrong number?
3³ + 7³ + 0³ = 27 + 343 + 0 = 370.