C Notes
Fast-paced C programming revision notes covering all topics from basics to advanced in a concise format. Perfect for last-minute exam preparation and quick review.
These concise notes cover the entire C programming syllabus in a rapid-fire format. Perfect for last-minute revision before exams or interviews. Each topic is condensed to its essentials.
1. C Basics
- C is a procedural, middle-level language created by Dennis Ritchie (1972)
- Compiled language: Source → Preprocessor → Compiler → Assembler → Linker → Executable
- Entry point:
int main()– execution always starts here - Case-sensitive:
int≠Int - Statements end with semicolons
; - Comments:
//single lineand/* multi-line */
2. Data Types
| Type | Size | Range | Format |
|---|---|---|---|
| char | 1B | -128 to 127 | %c |
| int | 4B | ±2.1 billion | %d |
| float | 4B | ±3.4×10³⁸ | %f |
| double | 8B | ±1.7×10³⁰⁸ | %lf |
| short | 2B | -32768 to 32767 | %hd |
| long | 8B | ±9.2×10¹⁸ | %ld |
| unsigned int | 4B | 0 to 4.2 billion | %u |
3. Variables and Constants
int x = 10; // Variable
const int PI = 314; // Constant (runtime)
#define MAX 100 // Macro constant (compile-time)
enum { RED, GREEN, BLUE }; // Enumeration (0, 1, 2)Storage Classes: auto (default), static (persists), extern (external), register (hint for CPU register)
4. Operators (by precedence)
() [] -> .– Postfix! ~ ++ -- + - * & sizeof (type)– Unary* / %– Multiplicative+ -– Additive<< >>– Shift< <= > >=– Relational== !=– Equality& ^ |– Bitwise&& ||– Logical? :– Ternary= += -= *=– Assignment,– Comma
5. Control Statements
// Conditional
if (cond) { } else if (cond2) { } else { }
switch (expr) { case val: ...; break; default: ...; }
// Loops
for (init; condition; update) { }
while (condition) { }
do { } while (condition); // Executes at least once
// Jump
break; // Exit loop
continue; // Skip iteration
return val; // Exit function
goto label; // Jump (avoid!)6. Functions
return_type function_name(parameters) { body; return value; }
// Call by value: copies data (original unchanged)
void modify(int x) { x = 100; } // Doesn't affect caller
// Call by reference: passes address (original changed)
void modify(int *x) { *x = 100; } // Changes caller's variable
// Recursion: function calls itself
int fact(int n) { return (n <= 1) ? 1 : n * fact(n-1); }7. Arrays
- Indexing starts at 0
- No bounds checking (programmer's responsibility)
- Contiguous memory allocation
8. Strings
char str[] = "Hello"; // Modifiable, size 6 (includes \0)
char *ptr = "Hello"; // Read-only literal
// Essential functions (string.h)
strlen(s) // Length
strcpy(dst, src) // Copy
strcat(dst, src) // Concatenate
strcmp(s1, s2) // Compare (0 if equal)
strstr(s, sub) // Find substring- Strings are NULL-terminated character arrays (
'\0') - Use
fgets()for input (nevergets())
9. Pointers
Key Rules:
NULLpointer: points nowhere, check before use- Dangling pointer: points to freed/invalid memory
- Wild pointer: uninitialized (contains garbage address)
10. Structures and Unions
typedef: typedef struct { ... } Name; – creates alias
11. Dynamic Memory Allocation
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; // Release
// ALWAYS check: if (p == NULL) { /* handle error */ }Memory Layout: Text → Data → BSS → Heap (↑) → Stack (↓)
12. File Handling
FILE *fp = fopen("file.txt", "r"); // Open
if (!fp) { perror("Error"); exit(1); }
// Text I/O
fprintf(fp, "text %d", val); // Write formatted
fscanf(fp, "%d", &val); // Read formatted
fgets(buf, size, fp); // Read line
// Binary I/O
fwrite(&data, sizeof(data), 1, fp); // Write struct
fread(&data, sizeof(data), 1, fp); // Read struct
fseek(fp, 0, SEEK_SET); // Reposition
fclose(fp); // Close (essential!)Modes: "r" read, "w" write, "a" append, "rb"/"wb" binary
13. Preprocessor
#include <stdio.h> // System header
#include "myfile.h" // Local header
#define PI 3.14159 // Constant
#define MAX(a,b) ((a)>(b)?(a):(b)) // Macro
#ifdef DEBUG // Conditional compilation
#endif
#pragma once // Include guard14. Bitwise Operations
a & b // AND
a | b // OR
a ^ b // XOR
~a // NOT (complement)
a << n // Left shift (multiply by 2^n)
a >> n // Right shift (divide by 2^n)
// Common tricks
n & 1 // Check odd/even (1=odd, 0=even)
n & (n-1) == 0 // Check power of 2
x ^ x == 0 // XOR with itself = 0
a ^= b; b ^= a; a ^= b; // Swap without temp15. Key Differences to Remember
| A | B | Key Difference |
|---|---|---|
malloc | calloc | calloc initializes to zero |
++i | i++ | pre returns new, post returns old |
= | == | assignment vs comparison |
struct | union | separate vs shared memory |
while | do-while | may skip vs always executes once |
break | continue | exit loop vs skip iteration |
& | && | bitwise vs logical |
char[] | char* | modifiable vs read-only literal |
| stack | heap | automatic vs manual management |
#define | const | textual vs typed constant |
16. Common Mistakes Checklist
- ❌ Using
=instead of==in conditions - ❌ Forgetting
breakin switch cases (fall-through) - ❌ Not null-terminating strings
- ❌ Array index out of bounds
- ❌ Forgetting to
free()malloc'd memory - ❌ Dereferencing NULL pointer
- ❌ Using
sizeof(pointer)thinking it's array size - ❌ Returning address of local variable
- ❌ Format specifier mismatch in printf/scanf
- ❌ Missing
&in scanf for non-array arguments
17. Complexity Quick Reference
| Algorithm | Best | Average | Worst |
|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) |
| Selection Sort | O(n²) | O(n²) | O(n²) |
| Insertion Sort | O(n) | O(n²) | O(n²) |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) |
| Quick Sort | O(n log n) | O(n log n) | O(n²) |
| Binary Search | O(1) | O(log n) | O(log n) |
| Linear Search | O(1) | O(n) | O(n) |
Summary
C programming revolves around: data types and variables, operators and expressions, control flow, functions, arrays and strings, pointers and memory management, structures, file handling, and the preprocessor. Master these fundamentals and you can build anything from operating systems to embedded firmware. Always remember: C trusts the programmer – validate inputs, check allocations, and manage memory carefully.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Quick Revision Notes for C Programming – Complete C in 10 Minutes.
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, resources, quick, revision, notes, quick revision notes for c programming – complete c in 10 minutes
Related C Programming Topics