■ CPoint-wise
Programming —
· Ready to expand in exam · All Exam Notes
major topics covered
■ 01. Basics of C Program
■ Structure of a C Program
→ Documentation section – Comments about the program (optional)
→ Link section – Header files using #include
→ Definition section – Macros using #define
→ Global declaration – Variables declared outside all functions
→ main() function – Entry point; every C program must have exactly one
→ Subprograms – User-defined functions called from main
#include <stdio.h> // Link section
#define PI 3.14 // Definition
int main() {
printf("Hello World\n");
return 0;
}
■ Tokens in C
→ Keywords – Reserved words: int, float, if, while, return (32 keywords)
→ Identifiers – Names for variables/functions; start with letter or _
→ Constants – Fixed values like 10, 3.14, 'A', "hello"
→ Operators – Symbols performing operations: + - * /
→ Special symbols – { } [ ] ( ) ; ,
→ Strings – Sequence of characters in double quotes
■ 02. Data Types & Variables
Type Size Range Format
int 2 or 4 bytes -32768 to 32767 (2B) %d
float 4 bytes 3.4e-38 to 3.4e+38 %f
double 8 bytes 1.7e-308 to 1.7e+308 %lf
char 1 byte -128 to 127 %c
void 0 bytes No value —
■ Variable Declaration Rules ■ Type Modifiers
→ Must start with a letter or underscore → signed – Can store negative values
(_) (default)
→ No special characters except _ → unsigned – Only positive values,
→ No spaces allowed in name double range
→ Keywords cannot be variable names → short – Reduces size (2 bytes for int)
→ Case-sensitive: age ≠ Age → long – Increases size (4/8 bytes for
→ Max 31 characters recommended int)
→ Example: unsigned long int
C Programming Exam Notes · Page 1 · All the best Rahoof! ■
■ Constants
→ Integer constant – 10, -5, 0 (decimal); 075 (octal); 0x1F (hex)
→ Float constant – 3.14, 2.5e3
→ Character constant – 'A', '\n', '\t' (single quotes)
→ String constant – "hello" (double quotes)
→ Symbolic constant – #define MAX 100
→ const keyword – const int x = 5; (value cannot change)
■ 03. Operators
■ Arithmetic Operators ■ Relational Operators
→ + Addition → == Equal to
→ - Subtraction → != Not equal to
→ * Multiplication → > Greater than
→ / Division (integer div for int/int) → < Less than
→ % Modulus (remainder) — integers → >= Greater than or equal
only → <= Less than or equal
■ Logical Operators ■ Bitwise Operators
→ && AND – both conditions true → & Bitwise AND
→ || OR – at least one true → | Bitwise OR
→ ! NOT – reverses the condition → ^ Bitwise XOR
→ Returns 1 (true) or 0 (false) → ~ Bitwise complement (NOT)
→ << Left shift
→ >> Right shift
■ Special Operators
→ Increment/Decrement – ++a (pre), a++ (post), --a, a--
→ Ternary – condition ? expr1 : expr2
→ sizeof – sizeof(int) returns size in bytes
→ Comma – Evaluates left to right, returns rightmost value
→ Assignment – = += -= *= /= %=
→ Address & Pointer – & (address of), * (dereference)
→ Member access – . (structure), -> (pointer to structure)
■ Precedence (high→low): ()[] .-> → Unary → Arithmetic → Shift → Relational → Bitwise → Logical → Ternary → Assignment →
Comma
■ 04. Control Flow Statements
C Programming Exam Notes · Page 2 · All the best Rahoof! ■
■ if / if-else / else-if Ladder
→ if – Executes block only if condition is true
→ if-else – Executes one of two blocks based on condition
→ else-if ladder – Multiple conditions checked; first true block executes
→ Nested if – if inside another if
if (marks >= 90) printf("A");
else if (marks >= 75) printf("B");
else printf("C");
■ switch Statement
→ Used for multi-way branching based on integer/char value
→ break – Ends each case to prevent fall-through
→ default – Executes when no case matches (optional)
→ Cannot use float or string in switch expression
→ Cases must be constant values — no variables
switch(ch) {
case 'A': printf("Vowel"); break;
case 'E': printf("Vowel"); break;
default: printf("Consonant");
}
■ Loops
→ for loop – Use when iterations are known; init, condition, update in one line
→ while loop – Entry-controlled; condition checked before each iteration
→ do-while – Exit-controlled; body executes at least once, condition after
→ break – Exits the loop immediately
→ continue – Skips current iteration, goes to next
→ goto – Jumps to a labeled statement (avoid in practice)
for(int i=0; i<5; i++) printf("%d ", i); // for
while(i < 5) { printf("%d", i); i++; } // while
do { printf("%d", i); i++; } while(i<5); // do-while
■ 05. Functions
■ Key Points
→ Function = reusable block of code with a specific task
→ Every program has at least main() function
→ Syntax – return_type function_name(parameters) { body }
→ Declaration – Prototype tells compiler about function before definition
→ Definition – Actual body of the function
→ Function can return only one value using return
→ void – Use if function returns nothing
int add(int a, int b) { // definition
return a + b;
}
int result = add(3, 4); // call -> result = 7
C Programming Exam Notes · Page 3 · All the best Rahoof! ■
■ Call by Value ■ Call by Reference
→ Copy of argument is passed → Address of variable is passed
→ Changes inside function do NOT → Changes inside function DO affect
affect original original
→ Default method in C → Uses pointers as parameters
→ Safe — original data protected → Useful for returning multiple values
■ Recursion
→ Function calling itself is recursion
→ Base case – Must exist to stop recursion, else stack overflow
→ Each call gets its own stack frame
→ Used for – factorial, fibonacci, tower of Hanoi, binary search
int factorial(int n) {
if(n == 0) return 1; // base case
return n * factorial(n-1); // recursive call
}
■ 06. Arrays
■ Key Points
→ Array = collection of same data type in contiguous memory
→ Index starts from 0; last index = size - 1
→ Declaration – int arr[5];
→ Initialization – int arr[] = {1, 2, 3, 4, 5};
→ Array name is the base address (pointer to first element)
→ Array size must be a constant at declaration
→ No built-in bounds checking — programmer's responsibility
■ 2D Arrays (Matrix)
→ Declaration – int mat[3][3]; (rows × columns)
→ Access – mat[i][j] (row i, column j)
→ Stored in row-major order in memory
→ Used for – matrices, tables, image data
int mat[2][3] = {{1,2,3},{4,5,6}};
// mat[0][1]=2, mat[1][2]=6
■ Passing array to function: void display(int arr[], int n) — array is always passed by reference (address).
■ 07. Strings
■ Key Points
→ String = array of characters ending with null character '\0'
→ Declaration – char name[20]; or char name[] = "Rahoof";
→ scanf("%s", name) – stops at whitespace
→ gets(name) – reads full line including spaces
→ fgets(name,20,stdin) – safer alternative to gets()
→ Strings cannot be assigned with = after declaration (use strcpy)
C Programming Exam Notes · Page 4 · All the best Rahoof! ■
Function Purpose Example
strlen(s) Length (excludes '\0') strlen("hi") → 2
strcpy(d, s) Copy s into d strcpy(a, "hello")
strcat(d, s) Append s to end of d strcat(a, " world")
strcmp(s1,s2) Compare: 0=equal, <0/>0 diff strcmp("ab","ab")→0
strupr(s) Convert to uppercase strupr("hello")→"HELLO"
strlwr(s) Convert to lowercase strlwr("HI")→"hi"
strrev(s) Reverse a string strrev("abc")→"cba"
strstr(s,sub) Find substring in string strstr("hello","ll")
■ 08. Pointers
■ Key Points
→ Pointer = variable that stores the address of another variable
→ Declaration – int *p; (pointer to int)
→ & operator – returns address of a variable
→ * operator – dereferences pointer (accesses value at address)
→ NULL pointer – int *p = NULL; points to nothing (address 0)
→ Pointer size is always 4 bytes (32-bit) or 8 bytes (64-bit)
int x = 10;
int *p = &x; // p holds address of x
printf("%d", *p); // prints 10
*p = 20; // x becomes 20
■ Pointer Arithmetic ■ Types of Pointers
→ p++ – moves to next element (by → void *p – can point to any type
sizeof type) → NULL pointer – not pointing to
→ p-- – moves to previous element anything
→ p + n – points n elements ahead → Dangling pointer – points to freed
→ p1 - p2 – number of elements memory
between them → Wild pointer – uninitialized pointer
→ Works correctly only within an array → Double pointer – int **pp; pointer to
pointer
■ Pointer & Array Relationship
→ Array name = pointer to its first element
→ arr[i] is equivalent to *(arr + i)
→ &arr;[i] is equivalent to arr + i
→ Pointer can traverse array using p++
■ Dynamic memory: malloc(), calloc(), realloc(), free() — from stdlib.h
■ 09. Structures & Unions
C Programming Exam Notes · Page 5 · All the best Rahoof! ■
■ Structure — Key Points
→ Structure = collection of different data types under one name
→ Declared using struct keyword
→ member – Each element inside a structure is called a member
→ dot operator (.) – Access members of structure variable
→ arrow operator (->) – Access members via pointer to structure
→ Size of structure ≥ sum of member sizes (due to padding)
→ Can be nested, passed to functions, used in arrays
struct Student {
int roll;
char name[20];
float marks;
};
struct Student s1 = {1, "Rahoof", 95.5};
printf("%s", [Link]); // dot operator
■ Union — Key Points ■ Struct vs Union
→ Similar to struct but shares memory → Memory – Struct: sum of all; Union:
for all members largest only
→ Size = size of largest member → Active members – Struct: all at
→ Only one member can hold a value at once; Union: one at a time
a time → Keyword – struct vs union
→ Used to save memory
→ Declared using union keyword
■ typedef
→ Creates an alias for an existing data type
→ Commonly used with structures to avoid writing struct every time
typedef struct {
int x, y;
} Point;
Point p1 = {3, 4}; // no need to write struct
■ 10. File I/O
■ Key Points
→ Files allow data to be stored permanently on disk
→ File pointer – FILE *fp;
→ fopen() – Opens a file; returns NULL if file not found
→ fclose() – Closes the file (always close after use)
→ feof(fp) – Returns true if end of file is reached
→ Text files – Readable; stores as characters
→ Binary files – Faster; stores raw bytes (use "rb", "wb")
Mode Meaning
"r" Open for reading (file must exist)
"w" Open for writing (creates new / overwrites existing)
"a" Open for appending (adds at end of file)
"r+" Read and write (file must exist)
C Programming Exam Notes · Page 6 · All the best Rahoof! ■
Mode Meaning
"w+" Read and write (creates / overwrites)
"rb","wb" Binary read / write
Function Purpose
fprintf(fp, "fmt", vars) Write formatted data to file
fscanf(fp, "fmt", &vars;) Read formatted data from file
fputc(ch, fp) Write a single character
fgetc(fp) Read a single character
fputs(str, fp) Write a string to file
fgets(str, n, fp) Read a string from file
fwrite(&var;, size, n, fp) Write binary data
fread(&var;, size, n, fp) Read binary data
fseek(fp, offset, origin) Move file pointer position
rewind(fp) Go back to beginning of file
■ Example
FILE *fp = fopen("[Link]", "w");
if(fp == NULL) { printf("Error"); return 1; }
fprintf(fp, "Hello File!\n");
fclose(fp);
■ 11. Storage Classes
Class Keyword Default Scope Lifetime
Automatic auto Garbage Local (block) Block duration
Register register Garbage Local (block) Block duration
Static static Zero Local or file Entire program
External extern Zero Global (all files) Entire program
■ auto ■ register
→ Default for all local variables → Stored in CPU register for speed
→ Created on stack; destroyed when → Cannot use & (no memory address)
block ends → Use for frequently accessed variables
→ Stored in RAM (loop counters)
■ static ■ extern
→ Retains value between function calls → Declares variable defined in another
→ Initialized only once file
→ Static global = visible only in that file → Does not create new storage —
references existing
→ Used to share global variables across
files
■ 12. Preprocessor Directives
C Programming Exam Notes · Page 7 · All the best Rahoof! ■
■ Key Points
→ Preprocessor processes source code before compilation
→ All directives start with # symbol
→ No semicolon at the end of preprocessor directive
Directive Purpose Example
#include Include header file #include <stdio.h>
#define Define macro/constant #define PI 3.14
#undef Undefine a macro #undef PI
#ifdef If macro is defined #ifdef DEBUG
#ifndef If macro is NOT defined #ifndef HEADER_H
#if/#else/#endif Conditional compilation #if VERSION > 2
#pragma Compiler-specific instructions #pragma once
■ Macro Example
#define SQUARE(x) ((x)*(x)) // function-like macro
printf("%d", SQUARE(5)); // outputs 25
#ifdef DEBUG
printf("Debug mode on\n");
#endif
■ Common headers: stdio.h (I/O), stdlib.h (malloc/exit), string.h (string funcs), math.h (sqrt/pow), ctype.h (isdigit/toupper)
All 12 topics covered · C Programming Exam Notes · All the best Rahoof! ■
C Programming Exam Notes · Page 8 · All the best Rahoof! ■