Back to C Programming
2026-07-125 min read

C Additional Topics

Learn C Additional Topics step by step with clear examples and exercises.

Title: Mastering C Additional Topics: A full guide for Practical Depth

Why This Matters

As you progress in your C programming journey, understanding additional topics becomes crucial to tackle real-world problems and acing interviews. These advanced concepts will help you debug complex issues, write efficient code, and impress interviewers with your expertise.

Prerequisites

Before diving into the additional topics, ensure you have a solid grasp of:

  1. Basic C syntax and data types
  2. Control structures (if-else, for, while)
  3. Arrays and pointers
  4. Functions and recursion
  5. File handling
  6. Understanding of data structures like stacks and queues
  7. Knowledge of dynamic memory allocation using malloc() and free()

Core Concept

Strings in C

Strings are sequences of characters terminated by a null character ('\0'). In C, the string.h library provides functions to manipulate strings.

#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "Hello, World!";
char str2[20] = "";

// Concatenating two strings
strcat(str1, " This is a concatenated string.");
printf("%s\n", str1);

// Copying one string to another
strcpy(str2, str1);
printf("Copied string: %s\n", str2);

// Comparing two strings
if (strcmp(str1, "Hello, World!") == 0) {
printf("Both strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}

// Finding the length of a string without built-in functions
int len = 0;
while (str1[len] != '\0') {
++len;
}
printf("Length of the first string: %d\n", len);

return 0;
}

In the above example, we use strcat, strcpy, strcmp, and a custom loop to find the length of a string without using any built-in functions.

Mathematical Functions in C

The math.h library offers various mathematical functions like trigonometric, exponential, logarithmic, and more.

#include <stdio.h>
#include <math.h>

int main() {
double pi = M_PI; // Predefined value of Pi
double sinVal = sin(pi / 4);
double expVal = exp(1);
double logVal = log(25);

printf("Sine of Pi/4: %.6f\n", sinVal);
printf("e raised to the power of 1: %.6f\n", expVal);
printf("Logarithm base e of 25: %.6f\n", logVal);

// Using mathematical functions with custom data types
struct Point {
double x, y;
};

struct Point p1 = {3.0, 4.0};
double distance = sqrt(pow(p1.x - 2.0, 2) + pow(p1.y - 3.0, 2));
printf("Distance between points (2, 3) and (3, 4): %.6f\n", distance);

return 0;
}

In this example, we use sin, exp, log, custom data types, and mathematical functions with custom data types to calculate sine, exponential, logarithmic values, and the distance between two points.

Worked Example

Let's create a program that checks if a number is prime using the Sieve of Eratosthenes algorithm.

#include <stdio.h>

void sieve(int arr[], int n) {
for (int i = 2; i * i <= n; ++i) {
if (arr[i] == 1) { // If the number is already marked as composite, skip it
continue;
}
for (int j = i * i; j <= n; j += i) {
arr[j] = 0; // Mark the multiples of the current prime as composite
}
}
}

int isPrime(int num) {
if (num < 2) return 0; // Numbers less than 2 are not prime
for (int i = 2; i <= sqrt(num); ++i) {
if (num % i == 0) return 0; // If the number is divisible by any number up to its square root, it's not prime
}
return 1; // The number is prime if it's not divisible by any number up to its square root
}

int main() {
int arr[101]; // Array to store whether each number from 2 to 100 is prime or not
for (int i = 0; i < 101; ++i) {
arr[i] = 1; // Initially, all numbers are assumed to be prime
}

sieve(arr, 100); // Run the Sieve of Eratosthenes algorithm

printf("Prime numbers from 2 to 100:\n");
for (int i = 2; i < 101; ++i) {
if (isPrime(i)) { // Use a custom function to check if the number is prime before printing it
printf("%d ", i);
}
}

return 0;
}

In this example, we implement the Sieve of Eratosthenes algorithm to find all prime numbers from 2 to 100 using a custom isPrime() function.

Common Mistakes

  1. Forgetting to include necessary libraries (string.h, math.h, etc.)
  2. Using incorrect function prototypes or arguments for string functions
  3. Not initializing arrays properly before using them
  4. Ignoring the null character when working with strings
  5. Misunderstanding the logic of mathematical functions and their return types
  6. Incorrectly handling dynamic memory allocation, leading to memory leaks or segmentation faults
  7. Not properly checking for errors in mathematical functions that may have undefined behavior for certain inputs
  8. Using outdated or deprecated functions from libraries like stdlib.h and string.h

Practice Questions

  1. Implement a function that reverses a given string in C using recursion.
  2. Write a program to calculate the factorial of a number using recursion with dynamic memory allocation for large factors.
  3. Create a program to find the largest prime number less than or equal to 1,000,000.
  4. Write a program to check if a given year is a leap year in C using a function that takes the year as an argument.
  5. Implement a stack and queue data structure in C using arrays and test their functionality with some example inputs.
  6. Write a program to find the number of occurrences of a specific character in a string without using any built-in functions.
  7. Implement a function that finds the greatest common divisor (GCD) of two numbers using Euclid's algorithm in C.
  8. Create a program to calculate the Fibonacci sequence up to a given number using recursion and dynamic memory allocation for large numbers.
  9. Write a program to sort an array of integers using quicksort algorithm in C.
  10. Implement a binary search function in C that takes an sorted array and a target value as input, and returns the index of the target value if found, otherwise returns -1.

FAQ

  1. Why do we use the null character ('\0') to terminate strings in C?
  • The null character indicates the end of a string, making it easier for programs to handle and manipulate them efficiently.
  1. What is the difference between strcpy() and strncpy() in C?
  • strcpy() copies the entire source string to the destination string, while strncpy() only copies a specified number of characters from the source string to the destination string.
  1. Why do we need to initialize arrays before using them in C?
  • Initializing arrays helps avoid unexpected behavior and ensures that the program starts with known values when working with data structures like arrays.
  1. What is the Sieve of Eratosthenes algorithm, and why is it useful for finding prime numbers?
  • The Sieve of Eratosthenes is an efficient algorithm for finding all prime numbers up to a given limit. It works by iteratively marking the multiples of each prime number as composite, leaving only the primes unmarked. This method is useful because it allows us to find prime numbers quickly and efficiently.
  1. Why do we need to handle dynamic memory allocation carefully in C?
  • Dynamic memory allocation can lead to memory leaks or segmentation faults if not handled properly, so it's essential to use functions like malloc() and free() correctly and ensure that all dynamically allocated memory is freed when no longer needed.