C
exercises.c🔧c
/*
* ============================================================================
* HISTORY AND FEATURES OF C - EXERCISES
* ============================================================================
* Complete the exercises below to test your understanding of C features.
*
* Instructions:
* 1. Read each exercise carefully
* 2. Write your code in the designated area
* 3. Compile and test: gcc exercises.c -o exercises && ./exercises
* 4. Compare your output with the expected output
* ============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*
* ============================================================================
* EXERCISE 1: Understanding C Keywords
* ============================================================================
* Difficulty: Easy
*
* Task: C has 32 keywords in C89 standard. Create variables using different
* data type keywords and print their values.
*
* Expected Output:
* Integer value: [your value]
* Float value: [your value]
* Character value: [your value]
* Double value: [your value]
*/
void exercise_1() {
printf("\n=== Exercise 1: C Keywords ===\n");
// TODO: Declare an integer variable named 'myInt' with value 42
// TODO: Declare a float variable named 'myFloat' with value 3.14
// TODO: Declare a character variable named 'myChar' with value 'C'
// TODO: Declare a double variable named 'myDouble' with value 2.71828
// TODO: Print all variables using printf
// Hint: Use %d for int, %f for float, %c for char, %lf for double
}
/*
* ============================================================================
* EXERCISE 2: Demonstrating Portability
* ============================================================================
* Difficulty: Easy
*
* Task: Write a program that prints a greeting message and shows that the
* same code works on different platforms.
*
* Expected Output:
* Hello from C Programming!
* This code can run on: [Windows/Linux/macOS]
* C is truly portable!
*/
void exercise_2() {
printf("\n=== Exercise 2: Portability ===\n");
// TODO: Print "Hello from C Programming!"
// TODO: Use preprocessor directives to detect and print the OS
// Hint: #ifdef _WIN32, #elif __linux__, #elif __APPLE__
// TODO: Print "C is truly portable!"
}
/*
* ============================================================================
* EXERCISE 3: Working with Pointers (Low-Level Feature)
* ============================================================================
* Difficulty: Medium
*
* Task: Demonstrate C's low-level capabilities by using pointers to:
* 1. Store the address of a variable
* 2. Access the value through the pointer
* 3. Modify the value through the pointer
*
* Expected Output:
* Original value: 50
* Address of variable: 0x... (some hex address)
* Value through pointer: 50
* Modified value: 100
*/
void exercise_3() {
printf("\n=== Exercise 3: Pointers ===\n");
// TODO: Declare an integer variable 'num' with value 50
// TODO: Declare a pointer 'ptr' that stores the address of 'num'
// TODO: Print the original value of num
// TODO: Print the address of num (use %p format specifier)
// TODO: Print the value accessed through the pointer (use *ptr)
// TODO: Modify the value to 100 using the pointer (use *ptr = 100)
// TODO: Print the modified value of num
}
/*
* ============================================================================
* EXERCISE 4: Structured Programming
* ============================================================================
* Difficulty: Medium
*
* Task: Create three separate functions to demonstrate structured programming:
* 1. A function to calculate the square of a number
* 2. A function to calculate the cube of a number
* 3. A function to check if a number is even or odd
*
* Expected Output:
* Square of 5: 25
* Cube of 3: 27
* 7 is odd
* 8 is even
*/
// TODO: Define a function 'square' that takes an int and returns its square
// TODO: Define a function 'cube' that takes an int and returns its cube
// TODO: Define a function 'checkEvenOdd' that takes an int and prints if it's even or odd
void exercise_4() {
printf("\n=== Exercise 4: Structured Programming ===\n");
// TODO: Call square(5) and print the result
// TODO: Call cube(3) and print the result
// TODO: Call checkEvenOdd(7) and checkEvenOdd(8)
}
/*
* ============================================================================
* EXERCISE 5: Using Standard Library Functions
* ============================================================================
* Difficulty: Easy
*
* Task: Use functions from different C standard libraries to:
* 1. Find the length of a string (string.h)
* 2. Generate a random number between 1 and 100 (stdlib.h)
* 3. Get the current time (time.h)
*
* Expected Output:
* String "Hello, C!" has length: 9
* Random number between 1 and 100: [random number]
* Current time: [current date and time]
*/
void exercise_5() {
printf("\n=== Exercise 5: Standard Library ===\n");
// TODO: Create a string "Hello, C!" and find its length using strlen()
// TODO: Generate a random number between 1 and 100
// Hint: Use srand(time(NULL)) to seed, then rand() % 100 + 1
// TODO: Get and print the current time using time() and ctime()
}
/*
* ============================================================================
* EXERCISE 6: Memory Management
* ============================================================================
* Difficulty: Hard
*
* Task: Demonstrate dynamic memory allocation by:
* 1. Allocating memory for an array of 5 integers
* 2. Filling it with values 10, 20, 30, 40, 50
* 3. Printing the array
* 4. Freeing the memory
*
* Expected Output:
* Dynamic array created!
* Array values: 10 20 30 40 50
* Memory freed successfully!
*/
void exercise_6() {
printf("\n=== Exercise 6: Memory Management ===\n");
// TODO: Allocate memory for 5 integers using malloc()
// Hint: int *arr = (int*)malloc(5 * sizeof(int));
// TODO: Check if memory allocation was successful
// TODO: Fill the array with values 10, 20, 30, 40, 50
// TODO: Print the array values
// TODO: Free the allocated memory using free()
}
/*
* ============================================================================
* EXERCISE 7: Recursion
* ============================================================================
* Difficulty: Medium
*
* Task: Write a recursive function to calculate the sum of digits of a number.
* For example: sumOfDigits(123) = 1 + 2 + 3 = 6
*
* Expected Output:
* Sum of digits of 123: 6
* Sum of digits of 9876: 30
* Sum of digits of 5: 5
*/
// TODO: Define a recursive function 'sumOfDigits' that calculates sum of digits
// Hint: Base case: if n < 10, return n
// Recursive case: return (n % 10) + sumOfDigits(n / 10)
void exercise_7() {
printf("\n=== Exercise 7: Recursion ===\n");
// TODO: Test sumOfDigits with 123, 9876, and 5
}
/*
* ============================================================================
* EXERCISE 8: Bit Manipulation (Low-Level Feature)
* ============================================================================
* Difficulty: Hard
*
* Task: Demonstrate C's low-level bit manipulation capabilities:
* 1. Check if a number is even using bitwise AND
* 2. Multiply a number by 2 using left shift
* 3. Divide a number by 2 using right shift
* 4. Toggle all bits using bitwise NOT
*
* Expected Output:
* Number 10:
* Is even (using & 1): Yes
* Multiplied by 2 (using << 1): 20
* Divided by 2 (using >> 1): 5
* Bitwise NOT of 10: -11
*/
void exercise_8() {
printf("\n=== Exercise 8: Bit Manipulation ===\n");
int num = 10;
// TODO: Check if num is even using bitwise AND with 1
// Hint: (num & 1) == 0 means even
// TODO: Multiply num by 2 using left shift
// Hint: num << 1
// TODO: Divide num by 2 using right shift
// Hint: num >> 1
// TODO: Show bitwise NOT of num
// Hint: ~num
}
/*
* ============================================================================
* BONUS EXERCISE: Feature Summary Program
* ============================================================================
* Difficulty: Medium
*
* Task: Create a program that demonstrates at least 5 features of C in a
* creative way. Combine pointers, functions, loops, and any other
* features you've learned about.
*/
void bonus_exercise() {
printf("\n=== BONUS: Feature Summary ===\n");
// TODO: Be creative! Show off what you've learned about C's features
// Ideas:
// - Create a function that uses pointers
// - Use bit manipulation for something practical
// - Combine recursion with other features
// - Demonstrate memory allocation
}
/*
* ============================================================================
* MAIN FUNCTION
* ============================================================================
*/
int main() {
printf("╔════════════════════════════════════════════════╗\n");
printf("║ HISTORY AND FEATURES OF C - EXERCISES ║\n");
printf("║ Complete each exercise below ║\n");
printf("╚════════════════════════════════════════════════╝\n");
// Uncomment each exercise as you complete it
// exercise_1();
// exercise_2();
// exercise_3();
// exercise_4();
// exercise_5();
// exercise_6();
// exercise_7();
// exercise_8();
// bonus_exercise();
printf("\n=== Uncomment exercises in main() to run them ===\n");
return 0;
}
/*
* ============================================================================
* ANSWER KEY (Don't peek until you've tried!)
* ============================================================================
*
* Exercise 1:
* int myInt = 42;
* float myFloat = 3.14;
* char myChar = 'C';
* double myDouble = 2.71828;
*
* Exercise 3:
* int num = 50;
* int *ptr = #
* *ptr = 100;
*
* Exercise 4:
* int square(int n) { return n * n; }
* int cube(int n) { return n * n * n; }
* void checkEvenOdd(int n) {
* if (n % 2 == 0) printf("%d is even\n", n);
* else printf("%d is odd\n", n);
* }
*
* Exercise 7 (sumOfDigits):
* int sumOfDigits(int n) {
* if (n < 10) return n;
* return (n % 10) + sumOfDigits(n / 10);
* }
*
* ============================================================================
*/