You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A personal reference repository covering core C programming concepts from fundamentals to data structures and algorithms. Each folder is a topic, and each file is a focused example.
Declare and initialize variables of different data types (int, float, char).
Format specifiers: %d, %f, %c, %s.
User Input (userinput.c)
scanf() reads user input from stdin.
Always pass the address of the variable: scanf("%d", &x).
Conditionals (if_else.c, switch_case.c)
if / else if / else for branching logic.
switch is cleaner when matching one variable to many constant values.
Loops (for_loop.c, while_loop.c)
for loop: best when iteration count is known.
while loop: best when condition is unknown ahead of time.
Arrays (arrays.c, arrays_loop.c, multi_dim_arr.c)
Fixed-size collection of elements of the same type.
Index starts at 0: arr[0] is the first element.
Multi-dimensional arrays: int grid[3][3] β row Γ column.
Functions (functions.c)
// Two types: library (printf, rand) and user-definedintsum(inta, intb); // prototype (declare before main)intsum(inta, intb) { // definitionreturna+b;
}
Key rules:
Declare a prototype before main().
A function can return only one value.
Changes to a parameter inside a function do not affect the caller (pass-by-value).
Recursion (recursion.c)
intfactorial(intx) {
if (x==1||x==0) return1; // base case β stops recursionreturnx*factorial(x-1); // recursive call
}
A function calling itself.
Base case is mandatory β without it, you get infinite recursion β stack overflow.
Useful for: factorial, Fibonacci, tree traversal.
Practice Problems (basics/problems/)
File
Topic
prob1.c
General problem
revArr.c
Reverse an array
tableOf.c
Multiplication table
multi_arr.c
Multi-dimensional array practice
2. π€ Strings (strings/)
Strings in C are arrays of characters terminated by '\0'.
charstr[] ="Kalpit"; // stored as: K a l p i t \0char*ptr=str;
while (*ptr!='\0') { printf("%c", *ptr); ptr++; }
A char[] array cannot be re-assigned β use strcpy().
A char * pointer can be re-pointed: name = "NewName";
Key string functions
Function
Purpose
Header
strlen(s)
Length of string
<string.h>
strcpy(dst, src)
Copy string
<string.h>
strcat(dst, src)
Concatenate strings
<string.h>
gets(s) / puts(s)
Read/print whole line
<stdio.h>
Files
File
What it covers
strings.c
String declaration & pointer traversal
strlen.c
Manual length calculation
strcpycat.c
strcpy and strcat demos
str_slice.c
Extracting substrings
str_char_input.c
Reading character by character
gets_puts_str.c
gets / puts usage
3. π― Pointers (oop/)
A pointer stores the memory address of another variable.
inta=4;
int*p=&a; // p holds the address of aint**k=&p; // k holds the address of p (double pointer)printf("%d", *p); // dereference: prints value at address β 4
Symbol
Meaning
&x
Address of variable x
*p
Value at the address stored in p
**k
Value at address of address (double pointer)
Files
File
Topic
pointers.c
Single and double pointer basics
pointer_arithmatic.c
Incrementing pointers through arrays
pointer_array.c
Array name as a pointer
ptr_arr_scanf.c
Reading arrays via pointers
arr_func_ptr.c
Passing arrays to functions via pointers
4. π§ Dynamic Memory Allocation (dma/)
C arrays have a fixed size at compile-time. DMA lets you allocate memory at runtime (on the heap).
intglobal=3; // accessible everywhereintfunc1() {
staticinta=5; // initialized ONCE; retains value across callsa++;
returna; // returns 6, then 7, then 8...
}
Type
Scope
Lifetime
Initialized
Local
Inside block
Function call
Every call
Global
Entire file
Program lifetime
Once (0 default)
Static
Inside block
Program lifetime
Once (0 default)
Unions (unions.c)
uniontest {
inta;
floatb;
chard[34]; // all share the SAME memory block
};
All members share one memory location (size = largest member).
Only one member is valid at a time β writing to one corrupts others.
Use case: memory-efficient storage when only one field is active at a time.
Function Pointers (function_pointers.c)
intsum(inta, intb) { returna+b; }
int (*fPtr)(int, int); // declare function pointerfPtr=∑ // point to functionintresult= (*fPtr)(4, 6); // call through pointer β 10
Useful for callbacks and strategy patterns.
Syntax: returnType (*pointerName)(paramTypes)
Callback Functions (callback_function.c)
A function passed as an argument to another function.
Enables flexible, pluggable logic.
goto Statement (gotostatment.c)
Jumps to a labelled line in code.
Avoid in most cases β makes code hard to follow. Useful only for breaking out of nested loops.
7. π File I/O (fileio/)
Files are accessed through a FILE * pointer.
FILE*ptr=fopen("file.txt", "r"); // open for readingif (ptr==NULL) { /* handle error */ }
fclose(ptr); // always close the file
File Modes
Mode
Meaning
"r"
Read (error if file doesn't exist)
"w"
Write (creates or overwrites file)
"a"
Append (creates if not exists)
"rb" / "wb"
Binary read/write
Key Functions
Function
Purpose
fopen(path, mode)
Open a file
fclose(fp)
Close a file
fprintf(fp, ...)
Write formatted text
fscanf(fp, ...)
Read formatted text
fgetc(fp)
Read one character
fputc(c, fp)
Write one character
Files
File
Topic
fileio.c
File modes overview
file_write.c
Writing to a file
file_read.c
Reading from a file
file_check.c
Checking if a file exists
fgetc_file_read.c
Reading char by char
file_fgetc_fputc.c
Copy file char by char
8. βοΈ Preprocessing (preproccessing/)
Preprocessor directives run before compilation and start with #.
#include
#include<stdio.h>// system header β looks in system directories#include"info.c"// local file β looks in current directory
#define
#definePI 3.14 // constant β text substitution
#defineSQUARE(r) r*r // macro β acts like an inline function
floatarea=PI*SQUARE(5); // becomes: 3.14 * 5*5
A linked list is a chain of nodes where each node holds data and a pointer to the next node.
structNode {
intdata;
structNode*nextx; // pointer to next node
};
Traversal:
voidlinkedListTraversal(structNode*ptr) {
while (ptr!=NULL) {
printf("%d\n", ptr->data);
ptr=ptr->nextx; // advance to next node
}
}
Insert at front:
structNode*insertAtFirst(structNode*head, intdata) {
structNode*newNode= (structNode*)malloc(sizeof(structNode));
newNode->data=data;
newNode->nextx=head; // point new node to old headreturnnewNode; // new node becomes head
}
Operation
Time Complexity
Traversal
O(n)
Insert at front
O(1)
Insert at end
O(n)
Search
O(n)
Linked list nodes live on the heap (via malloc). Unlike arrays, they are not contiguous in memory.
10. π Small Projects (smallprojects/)
File
Description
fibonacci.c
Nth Fibonacci number using recursion
findthegreatest.c
Find greatest among N numbers
guessthenumber.c
Number guessing game
leapyear.c
Check if a year is a leap year
lowercaseornot.c
Check if character is lowercase
patterns.c
Print star/number patterns
snackwatergun.c
Snake, Water, Gun game (like rock-paper-scissors)
sumof10numbers.c
Sum of first 10 numbers
π Quick Reference Cheatsheet
Data Types & Format Specifiers
Type
Size
Format
int
4 bytes
%d
float
4 bytes
%f
double
8 bytes
%lf
char
1 byte
%c
char[]
n bytes
%s
pointer
8 bytes
%p or %u
Common Headers
Header
Provides
<stdio.h>
printf, scanf, FILE, fopen
<stdlib.h>
malloc, calloc, realloc, free, rand, exit
<string.h>
strlen, strcpy, strcat, strcmp
<math.h>
pow, sqrt, abs
Memory Layout (simplified)
βββββββββββββββ
β Stack β β local variables, function calls
βββββββββββββββ€
β Heap β β malloc / calloc / realloc
βββββββββββββββ€
β Data Segmentβ β global & static variables
βββββββββββββββ€
β Code β β your compiled program instructions
βββββββββββββββ
π οΈ How to Compile & Run
# Compile a single file
gcc filename.c -o output
# Compile with math library
gcc filename.c -o output -lm
# Run
./output