c
exercises
exercises.c🔧c
/*
* ============================================================================
* DATA TYPES IN C - EXERCISES
* ============================================================================
* Complete these exercises to master C data types.
*
* Compile: gcc -Wall exercises.c -o exercises
* Run: ./exercises
* ============================================================================
*/
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <stdint.h>
#include <stdbool.h>
/*
* ============================================================================
* EXERCISE 1: Declare and Print Different Types
* ============================================================================
* Difficulty: Easy
*
* Task: Declare variables of each type and print them with correct specifiers.
*
* Expected Output:
* Integer: 42
* Float: 3.14
* Double: 3.14159265359
* Character: A
* Unsigned: 1000
*/
void exercise_1() {
printf("\n=== Exercise 1: Basic Types ===\n");
// TODO: Declare an int named 'myInt' with value 42
// TODO: Declare a float named 'myFloat' with value 3.14
// TODO: Declare a double named 'myDouble' with value 3.14159265359
// TODO: Declare a char named 'myChar' with value 'A'
// TODO: Declare an unsigned int named 'myUnsigned' with value 1000
// TODO: Print all variables with appropriate format specifiers
}
/*
* ============================================================================
* EXERCISE 2: Size of Types
* ============================================================================
* Difficulty: Easy
*
* Task: Use sizeof to determine and print the size of each type.
*
* Expected Output:
* char: X byte(s)
* short: X byte(s)
* int: X byte(s)
* long: X byte(s)
* long long: X byte(s)
* float: X byte(s)
* double: X byte(s)
*/
void exercise_2() {
printf("\n=== Exercise 2: Size of Types ===\n");
// TODO: Print the size of each type using sizeof
// Use %zu format specifier for sizeof results
}
/*
* ============================================================================
* EXERCISE 3: ASCII Values
* ============================================================================
* Difficulty: Easy
*
* Task:
* 1. Print the ASCII value of 'A', 'Z', 'a', 'z', '0', '9'
* 2. Print the character for ASCII values 65, 97, 48
*
* Expected Output:
* 'A' = 65
* 'Z' = 90
* 'a' = 97
* 'z' = 122
* '0' = 48
* '9' = 57
* ASCII 65 = 'A'
* ASCII 97 = 'a'
* ASCII 48 = '0'
*/
void exercise_3() {
printf("\n=== Exercise 3: ASCII Values ===\n");
// TODO: Print ASCII values of characters
// Hint: Use %d to print char as integer
// TODO: Print characters for given ASCII values
// Hint: Use %c to print int as character
}
/*
* ============================================================================
* EXERCISE 4: Number Bases
* ============================================================================
* Difficulty: Medium
*
* Task:
* 1. Declare an int using decimal (100)
* 2. Declare an int using octal (same value)
* 3. Declare an int using hexadecimal (same value)
* 4. Print all three values (they should be equal)
* 5. Print 255 in decimal, octal, and hex
*
* Expected Output:
* Decimal 100: 100
* Octal 0144: 100
* Hex 0x64: 100
*
* 255 in decimal: 255
* 255 in octal: 377
* 255 in hex: ff
*/
void exercise_4() {
printf("\n=== Exercise 4: Number Bases ===\n");
// TODO: Declare same value in different bases
// TODO: Print 255 in all bases
}
/*
* ============================================================================
* EXERCISE 5: Type Limits
* ============================================================================
* Difficulty: Medium
*
* Task: Print the minimum and maximum values for:
* - char
* - int
* - unsigned int
* - long long
*
* Use constants from limits.h
*
* Expected Output:
* char: -128 to 127
* int: -2147483648 to 2147483647
* unsigned int: 0 to 4294967295
* long long: -9223372036854775808 to 9223372036854775807
*/
void exercise_5() {
printf("\n=== Exercise 5: Type Limits ===\n");
// TODO: Print limits for each type using limits.h constants
// CHAR_MIN, CHAR_MAX, INT_MIN, INT_MAX, UINT_MAX, LLONG_MIN, LLONG_MAX
}
/*
* ============================================================================
* EXERCISE 6: Floating-Point Precision
* ============================================================================
* Difficulty: Medium
*
* Task:
* 1. Store 0.1 in a float and double
* 2. Multiply each by 10 and check if it equals 1.0
* 3. Demonstrate precision loss with a large number
*
* Expected Output:
* float: 0.1 * 10 = 1.000000 (but may not equal 1.0!)
* double: 0.1 * 10 = 1.000000
* Large number in float: loses precision
* Large number in double: accurate
*/
void exercise_6() {
printf("\n=== Exercise 6: Floating-Point Precision ===\n");
// TODO: Demonstrate precision differences between float and double
// TODO: Show precision loss with large number (e.g., 123456789)
}
/*
* ============================================================================
* EXERCISE 7: Type Conversion Traps
* ============================================================================
* Difficulty: Medium
*
* Task: Identify and fix the bugs in these expressions:
* 1. Integer division losing decimal part
* 2. Overflow when multiplying large numbers
* 3. Wrong format specifier
*
* Expected Output:
* 5/2 = 2.5 (not 2!)
* Large multiplication = correct result
* Proper format specifiers
*/
void exercise_7() {
printf("\n=== Exercise 7: Type Conversion Traps ===\n");
// Bug 1: Integer division
int a = 5, b = 2;
float wrong = a / b; // This gives 2.0, not 2.5
// TODO: Fix to get 2.5
float correct = 0; // Fix this
printf("Wrong: %f\n", wrong);
printf("Correct: %f\n", correct);
// Bug 2: Overflow
int x = 100000;
int overflow = x * x; // Overflow!
// TODO: Fix using appropriate types
long long safe = 0; // Fix this
printf("Overflow: %d\n", overflow);
printf("Safe: %lld\n", safe);
// Bug 3: Wrong format specifier
long long big = 9223372036854775807LL;
// printf("%d\n", big); // Wrong! Will print garbage
// TODO: Use correct format specifier
}
/*
* ============================================================================
* EXERCISE 8: Unsigned Behavior
* ============================================================================
* Difficulty: Medium
*
* Task:
* 1. Demonstrate unsigned overflow (wrapping)
* 2. Demonstrate unsigned underflow
* 3. Show why unsigned is useful for sizes
*
* Expected Output:
* 255 + 1 = 0 (for unsigned char)
* 0 - 1 = 255 (for unsigned char)
* Array size is always non-negative
*/
void exercise_8() {
printf("\n=== Exercise 8: Unsigned Behavior ===\n");
// TODO: Demonstrate unsigned char overflow
unsigned char uc = 255;
// What happens when you add 1?
// TODO: Demonstrate unsigned underflow
unsigned char zero = 0;
// What happens when you subtract 1?
// TODO: Use unsigned for array size
}
/*
* ============================================================================
* EXERCISE 9: Fixed-Width Types
* ============================================================================
* Difficulty: Medium
*
* Task: Use fixed-width types from stdint.h to:
* 1. Declare an 8-bit signed integer
* 2. Declare a 32-bit unsigned integer
* 3. Declare a 64-bit signed integer
* 4. Verify their sizes with sizeof
*
* Expected Output:
* int8_t size: 1 byte
* uint32_t size: 4 bytes
* int64_t size: 8 bytes
*/
void exercise_9() {
printf("\n=== Exercise 9: Fixed-Width Types ===\n");
// TODO: Declare fixed-width types and print their sizes
}
/*
* ============================================================================
* EXERCISE 10: Format Specifier Practice
* ============================================================================
* Difficulty: Easy
*
* Task: Print the number 42 in different formats:
* - Right-aligned in field of width 10
* - Left-aligned in field of width 10
* - Zero-padded in field of width 10
* - With explicit + sign
* - In hexadecimal with 0x prefix
*
* Expected Output:
* [ 42]
* [42 ]
* [0000000042]
* [+42]
* [0x2a]
*/
void exercise_10() {
printf("\n=== Exercise 10: Format Specifiers ===\n");
int num = 42;
// TODO: Print in different formats
// Right-aligned, width 10
// Left-aligned, width 10
// Zero-padded, width 10
// With + sign
// Hexadecimal with prefix
}
/*
* ============================================================================
* BONUS EXERCISE: Create a Type Information Function
* ============================================================================
* Difficulty: Hard
*
* Task: Create a function that prints comprehensive information about
* all basic data types including size, min, max, and format specifier.
*/
void bonus_exercise() {
printf("\n=== BONUS: Type Information ===\n");
// TODO: Create a comprehensive type information display
// Should show: Type name, Size, Min value, Max value, Format specifier
printf("TYPE SIZE MIN MAX FMT\n");
printf("────────────────────────────────────────────────────────────────\n");
// Example row:
// printf("int 4 -2147483648 2147483647 %%d\n");
// TODO: Add rows for: char, short, int, long, long long
// TODO: Add rows for: unsigned versions
// TODO: Add rows for: float, double
}
/*
* ============================================================================
* MAIN FUNCTION
* ============================================================================
*/
int main() {
printf("╔════════════════════════════════════════════════╗\n");
printf("║ DATA TYPES IN C - EXERCISES ║\n");
printf("║ Complete each exercise to practice ║\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();
// exercise_9();
// exercise_10();
// bonus_exercise();
printf("\n=== Uncomment exercises in main() to run them ===\n");
return 0;
}
/*
* ============================================================================
* ANSWER KEY
* ============================================================================
*
* Exercise 1:
* int myInt = 42;
* float myFloat = 3.14f;
* double myDouble = 3.14159265359;
* char myChar = 'A';
* unsigned int myUnsigned = 1000;
*
* Exercise 4 (number bases):
* int decimal = 100;
* int octal = 0144;
* int hex = 0x64;
*
* Exercise 7 (fix integer division):
* float correct = (float)a / b;
* // OR
* float correct = a / (float)b;
* // OR
* float correct = (float)a / (float)b;
*
* Exercise 7 (fix overflow):
* long long safe = (long long)x * x;
*
* Exercise 10 (format specifiers):
* printf("[%10d]\n", num); // Right-aligned
* printf("[%-10d]\n", num); // Left-aligned
* printf("[%010d]\n", num); // Zero-padded
* printf("[%+d]\n", num); // With sign
* printf("[0x%x]\n", num); // Hex with prefix
*
* ============================================================================
*/