C Programming Cheat Sheet for Quick Revision
1. Basic Syntax
Header: #include <stdio.h>
Main Function: int main() { return 0; }
Comments: // Single-line or /* Multi-line */
2. Data Types & Variables
Basic: int, float, char, double
Example: int a = 10; float b = 5.5; char c = 'A';
3. Operators
Arithmetic: +, -, *, /, %
Relational: ==, !=, >, <, >=, <=
Logical: &&, ||, !
Bitwise: &, |, ^, ~, <<, >>
4. Control Structures
If-else:
if (a > b) { printf("A is greater"); } else { printf("B is greater");
}
Switch:
switch(choice) {
case 1: printf("One"); break;
default: printf("Other");
}
Loops:
for (int i = 0; i < 10; i++) { printf("%d", i); }
while (x > 0) { x--; }
do { y++; } while (y < 5);
5. Functions
Declaration: int add(int a, int b);
Definition:
int add(int a, int b) { return a + b; }
Call: int sum = add(5, 3);
6. Arrays & Strings
Array: int arr[5] = {1, 2, 3, 4, 5};
String: char str[] = "Hello";
7. Pointers
Declaration: int *p;
Assignment: int x = 10; p = &x;
Dereferencing: printf("%d", *p);
8. Structures
struct Student {
char name[20];
int age;
};
struct Student s1 = {"John", 20};
9. File Handling
FILE *fptr = fopen("file.txt", "r");
if (fptr != NULL) { fclose(fptr); }
10. Memory Allocation
malloc(), calloc(), free()
int *ptr = (int*)malloc(5 * sizeof(int));
free(ptr);
11. Important Functions
Input/Output: scanf(), printf()
String: strcpy(), strlen(), strcmp()
Math: sqrt(), pow() (Add #include <math.h>)
12. Common Errors to Avoid
Uninitialized variables
Array out-of-bounds
Dangling pointers
Memory leaks