These exercices are designed to support learning and are a great help in preparing for the final exam. The more challenging exercises also serve as an in-depth exploration for those who wish to deepen their understanding beyond the core material
- Write a C program that takes an integer and prints whether it is positive, negative, or zero using if-else statements. Then, rewrite the same using a switch statement for values -1, 0, and 1.
- Write a function that takes two integers and returns their greatest common divisor (GCD). Use this function in a main program where you read two integers and print their GCD
- Implement a recursive function to compute the factorial of a number
- Write a function that prints the first N Fibonacci numbers. Use iteration and recursion variants
- Write a program that reads a string and checks if it is a palindrome (reads the same forwards and backwards). You can treat the string as a char array without pointers
- Implement a calculator program that takes two numbers and an operator (+, -, *, /) and returns the result. Use functions to separate logic
- Write a function to sort an integer array using bubble sort. Test it with user input
- Implement multiplication of two matrices (size defined by the user). Check the validity of the sizes
- Declare an integer variable and an integer pointer. Assign the address of the variable to the pointer. Print the variable's value using both the variable name and dereferencing the pointer
- Create an integer array of 5 elements. Use a pointer to traverse the array and print each element using pointer arithmetic (incrementing the pointer)
- Write a program that takes a string input from the user, and prints the string backwards using pointer manipulation only (no indexing)
- Write a program that declares a pointer to an integer pointer. Assign values such that changing the value via the pointer-to-pointer changes the original integer variable. Print the values to verify
- Write a program that allocates memory for an array of 10 integers using malloc(), fills the array with the first 10 natural numbers, prints them, and then frees the memory
- Modify the previous program to double the size of the array using realloc(), fill the new slots with the next 10 natural numbers, print all 20 numbers, then free the memory
- Implement a function that swaps two integers using pointers. The function should take two integer pointers as parameters and swap the values they point to. Demonstrate this in the main program
- Implement your own strlen(). Write a function that calculates the length of a string using pointer arithmetic only, without indexing
- Declare a 2D array of integers and access elements using pointer-to-pointer notation. Print the entire 2D array using this method
- Write a program that declares a function pointer to a function that takes two integers and returns an integer. Use it to point to functions implementing addition and multiplication and demonstrate calling through the pointer
- Write a program that creates a text file called output.txt and writes a simple message inside it, e.g. "Hello, File System!ˮ
- Write a program that opens the file output.txt created in Exercise 1, reads its content, and prints it on the screen
- Write a program that copies the content of a text file (input.txt) into another file (copy.txt)
- Write a program that reads a text file and counts the number of lines, words, and characters it contains
- Modify the program from Exercise 1 to append a new line of text to the existing file without overwriting the current content
- Write a program that receives a file name as input and checks if the file is readable, writable, and executable by the current user
- Create a program that writes an array of integers to a binary file and then reads back the data from the binary file to verify correctness
- Write a program that appends log entries (with timestamps) to a log file every time it is run
- Write a program that lists all files in a given directory (use POSIX functions, so this is Linux/Unix specific)
- Write a C program that prints the Process ID (PID) and Parent Process ID (PPID) of the running process
- Write a program that creates a child process using fork(). The parent process should print its PID and the childʼs PID. The child should print its own PID and PPID. Make sure the parent waits for the child to finish
- Write a program that creates two child processes. Each child should run a CPU-intensive loop printing a message every second. Observe the interleaving of output to understand process scheduling
- Implement a simple producer-consumer problem using two processes. Use POSIX shared memory and semaphores to synchronize the producer writing an integer to a buffer and the consumer reading it
- Write a program that creates a pipe for communication between a parent and a child process. The parent sends a message string to the child through the pipe, and the child reads and prints it
- Write a program that creates a new thread using pthread_create. The thread should print “Hello from threadˮ and terminate. The main program should wait for the thread to finish using pthread_join
- Modify the previous program to pass an integer argument to the thread function. The thread should print the received number multiplied by 2
- Write the two thread functions print_even and print_odd so that each prints the even and odd numbers respectively from 0 to 20. Each number should be printed on its own line
- Write a program that creates two threads incrementing a shared global counter 1 million times each without synchronization. Run multiple times and observe the final counter value. Explain why it differs from the expected value