FREE ONLINE INTERMEDIATE C TUTORIALS
40 Intermediate C Tutorials

40 Intermediate C Tutorials

1. Basic Pointers

Pointers store memory addresses for direct data access.

Example: Use a pointer to modify a variable.

#include 
int main() {
int x = 10, *p = &x;
*p = 20;
printf("Value: %d\n", x);
return 0;
}

Value: 20

Note: Use & for address, * for dereference.

2. Pointer to Array

Pointers can access array elements efficiently.

Example: Iterate array using a pointer.

#include 
int main() {
int arr[] = {1, 2, 3};
int *p = arr;
for (int i = 0; i < 3; i++) printf("%d ", *(p + i));
printf("\n");
return 0;
}

1 2 3

Note: Array name is a pointer to first element.

3. Dynamic Memory

Allocate memory at runtime with malloc.

Example: Allocate and use an array.

#include 
#include 
int main() {
int *arr = (int*)malloc(3 * sizeof(int));
arr[0] = 1; arr[1] = 2; arr[2] = 3;
printf("%d %d %d\n", arr[0], arr[1], arr[2]);
free(arr);
return 0;
}

1 2 3

Note: Always free allocated memory.

4. String Handling

Manipulate strings using standard library functions.

Example: Copy and concatenate strings.

#include 
#include 
int main() {
char s1[20] = "Hello", s2[10] = "World";
strcat(s1, s2);
printf("String: %s\n", s1);
return 0;
}

String: HelloWorld

Note: Include . Ensure buffer size.

5. Structures

Group related data using structs.

Example: Define and use a struct.

#include 
struct Point { int x, y; };
int main() {
struct Point p = {3, 4};
printf("Point: (%d, %d)\n", p.x, p.y);
return 0;
}

Point: (3, 4)

Note: Access fields with dot operator.

6. File Writing

Write data to files using FILE pointers.

Example: Write text to a file.

#include 
int main() {
FILE *f = fopen("output.txt", "w");
fprintf(f, "Hello, File!\n");
fclose(f);
printf("Written to file\n");
return 0;
}

Written to file

Note: Use "w" mode. Always close files.

7. File Reading

Read data from files using FILE pointers.

Example: Read text from a file.

#include 
int main() {
FILE *f = fopen("input.txt", "r");
char buffer[20];
fgets(buffer, 20, f);
printf("Read: %s", buffer);
fclose(f);
return 0;
}

Read: [file content]

Note: Use "r" mode. Check file existence.

8. Function Pointers

Store function addresses for dynamic calls.

Example: Call function via pointer.

#include 
int add(int a, int b) { return a + b; }
int main() {
int (*p)(int, int) = add;
printf("Sum: %d\n", p(2, 3));
return 0;
}

Sum: 5

Note: Match function signature.

9. Arrays of Pointers

Store multiple pointers in an array.

Example: Array of string pointers.

#include 
int main() {
char *arr[] = {"Hello", "World"};
printf("%s %s\n", arr[0], arr[1]);
return 0;
}

Hello World

Note: Useful for dynamic data.

10. Linked List

Dynamic structure with linked nodes.

Example: Create and print list.

#include 
#include 
struct Node { int data; struct Node* next; };
int main() {
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head->data = 1; head->next = NULL;
printf("%d\n", head->data);
free(head);
return 0;
}

1

Note: Free nodes to avoid leaks.

11. Stack Array

LIFO structure using an array.

Example: Push and pop elements.

#include 
#define MAX 3
int stack[MAX], top = -1;
void push(int v) { if (top < MAX-1) stack[++top] = v; }
int main() {
push(1);
printf("Popped: %d\n", stack[top--]);
return 0;
}

Popped: 1

Note: Check for overflow/underflow.

12. Queue Array

FIFO structure using an array.

Example: Enqueue and dequeue.

#include 
#define MAX 3
int queue[MAX], front = 0, rear = -1;
void enqueue(int v) { if (rear < MAX-1) queue[++rear] = v; }
int main() {
enqueue(1);
printf("Dequeued: %d\n", queue[front++]);
return 0;
}

Dequeued: 1

Note: Handle full/empty conditions.

13. Struct Pointers

Use pointers to access struct fields.

Example: Modify struct via pointer.

#include 
struct Point { int x, y; };
int main() {
struct Point p = {1, 2}, *ptr = &p;
ptr->x = 3;
printf("X: %d\n", p.x);
return 0;
}

X: 3

Note: Use -> for pointer access.

14. Dynamic Strings

Allocate strings dynamically with malloc.

Example: Create dynamic string.

#include 
#include 
#include 
int main() {
char *s = (char*)malloc(10 * sizeof(char));
strcpy(s, "Hello");
printf("%s\n", s);
free(s);
return 0;
}

Hello

Note: Include . Free memory.

15. Command Line Args

Access program arguments via main.

Example: Print arguments.

#include 
int main(int argc, char *argv[]) {
for (int i = 0; i < argc; i++) {
printf("Arg %d: %s\n", i, argv[i]);
}
return 0;
}

Arg 0: program
Arg 1: test

Note: argv[0] is program name.

16. Error Handling

Check for errors in file operations.

Example: Handle file open error.

#include 
int main() {
FILE *f = fopen("no.txt", "r");
if (!f) printf("File not found\n");
else fclose(f);
return 0;
}

File not found

Note: Check pointers for NULL.

17. Binary Files

Read/write raw data to files.

Example: Write/read integer.

#include 
int main() {
int x = 42;
FILE *f = fopen("data.bin", "wb");
fwrite(&x, sizeof(int), 1, f);
fclose(f);
f = fopen("data.bin", "rb");
fread(&x, sizeof(int), 1, f);
printf("Value: %d\n", x);
fclose(f);
return 0;
}

Value: 42

Note: Use "wb"/"rb" modes.

18. Enum Types

Define named integer constants.

Example: Use enums for states.

#include 
enum State { OFF, ON };
int main() {
enum State s = ON;
printf("State: %d\n", s);
return 0;
}

State: 1

Note: Enums improve code readability.

19. Union Types

Store different types in same memory.

Example: Use union for int/char.

#include 
union Data { int i; char c; };
int main() {
union Data d;
d.i = 65;
printf("Char: %c\n", d.c);
return 0;
}

Char: A

Note: Shares memory; use one field at a time.

20. Recursion

Functions that call themselves.

Example: Calculate factorial.

#include 
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
printf("Factorial 5: %d\n", factorial(5));
return 0;
}

Factorial 5: 120

Note: Ensure base case to avoid stack overflow.

21. String Tokenizing

Split strings into tokens.

Example: Tokenize a string.

#include 
#include 
int main() {
char str[] = "A,B";
char *token = strtok(str, ",");
printf("Token: %s\n", token);
return 0;
}

Token: A

Note: strtok modifies input string.

22. Dynamic 2D Array

Allocate 2D arrays dynamically.

Example: Create 2x2 array.

#include 
#include 
int main() {
int **arr = (int**)malloc(2 * sizeof(int*));
for (int i = 0; i < 2; i++) arr[i] = (int*)malloc(2 * sizeof(int));
arr[0][0] = 1;
printf("%d\n", arr[0][0]);
for (int i = 0; i < 2; i++) free(arr[i]);
free(arr);
return 0;
}

1

Note: Free each row and array.

23. Void Pointers

Generic pointers for any data type.

Example: Use void pointer.

#include 
int main() {
int x = 10;
void *p = &x;
printf("Value: %d\n", *(int*)p);
return 0;
}

Value: 10

Note: Cast before dereferencing.

24. Const Qualifiers

Prevent modification of variables.

Example: Use const pointer.

#include 
int main() {
const int x = 10;
const int *p = &x;
printf("Value: %d\n", *p);
return 0;
}

Value: 10

Note: Const ensures read-only access.

25. Static Variables

Retain value between function calls.

Example: Count function calls.

#include 
void count() {
static int c = 0;
printf("Count: %d\n", ++c);
}
int main() {
count(); count();
return 0;
}

Count: 1
Count: 2

Note: Static retains value across calls.

26. Preprocessor Macros

Define constants or simple functions.

Example: Use macro for square.

#include 
#define SQUARE(x) ((x) * (x))
int main() {
printf("Square: %d\n", SQUARE(5));
return 0;
}

Square: 25

Note: Parentheses prevent operator issues.

27. Bitwise Operators

Manipulate bits for efficient operations.

Example: Set a bit.

#include 
int main() {
int x = 0;
x |= (1 << 2);
printf("Value: %d\n", x);
return 0;
}

Value: 4

Note: Use <<, |, & for bit operations.

28. File Positioning

Move file pointer for specific access.

Example: Seek and read.

#include 
int main() {
FILE *f = fopen("input.txt", "r");
fseek(f, 2, SEEK_SET);
char c = fgetc(f);
printf("Char: %c\n", c);
fclose(f);
return 0;
}

Char: [third char]

Note: Use SEEK_SET, SEEK_CUR, SEEK_END.

29. Array Sorting

Sort arrays using standard library.

Example: Sort integers.

#include 
#include 
int cmp(const void *a, const void *b) { return (*(int*)a - *(int*)b); }
int main() {
int arr[] = {3, 1, 2};
qsort(arr, 3, sizeof(int), cmp);
printf("%d %d %d\n", arr[0], arr[1], arr[2]);
return 0;
}

1 2 3

Note: Define comparison function.

30. Linked List Insert

Insert nodes into linked list.

Example: Insert at head.

#include 
#include 
struct Node { int data; struct Node* next; };
int main() {
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head->data = 2; head->next = NULL;
struct Node* new = (struct Node*)malloc(sizeof(struct Node));
new->data = 1; new->next = head;
printf("%d\n", new->data);
free(new); free(head);
return 0;
}

1

Note: Update pointers carefully.

31. Struct Arrays

Store multiple structs in an array.

Example: Array of structs.

#include 
struct Point { int x, y; };
int main() {
struct Point arr[2] = {{1, 2}, {3, 4}};
printf("Point 0: (%d, %d)\n", arr[0].x, arr[0].y);
return 0;
}

Point 0: (1, 2)

Note: Access with index and dot.

32. String Formatting

Format strings with sprintf.

Example: Create formatted string.

#include 
int main() {
char buffer[20];
int x = 42;
sprintf(buffer, "Value: %d", x);
printf("%s\n", buffer);
return 0;
}

Value: 42

Note: Ensure buffer size is sufficient.

33. Pass by Reference

Modify variables using pointers.

Example: Swap two numbers.

#include 
void swap(int *a, int *b) {
int temp = *a; *a = *b; *b = temp;
}
int main() {
int x = 1, y = 2;
swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
}

2 1

Note: Use pointers for modification.

34. Dynamic Memory Free

Properly free allocated memory.

Example: Allocate and free.

#include 
#include 
int main() {
int *p = (int*)malloc(sizeof(int));
*p = 10;
printf("Value: %d\n", *p);
free(p);
return 0;
}

Value: 10

Note: Avoid dangling pointers.

35. Simple Stack

Implement stack with dynamic memory.

Example: Push to stack.

#include 
#include 
struct Stack { int *data; int top; };
int main() {
struct Stack s = {(int*)malloc(2 * sizeof(int)), -1};
s.data[++s.top] = 1;
printf("Top: %d\n", s.data[s.top]);
free(s.data);
return 0;
}

Top: 1

Note: Manage stack size dynamically.

36. Simple Queue

Implement queue with dynamic memory.

Example: Enqueue and dequeue.

#include 
#include 
struct Queue { int *data; int front, rear; };
int main() {
struct Queue q = {(int*)malloc(2 * sizeof(int)), 0, -1};
q.data[++q.rear] = 1;
printf("Dequeued: %d\n", q.data[q.front++]);
free(q.data);
return 0;
}

Dequeued: 1

Note: Track front and rear indices.

37. Nested Structures

Structures containing other structures.

Example: Nested struct access.

#include 
struct Point { int x, y; };
struct Rect { struct Point topLeft; int width; };
int main() {
struct Rect r = {{1, 2}, 10};
printf("X: %d\n", r.topLeft.x);
return 0;
}

X: 1

Note: Use dot for nested access.

38. Array of Functions

Store function pointers in array.

Example: Call functions dynamically.

#include 
int add(int a) { return a + 1; }
int main() {
int (*funcs[1])(int) = {add};
printf("Result: %d\n", funcs[0](5));
return 0;
}

Result: 6

Note: Match function signatures.

39. File Append

Add data to existing file.

Example: Append text.

#include 
int main() {
FILE *f = fopen("output.txt", "a");
fprintf(f, "More text\n");
fclose(f);
printf("Appended\n");
return 0;
}

Appended

Note: Use "a" mode for append.

40. Basic Binary Tree

Tree with left and right nodes.

Example: Create simple tree.

#include 
#include 
struct Node { int data; struct Node *left, *right; };
int main() {
struct Node* root = (struct Node*)malloc(sizeof(struct Node));
root->data = 1; root->left = root->right = NULL;
printf("Root: %d\n", root->data);
free(root);
return 0;
}

Root: 1

Note: Free nodes to avoid leaks.

Complete C Programming Guide + Compilers Collection


1. C srand() Function – Understanding Seed Initialization

https://macronepal.com/understanding-the-c-srand-function
Explains how srand() initializes the pseudo-random number generator in C by setting a seed value. Using the same seed produces the same sequence, while time(NULL) gives different results each run.


2. C rand() Function Mechanics and Limitations

https://macronepal.com/c-rand-function-mechanics-and-limitations
Explains how rand() generates pseudo-random numbers between 0 and RAND_MAX, its deterministic nature, and limitations for security use cases.


3. C log() Function

https://macronepal.com/c-log-function-2
Covers natural logarithm calculation using <math.h> and its applications.


4. Mastering Date and Time in C

https://macronepal.com/mastering-date-and-time-in-c
Explains <time.h> functions like time(), clock(), difftime(), and struct tm.


5. Mastering time_t Type in C

https://macronepal.com/mastering-the-c-time_t-type-for-time-management
Explains time representation as seconds since Unix epoch and conversion functions.


6. C exp() Function

https://macronepal.com/c-exp-function-mechanics-and-implementation
Explains exponential function exp(x) and its scientific applications.


7. C log() Function (Alternate Guide)

https://macronepal.com/c-log-function
Comparison of log() and log10() with usage examples.


8. C log10() Function

https://macronepal.com/mastering-the-log10-function-in-c
Explains base-10 logarithm for engineering and scientific applications.


9. C tan() Function

https://macronepal.com/understanding-the-c-tan-function
Explains tangent function and radian-based calculations.


10. Random Numbers in C (Secure vs Predictable)

https://macronepal.com/mastering-c-random-numbers-for-secure-and-predictable-applications
Explains difference between rand() and secure randomness methods.


11. Free Online C Compiler

https://macronepal.com/free-online-c-code-compiler-2
Browser-based compiler for testing C programs instantly.


C Functions, Arguments, Parameters & Flow

Mastering Functions in C – Complete Guide

https://macronepal.com/c/mastering-functions-in-c-a-complete-guide/
Covers function structure, modular programming, and real-world usage.


Function Arguments in C

https://macronepal.com/c-function-arguments/
Explains how arguments are passed and used in function calls.


Function Parameters in C

https://macronepal.com/c-function-parameters/
Explains defining inputs for functions and matching them with arguments.


Function Declarations in C

https://macronepal.com/c-function-declarations-syntax-rules-and-best-practices/
Covers prototypes, syntax rules, and best practices.


Function Calls in C

https://macronepal.com/understanding-function-calls-in-c-syntax-mechanics-and-best-practices/
Explains execution flow and parameter handling during function calls.


Void Functions in C

https://macronepal.com/understanding-void-functions-in-c-syntax-patterns-and-best-practices/
Explains functions that do not return values.


Return Values in C

https://macronepal.com/c-return-values-mechanics-types-and-best-practices/
Explains different return types and how functions return results.


Pass-by-Value in C

https://macronepal.com/aws/understanding-pass-by-value-in-c-mechanics-implications-and-best-practices/
Explains how copies of variables are passed into functions.


Pass-by-Reference in C

https://macronepal.com/c/understanding-pass-by-reference-in-c-pointers-semantics-and-safe-practices/
Explains using pointers to modify original variables.


C strstr() Function

https://macronepal.com/aws/c-strstr-function/
Explains substring search inside strings in C.


C Preprocessor & Macros

https://macronepal.com/mastering-c-variadic-macros-for-flexible-debugging/
https://macronepal.com/mastering-the-stdc-macro-in-c/
https://macronepal.com/c-time-macro-mechanics-and-usage/
https://macronepal.com/understanding-the-c-date-macro/
https://macronepal.com/c-file-type/
https://macronepal.com/mastering-c-line-macro-for-debugging-and-diagnostics/
https://macronepal.com/mastering-predefined-macros-in-c/
https://macronepal.com/c-error-directive-mechanics-and-usage/
https://macronepal.com/understanding-the-c-pragma-directive/
https://macronepal.com/c-include-directive/


C Structures, Memory, Scope & Linkage

https://macronepal.com/mastering-structures-in-c/
https://macronepal.com/c-structure-declaration-mechanics-and-usage/
https://macronepal.com/c-structure-initialization-mechanics-and-best-practices/
https://macronepal.com/mastering-c-structure-member-access-for-reliable-data-handling/
https://macronepal.com/c-nested-structures/
https://macronepal.com/mastering-arrays-of-structures-in-c/
https://macronepal.com/c-structure-pointers-mechanics-and-implementation/
https://macronepal.com/understanding-c-structure-parameter-passing-mechanics/
https://macronepal.com/mastering-c-returning-structures-for-efficient-data-flow/
https://macronepal.com/c-self-referential-structures/
https://macronepal.com/mastering-structure-alignment-in-c/
https://macronepal.com/c-structure-padding-mechanics-and-optimization/
https://macronepal.com/understanding-c-flexible-array-members-mechanics-and-usage/
https://macronepal.com/mastering-c-anonymous-structures-for-flattened-data-layouts/
https://macronepal.com/c-unions/
https://macronepal.com/mastering-c-name-mangling-and-symbol-decoration/
https://macronepal.com/c-no-linkage-mechanics-and-scope-isolation/
https://macronepal.com/understanding-c-internal-linkage-mechanics-and-architecture/


C Scope, Storage Classes & Typedef

https://macronepal.com/mastering-function-prototype-scope-in-c/
https://macronepal.com/c-function-scope-mechanics-and-visibility/
https://macronepal.com/understanding-c-file-scope-mechanics-and-architecture/
https://macronepal.com/mastering-c-scope-rules-for-predictable-name-resolution/
https://macronepal.com/c-scope-rules/
https://macronepal.com/mastering-c-register-storage-class-for-historical-context-and-modern-alternatives/
https://macronepal.com/mastering-_thread_local-in-c/
https://macronepal.com/c-extern-storage-class-mechanics-and-usage/
https://macronepal.com/understanding-the-c-static-storage-class-mechanics-and-usage/
https://macronepal.com/c-auto-storage-class/
https://macronepal.com/c-typedef-with-pointers/


Extra Articles

https://macronepal.com/13757-2/
https://macronepal.com/13748-2/
https://macronepal.com/13747-2/
https://macronepal.com/13746-2/
https://macronepal.com/13745-2/
https://macronepal.com/13708-2/
https://macronepal.com/13707-2/
https://macronepal.com/13702-2/


Online Compilers

https://macronepal.com/free-html-online-code-compiler/
https://macronepal.com/free-online-python-code-compiler/
https://macronepal.com/free-online-python2-code-compiler/
https://macronepal.com/free-online-java-code-compiler/
https://macronepal.com/free-online-javascript-code-compiler/
https://macronepal.com/free-online-node-js-code-compiler/
https://macronepal.com/free-online-c-code-compiler/
https://macronepal.com/free-online-c-code-compiler-2/
https://macronepal.com/free-online-c-code-compiler-3/
https://macronepal.com/free-online-php-code-compiler/
https://macronepal.com/free-online-ruby-code-compiler/
https://macronepal.com/free-online-perl-code-compiler/
https://macronepal.com/free-online-lua-code-compiler/
https://macronepal.com/free-online-tcl-code-compiler/
https://macronepal.com/free-online-groovy-code-compiler/
https://macronepal.com/free-online-j-shell-code-compiler/
https://macronepal.com/free-online-haskell-code-compiler/
https://macronepal.com/free-online-scala-code-compiler/
https://macronepal.com/free-online-common-lisp-code-compiler/
https://macronepal.com/free-online-d-code-compiler/
https://macronepal.com/free-online-ada-code-compiler/
https://macronepal.com/free-erlang-code-compiler/
https://macronepal.com/free-online-assembly-code-compiler/

Leave a Reply

Your email address will not be published. Required fields are marked *


Macro Nepal Helper