├── 09_Sneklang └── notes │ └── sneklang.md ├── 05_Memory_Management └── notes │ └── memory_management.md ├── 08_Garbage_Collection └── notes │ └── garbage_collection.md ├── 07_Efficient_Programming └── notes │ └── efficient_programming.md ├── 06_Advanced_Data_Structures └── notes │ └── advanced_data_structures.md ├── 03_Introduction_to_Linux ├── exercises │ └── exercises.md ├── examples │ └── examples.md └── notes │ └── brief_introduction_of_linux_commands.md ├── 04_Basics_of_C ├── exercises │ └── exercises.md ├── examples │ └── examples.c └── notes │ └── basics_of_c.md ├── 02_Hardware_Software_Interaction ├── examples │ ├── examples.md │ └── examples.c ├── notes │ ├── hardware_software_interaction.c │ └── software_hardware_interaction.md └── exercises │ ├── practice_hardware_software.c │ └── exercices.md ├── 01_History_of_C ├── examples │ ├── practical_history.c │ └── history_examples.md ├── exercises │ ├── history_exercices.md │ └── explain_exercise.c └── notes │ └── history_of_C.md └── README.md /09_Sneklang/notes/sneklang.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /05_Memory_Management/notes/memory_management.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /08_Garbage_Collection/notes/garbage_collection.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /07_Efficient_Programming/notes/efficient_programming.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /06_Advanced_Data_Structures/notes/advanced_data_structures.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /03_Introduction_to_Linux/exercises/exercises.md: -------------------------------------------------------------------------------- 1 | # Exercises: Basic Linux Commands 2 | 3 | This file provides exercises to practice basic Linux commands. 4 | 5 | --- 6 | 7 | ## **1. Navigating the File System** 8 | 1. Use the `pwd` command to print the current directory. 9 | 2. Use the `ls` command to list files in the current directory. 10 | 3. Use the `cd` command to change to the home directory. 11 | 12 | --- 13 | 14 | ## **2. File and Directory Operations** 15 | 1. Create a file called `test.txt` using the `touch` command. 16 | 2. Create a directory called `test_dir` using the `mkdir` command. 17 | 3. Delete the file `test.txt` using the `rm` command. 18 | 19 | --- 20 | 21 | ## **3. Viewing and Editing Files** 22 | 1. Display the contents of a file using the `cat` command. 23 | 2. Edit a file using the `nano` command. 24 | 25 | --- 26 | 27 | ## **4. System Information** 28 | 1. Display system information using the `uname` command. 29 | 2. Display disk space usage using the `df` command. 30 | 31 | --- 32 | 33 | ## **5. Miscellaneous Commands** 34 | 1. View the manual for the `ls` command using the `man` command. 35 | 2. Clear the terminal screen using the `clear` command. 36 | -------------------------------------------------------------------------------- /04_Basics_of_C/exercises/exercises.md: -------------------------------------------------------------------------------- 1 | # Exercises: Basics of C Programming 2 | 3 | This file provides exercises to practice basic C programming concepts. 4 | 5 | --- 6 | 7 | ## **1. Hello World** 8 | 1. Write a C program to print "Hello, World!" to the console. 9 | 10 | --- 11 | 12 | ## **2. Variables and Data Types** 13 | 1. Declare an integer variable `age` and assign it a value of `30`. Print the value. 14 | 2. Declare a float variable `price` and assign it a value of `99.99`. Print the value. 15 | 3. Declare a character variable `grade` and assign it a value of `'B'`. Print the value. 16 | 17 | --- 18 | 19 | ## **3. Input and Output** 20 | 1. Write a program that asks the user to enter their name and prints "Hello, [Name]!". 21 | 22 | --- 23 | 24 | ## **4. If-Else Statement** 25 | 1. Write a program that checks if a number is positive, negative, or zero. 26 | 27 | --- 28 | 29 | ## **5. For Loop** 30 | 1. Write a program to print numbers from 1 to 10 using a `for` loop. 31 | 32 | --- 33 | 34 | ## **6. Functions** 35 | 1. Write a function `multiply` that takes two integers as parameters and returns their product. Call this function in `main`. 36 | 37 | --- 38 | 39 | ## **7. Arrays** 40 | 1. Declare an array of 5 integers and print all the elements using a `for` loop. 41 | 42 | --- 43 | 44 | ## **8. Pointers** 45 | 1. Declare an integer variable `num` and a pointer `ptr` that points to `num`. Print the value and address of `num` using the pointer. 46 | 47 | --- 48 | 49 | ## **9. Structures** 50 | 1. Define a structure `Student` with fields `name` (string) and `grade` (char). Create a variable of type `Student`, assign values to its fields, and print them. 51 | 52 | --- 53 | 54 | ## **10. File Handling** 55 | 1. Write a program to create a file `output.txt` and write "This is a test file." to it. 56 | -------------------------------------------------------------------------------- /03_Introduction_to_Linux/examples/examples.md: -------------------------------------------------------------------------------- 1 | # Examples: Basic Linux Commands 2 | 3 | This file provides examples of basic Linux commands to help you practice. 4 | 5 | --- 6 | 7 | ## **1. Navigating the File System** 8 | - Print the current directory: 9 | ```bash 10 | pwd 11 | 12 | List files in the current directory: 13 | bash 14 | Copy 15 | 16 | ls 17 | 18 | Change to the home directory: 19 | bash 20 | Copy 21 | 22 | cd ~ 23 | 24 | 2. File and Directory Operations 25 | 26 | Create a file: 27 | bash 28 | Copy 29 | 30 | touch example.txt 31 | 32 | Create a directory: 33 | bash 34 | Copy 35 | 36 | mkdir new_directory 37 | 38 | Delete a file: 39 | bash 40 | Copy 41 | 42 | rm example.txt 43 | 44 | 3. Viewing and Editing Files 45 | 46 | Display file content: 47 | bash 48 | Copy 49 | 50 | cat example.txt 51 | 52 | Edit a file: 53 | bash 54 | Copy 55 | 56 | nano example.txt 57 | 58 | 4. System Information 59 | 60 | Display system information: 61 | bash 62 | Copy 63 | 64 | uname -a 65 | 66 | Display disk space usage: 67 | bash 68 | Copy 69 | 70 | df -h 71 | 72 | 5. Miscellaneous Commands 73 | 74 | View the manual for a command: 75 | bash 76 | Copy 77 | 78 | man ls 79 | 80 | Clear the terminal screen: 81 | bash 82 | Copy 83 | 84 | clear 85 | 86 | Copy 87 | 88 | 89 | --- 90 | 91 | ### How to Use These Files: 92 | 1. **Navigate to the `exercises` Directory**: 93 | ```bash 94 | cd ~/introduction-to-c/03_Introduction_to_Linux/exercises 95 | 96 | Create the exercises.md File: 97 | bash 98 | Copy 99 | 100 | nano exercises.md 101 | -------------------------------------------------------------------------------- /02_Hardware_Software_Interaction/examples/examples.md: -------------------------------------------------------------------------------- 1 | # Examples: Hardware and Software Interaction 2 | 3 | This file provides examples and explanations of key concepts related to hardware and software interaction. 4 | 5 | --- 6 | 7 | ## **1. Binary Representation** 8 | Computers use **binary** (0s and 1s) to represent data and instructions. Each binary digit is called a **bit**, and a group of 8 bits is called a **byte**. 9 | 10 | ### Example: 11 | - The decimal number `5` is represented as `0101` in binary. 12 | - The decimal number `3` is represented as `0011` in binary. 13 | 14 | --- 15 | 16 | ## **2. Logic Gates** 17 | Logic gates are circuits made of transistors that perform basic logical operations. Here are the most common logic gates: 18 | 19 | ### **AND Gate** 20 | - Outputs `1` only if **all inputs are `1`**. 21 | - Example: `1 AND 1 = 1`, `1 AND 0 = 0`. 22 | 23 | ### **OR Gate** 24 | - Outputs `1` if **at least one input is `1`**. 25 | - Example: `1 OR 0 = 1`, `0 OR 0 = 0`. 26 | 27 | ### **NOT Gate** 28 | - Outputs the **opposite** of the input. 29 | - Example: `NOT 1 = 0`, `NOT 0 = 1`. 30 | 31 | ### **XOR Gate** 32 | - Outputs `1` if the inputs are **different**. 33 | - Example: `1 XOR 0 = 1`, `1 XOR 1 = 0`. 34 | 35 | --- 36 | 37 | ## **3. Binary Addition** 38 | Binary addition works similarly to decimal addition but uses only `0` and `1`. 39 | 40 | ### Example: 41 | - Adding `5` (`0101`) and `3` (`0011`) in binary:0101 (5)1000 (8) 42 | 43 | 44 | --- 45 | 46 | ## **4. Fetch-Decode-Execute Cycle** 47 | The CPU executes instructions by performing the following steps: 48 | 49 | 1. **Fetch**: The CPU fetches the next instruction from memory. 50 | 2. **Decode**: The CPU decodes the instruction to understand what operation to perform. 51 | 3. **Execute**: The CPU performs the operation (e.g., addition, subtraction). 52 | 4. **Writeback**: The CPU stores the result in memory or a register. 53 | 54 | ### Example: 55 | - Instruction: `1010` 56 | - Opcode: `10` (Operation: Multiply) 57 | - Operand: `10` (Data: 2) 58 | -------------------------------------------------------------------------------- /01_History_of_C/examples/practical_history.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Example 1: The First C Program 4 | void first_c_program() { 5 | printf("This is a simple C program, similar to the first C programs written in the 1970s.\n"); 6 | printf("C was developed by Dennis Ritchie at Bell Labs to build the Unix operating system.\n"); 7 | } 8 | 9 | // Example 2: Evolution of C Syntax 10 | void evolution_of_c_syntax() { 11 | int a = 5, b = 10; 12 | int sum = a + b; 13 | 14 | printf("In early C, code looked like this:\n"); 15 | printf("int a = 5, b = 10;\n"); 16 | printf("int sum = a + b;\n"); 17 | printf("Result: %d\n", sum); 18 | } 19 | 20 | // Example 3: Importance of the C Standard Library 21 | void c_standard_library() { 22 | printf("The C Standard Library provides essential functions like printf().\n"); 23 | printf("Without it, even simple tasks like printing to the console would be difficult.\n"); 24 | } 25 | 26 | // Example 4: C's Influence on Modern Languages 27 | void c_influence() { 28 | printf("C has influenced many modern programming languages, such as C++, Java, and Python.\n"); 29 | printf("For example, the 'printf' function in C inspired 'print' in Python.\n"); 30 | } 31 | 32 | // Example 5: Low-Level Memory Access 33 | void low_level_memory_access() { 34 | int x = 42; 35 | int *ptr = &x; 36 | 37 | printf("C allows direct memory access through pointers.\n"); 38 | printf("Value of x: %d\n", x); 39 | printf("Address of x: %p\n", (void*)ptr); 40 | } 41 | 42 | // Main Function to Run All Examples 43 | int main() { 44 | printf("=== Practical Examples: History of C ===\n\n"); 45 | 46 | first_c_program(); 47 | printf("\n"); 48 | 49 | evolution_of_c_syntax(); 50 | printf("\n"); 51 | 52 | c_standard_library(); 53 | printf("\n"); 54 | 55 | c_influence(); 56 | printf("\n"); 57 | 58 | low_level_memory_access(); 59 | printf("\n"); 60 | 61 | printf("=== End of Practical Examples ===\n"); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /01_History_of_C/exercises/history_exercices.md: -------------------------------------------------------------------------------- 1 | # Exercises: History of C 2 | 3 | ## Multiple Choice Questions 4 | 5 | 1. Who developed the C programming language? 6 | a) Ken Thompson 7 | b) Dennis Ritchie 8 | c) Martin Richards 9 | d) Bjarne Stroustrup 10 | 11 | **Answer**: b) Dennis Ritchie 12 | 13 | 2. Which language was the direct predecessor of C? 14 | a) BCPL 15 | b) B 16 | c) Java 17 | d) Python 18 | 19 | **Answer**: b) B 20 | 21 | 3. What was the primary purpose of creating C? 22 | a) Web development 23 | b) System programming 24 | c) Game development 25 | d) Data analysis 26 | 27 | **Answer**: b) System programming 28 | 29 | 4. Which operating system was rewritten in C, demonstrating its power and flexibility? 30 | a) Windows 31 | b) Linux 32 | c) UNIX 33 | d) macOS 34 | 35 | **Answer**: c) UNIX 36 | 37 | 5. What does `#include ` do in a C program? 38 | a) Includes the standard math library 39 | b) Includes the standard input/output library 40 | c) Defines a new data type 41 | d) Terminates the program 42 | 43 | **Answer**: b) Includes the standard input/output library 44 | 45 | --- 46 | 47 | ## True or False 48 | 49 | 6. C was developed in the 1980s. 50 | **Answer**: False (C was developed in the early 1970s.) 51 | 52 | 7. The B language was derived from BCPL. 53 | **Answer**: True 54 | 55 | 8. C introduced data types, which were absent in B. 56 | **Answer**: True 57 | 58 | 9. UNIX was originally written in assembly language. 59 | **Answer**: True 60 | 61 | 10. C is a high-level language with no low-level features. 62 | **Answer**: False (C has low-level features like pointers.) 63 | 64 | --- 65 | 66 | ## Short Answer Questions 67 | 68 | 11. What are the main differences between B and C? 69 | **Answer**: C introduced data types, standard libraries, and low-level features like pointers, which were absent in B. 70 | 71 | 12. Why was C used to rewrite UNIX? 72 | **Answer**: C made UNIX more portable and easier to maintain compared to assembly language. 73 | 74 | 13. What is the significance of `return 0;` in a C program? 75 | **Answer**: It indicates that the program executed successfully. 76 | 77 | 14. Name two modern programming languages influenced by C. 78 | **Answer**: C++, Java, Python, JavaScript (any two). 79 | 80 | 15. What role did Dennis Ritchie play in the development of C? 81 | **Answer**: Dennis Ritchie developed the C programming language at Bell Labs in the early 1970s. 82 | -------------------------------------------------------------------------------- /02_Hardware_Software_Interaction/examples/examples.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to demonstrate binary representation 4 | void binary_representation() { 5 | int num1 = 5, num2 = 3; 6 | printf("=== Binary Representation ===\n\n"); 7 | 8 | printf("Decimal: %d -> Binary: %04b\n", num1, num1); 9 | printf("Decimal: %d -> Binary: %04b\n", num2, num2); 10 | printf("\n"); 11 | } 12 | 13 | // Function to simulate logic gates 14 | void logic_gates() { 15 | int A = 1, B = 0; 16 | 17 | printf("=== Logic Gates ===\n\n"); 18 | 19 | // AND Gate 20 | printf("AND Gate: %d AND %d = %d\n", A, B, A && B); 21 | 22 | // OR Gate 23 | printf("OR Gate: %d OR %d = %d\n", A, B, A || B); 24 | 25 | // NOT Gate 26 | printf("NOT Gate: NOT %d = %d\n", A, !A); 27 | 28 | // XOR Gate 29 | printf("XOR Gate: %d XOR %d = %d\n", A, B, A ^ B); 30 | printf("\n"); 31 | } 32 | 33 | // Function to simulate binary addition 34 | void binary_addition() { 35 | int num1 = 5, num2 = 3; 36 | int sum = num1 + num2; 37 | 38 | printf("=== Binary Addition ===\n\n"); 39 | 40 | printf("Decimal: %d + %d = %d\n", num1, num2, sum); 41 | printf("Binary: %04b + %04b = %04b\n", num1, num2, sum); 42 | printf("\n"); 43 | } 44 | 45 | // Function to simulate the Fetch-Decode-Execute Cycle 46 | void fetch_decode_execute() { 47 | int instruction = 0b1010; // Example instruction (binary) 48 | int opcode = (instruction >> 2) & 0b11; // Extract opcode 49 | int operand = instruction & 0b11; // Extract operand 50 | 51 | printf("=== Fetch-Decode-Execute Cycle ===\n\n"); 52 | 53 | printf("Instruction: %04b\n", instruction); 54 | printf("Opcode: %02b (Operation to perform)\n", opcode); 55 | printf("Operand: %02b (Data to operate on)\n", operand); 56 | 57 | // Simulate execution 58 | switch (opcode) { 59 | case 0b00: 60 | printf("Operation: ADD\n"); 61 | break; 62 | case 0b01: 63 | printf("Operation: SUB\n"); 64 | break; 65 | case 0b10: 66 | printf("Operation: MUL\n"); 67 | break; 68 | case 0b11: 69 | printf("Operation: DIV\n"); 70 | break; 71 | default: 72 | printf("Operation: UNKNOWN\n"); 73 | break; 74 | } 75 | printf("\n"); 76 | } 77 | 78 | // Main Function to Run All Examples 79 | int main() { 80 | printf("=== Examples: Hardware and Software Interaction ===\n\n"); 81 | 82 | binary_representation(); 83 | logic_gates(); 84 | binary_addition(); 85 | fetch_decode_execute(); 86 | 87 | printf("=== End of Examples ===\n"); 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /02_Hardware_Software_Interaction/notes/hardware_software_interaction.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to demonstrate binary representation 4 | void binary_representation() { 5 | int num1 = 5, num2 = 3; 6 | printf("=== Binary Representation ===\n\n"); 7 | 8 | printf("Decimal: %d -> Binary: %04b\n", num1, num1); 9 | printf("Decimal: %d -> Binary: %04b\n", num2, num2); 10 | printf("\n"); 11 | } 12 | 13 | // Function to simulate logic gates 14 | void logic_gates() { 15 | int A = 1, B = 0; 16 | 17 | printf("=== Logic Gates ===\n\n"); 18 | 19 | // AND Gate 20 | printf("AND Gate: %d AND %d = %d\n", A, B, A && B); 21 | 22 | // OR Gate 23 | printf("OR Gate: %d OR %d = %d\n", A, B, A || B); 24 | 25 | // NOT Gate 26 | printf("NOT Gate: NOT %d = %d\n", A, !A); 27 | 28 | // XOR Gate 29 | printf("XOR Gate: %d XOR %d = %d\n", A, B, A ^ B); 30 | printf("\n"); 31 | } 32 | 33 | // Function to simulate binary addition 34 | void binary_addition() { 35 | int num1 = 5, num2 = 3; 36 | int sum = num1 + num2; 37 | 38 | printf("=== Binary Addition ===\n\n"); 39 | 40 | printf("Decimal: %d + %d = %d\n", num1, num2, sum); 41 | printf("Binary: %04b + %04b = %04b\n", num1, num2, sum); 42 | printf("\n"); 43 | } 44 | 45 | // Function to simulate the Fetch-Decode-Execute Cycle 46 | void fetch_decode_execute() { 47 | int instruction = 0b1010; // Example instruction (binary) 48 | int opcode = (instruction >> 2) & 0b11; // Extract opcode 49 | int operand = instruction & 0b11; // Extract operand 50 | 51 | printf("=== Fetch-Decode-Execute Cycle ===\n\n"); 52 | 53 | printf("Instruction: %04b\n", instruction); 54 | printf("Opcode: %02b (Operation to perform)\n", opcode); 55 | printf("Operand: %02b (Data to operate on)\n", operand); 56 | 57 | // Simulate execution 58 | switch (opcode) { 59 | case 0b00: 60 | printf("Operation: ADD\n"); 61 | break; 62 | case 0b01: 63 | printf("Operation: SUB\n"); 64 | break; 65 | case 0b10: 66 | printf("Operation: MUL\n"); 67 | break; 68 | case 0b11: 69 | printf("Operation: DIV\n"); 70 | break; 71 | default: 72 | printf("Operation: UNKNOWN\n"); 73 | break; 74 | } 75 | printf("\n"); 76 | } 77 | 78 | // Main Function to Run All Examples 79 | int main() { 80 | printf("=== Hardware and Software Interaction ===\n\n"); 81 | 82 | binary_representation(); 83 | logic_gates(); 84 | binary_addition(); 85 | fetch_decode_execute(); 86 | 87 | printf("=== End of Examples ===\n"); 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /02_Hardware_Software_Interaction/exercises/practice_hardware_software.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to practice binary representation 4 | void practice_binary_representation() { 5 | int num; 6 | 7 | printf("=== Practice: Binary Representation ===\n\n"); 8 | 9 | printf("Enter a decimal number (0-15): "); 10 | scanf("%d", &num); 11 | 12 | printf("Decimal: %d -> Binary: %04b\n", num, num); 13 | printf("\n"); 14 | } 15 | 16 | // Function to practice logic gates 17 | void practice_logic_gates() { 18 | int A, B; 19 | 20 | printf("=== Practice: Logic Gates ===\n\n"); 21 | 22 | printf("Enter two binary values (0 or 1) for A and B:\n"); 23 | printf("A: "); 24 | scanf("%d", &A); 25 | printf("B: "); 26 | scanf("%d", &B); 27 | 28 | printf("AND Gate: %d AND %d = %d\n", A, B, A && B); 29 | printf("OR Gate: %d OR %d = %d\n", A, B, A || B); 30 | printf("NOT Gate: NOT %d = %d\n", A, !A); 31 | printf("XOR Gate: %d XOR %d = %d\n", A, B, A ^ B); 32 | printf("\n"); 33 | } 34 | 35 | // Function to practice binary addition 36 | void practice_binary_addition() { 37 | int num1, num2; 38 | 39 | printf("=== Practice: Binary Addition ===\n\n"); 40 | 41 | printf("Enter two decimal numbers (0-15):\n"); 42 | printf("Number 1: "); 43 | scanf("%d", &num1); 44 | printf("Number 2: "); 45 | scanf("%d", &num2); 46 | 47 | int sum = num1 + num2; 48 | printf("Decimal: %d + %d = %d\n", num1, num2, sum); 49 | printf("Binary: %04b + %04b = %04b\n", num1, num2, sum); 50 | printf("\n"); 51 | } 52 | 53 | // Function to practice Fetch-Decode-Execute Cycle 54 | void practice_fetch_decode_execute() { 55 | int instruction; 56 | 57 | printf("=== Practice: Fetch-Decode-Execute Cycle ===\n\n"); 58 | 59 | printf("Enter a 4-bit binary instruction (e.g., 1010): "); 60 | scanf("%d", &instruction); 61 | 62 | int opcode = (instruction >> 2) & 0b11; // Extract opcode 63 | int operand = instruction & 0b11; // Extract operand 64 | 65 | printf("Instruction: %04b\n", instruction); 66 | printf("Opcode: %02b (Operation to perform)\n", opcode); 67 | printf("Operand: %02b (Data to operate on)\n", operand); 68 | 69 | // Simulate execution 70 | switch (opcode) { 71 | case 0b00: 72 | printf("Operation: ADD\n"); 73 | break; 74 | case 0b01: 75 | printf("Operation: SUB\n"); 76 | break; 77 | case 0b10: 78 | printf("Operation: MUL\n"); 79 | break; 80 | case 0b11: 81 | printf("Operation: DIV\n"); 82 | break; 83 | default: 84 | printf("Operation: UNKNOWN\n"); 85 | break; 86 | } 87 | printf("\n"); 88 | } 89 | 90 | // Main Function to Run All Practice Exercises 91 | int main() { 92 | printf("=== Practice: Hardware and Software Interaction ===\n\n"); 93 | 94 | practice_binary_representation(); 95 | practice_logic_gates(); 96 | practice_binary_addition(); 97 | practice_fetch_decode_execute(); 98 | 99 | printf("=== End of Practice Exercises ===\n"); 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /04_Basics_of_C/examples/examples.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Example 1: Hello World 4 | void hello_world() { 5 | printf("=== Hello World ===\n"); 6 | printf("Hello, World!\n"); 7 | printf("\n"); 8 | } 9 | 10 | // Example 2: Variables and Data Types 11 | void variables_and_data_types() { 12 | printf("=== Variables and Data Types ===\n"); 13 | int age = 25; 14 | float price = 19.99; 15 | char grade = 'A'; 16 | 17 | printf("Age: %d\n", age); 18 | printf("Price: %.2f\n", price); 19 | printf("Grade: %c\n", grade); 20 | printf("\n"); 21 | } 22 | 23 | // Example 3: Input and Output 24 | void input_and_output() { 25 | printf("=== Input and Output ===\n"); 26 | int num; 27 | printf("Enter a number: "); 28 | scanf("%d", &num); 29 | printf("You entered: %d\n", num); 30 | printf("\n"); 31 | } 32 | 33 | // Example 4: If-Else Statement 34 | void if_else_statement() { 35 | printf("=== If-Else Statement ===\n"); 36 | int num = 10; 37 | if (num > 0) { 38 | printf("Positive number\n"); 39 | } else { 40 | printf("Negative number\n"); 41 | } 42 | printf("\n"); 43 | } 44 | 45 | // Example 5: For Loop 46 | void for_loop() { 47 | printf("=== For Loop ===\n"); 48 | for (int i = 1; i <= 5; i++) { 49 | printf("%d\n", i); 50 | } 51 | printf("\n"); 52 | } 53 | 54 | // Example 6: Functions 55 | int add(int a, int b) { 56 | return a + b; 57 | } 58 | 59 | void functions_example() { 60 | printf("=== Functions ===\n"); 61 | int result = add(5, 3); 62 | printf("Result: %d\n", result); 63 | printf("\n"); 64 | } 65 | 66 | // Example 7: Arrays 67 | void arrays_example() { 68 | printf("=== Arrays ===\n"); 69 | int numbers[5] = {1, 2, 3, 4, 5}; 70 | for (int i = 0; i < 5; i++) { 71 | printf("%d\n", numbers[i]); 72 | } 73 | printf("\n"); 74 | } 75 | 76 | // Example 8: Pointers 77 | void pointers_example() { 78 | printf("=== Pointers ===\n"); 79 | int num = 10; 80 | int *ptr = # 81 | printf("Value: %d, Address: %p\n", *ptr, ptr); 82 | printf("\n"); 83 | } 84 | 85 | // Example 9: Structures 86 | struct Person { 87 | char name[50]; 88 | int age; 89 | }; 90 | 91 | void structures_example() { 92 | printf("=== Structures ===\n"); 93 | struct Person p1 = {"John", 25}; 94 | printf("Name: %s, Age: %d\n", p1.name, p1.age); 95 | printf("\n"); 96 | } 97 | 98 | // Example 10: File Handling 99 | void file_handling_example() { 100 | printf("=== File Handling ===\n"); 101 | FILE *file = fopen("example.txt", "w"); 102 | if (file != NULL) { 103 | fprintf(file, "Hello, File!\n"); 104 | fclose(file); 105 | printf("File created and written successfully.\n"); 106 | } else { 107 | printf("Failed to create file.\n"); 108 | } 109 | printf("\n"); 110 | } 111 | 112 | // Main Function to Run All Examples 113 | int main() { 114 | hello_world(); 115 | variables_and_data_types(); 116 | input_and_output(); 117 | if_else_statement(); 118 | for_loop(); 119 | functions_example(); 120 | arrays_example(); 121 | pointers_example(); 122 | structures_example(); 123 | file_handling_example(); 124 | 125 | return 0; 126 | } 127 | 128 | 129 | -------------------------------------------------------------------------------- /01_History_of_C/exercises/explain_exercise.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to explain the history of C 4 | void explain_history_of_c() { 5 | printf("=== History of C ===\n\n"); 6 | 7 | printf("1. Who developed the C programming language?\n"); 8 | printf(" Answer: Dennis Ritchie developed C at Bell Labs in the early 1970s.\n\n"); 9 | 10 | printf("2. Which language was the direct predecessor of C?\n"); 11 | printf(" Answer: The B language, which was derived from BCPL.\n\n"); 12 | 13 | printf("3. What was the primary purpose of creating C?\n"); 14 | printf(" Answer: C was created for system programming, particularly to rewrite UNIX.\n\n"); 15 | 16 | printf("4. Which operating system was rewritten in C?\n"); 17 | printf(" Answer: UNIX was rewritten in C, demonstrating its power and flexibility.\n\n"); 18 | 19 | printf("5. What does `#include ` do in a C program?\n"); 20 | printf(" Answer: It includes the standard input/output library, which provides functions like `printf` and `scanf`.\n\n"); 21 | } 22 | 23 | // Function to explain key features of C 24 | void explain_key_features_of_c() { 25 | printf("=== Key Features of C ===\n\n"); 26 | 27 | printf("6. C was developed in the 1980s. (True/False)\n"); 28 | printf(" Answer: False. C was developed in the early 1970s.\n\n"); 29 | 30 | printf("7. The B language was derived from BCPL. (True/False)\n"); 31 | printf(" Answer: True.\n\n"); 32 | 33 | printf("8. C introduced data types, which were absent in B. (True/False)\n"); 34 | printf(" Answer: True.\n\n"); 35 | 36 | printf("9. UNIX was originally written in assembly language. (True/False)\n"); 37 | printf(" Answer: True.\n\n"); 38 | 39 | printf("10. C is a high-level language with no low-level features. (True/False)\n"); 40 | printf(" Answer: False. C has low-level features like pointers.\n\n"); 41 | } 42 | 43 | // Function to demonstrate practical examples 44 | void demonstrate_practical_examples() { 45 | printf("=== Practical Examples ===\n\n"); 46 | 47 | printf("11. What are the main differences between B and C?\n"); 48 | printf(" Answer: C introduced data types, standard libraries, and low-level features like pointers, which were absent in B.\n\n"); 49 | 50 | printf("12. Why was C used to rewrite UNIX?\n"); 51 | printf(" Answer: C made UNIX more portable and easier to maintain compared to assembly language.\n\n"); 52 | 53 | printf("13. What is the significance of `return 0;` in a C program?\n"); 54 | printf(" Answer: It indicates that the program executed successfully.\n\n"); 55 | 56 | printf("14. Name two modern programming languages influenced by C.\n"); 57 | printf(" Answer: C++, Java, Python, JavaScript (any two).\n\n"); 58 | 59 | printf("15. What role did Dennis Ritchie play in the development of C?\n"); 60 | printf(" Answer: Dennis Ritchie developed the C programming language at Bell Labs in the early 1970s.\n\n"); 61 | } 62 | 63 | // Main Function to Run All Explanations 64 | int main() { 65 | printf("=== Explaining the Concepts in Exercises ===\n\n"); 66 | 67 | explain_history_of_c(); 68 | printf("\n"); 69 | 70 | explain_key_features_of_c(); 71 | printf("\n"); 72 | 73 | demonstrate_practical_examples(); 74 | printf("\n"); 75 | 76 | printf("=== End of Explanations ===\n"); 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /02_Hardware_Software_Interaction/notes/software_hardware_interaction.md: -------------------------------------------------------------------------------- 1 | # Hardware and Software Interaction 2 | 3 | ## **The Building Blocks of Computing** 4 | At the heart of every computer are **transistors**, tiny electronic switches that control the flow of electricity. By combining transistors into **logic gates**, and logic gates into more complex circuits, computers can perform calculations, store data, and execute instructions. 5 | 6 | --- 7 | 8 | ## **Transistors: The Electronic Switches** 9 | A transistor is a semiconductor device that acts like a **switch**. It has three parts: 10 | 1. **Collector**: Where current enters. 11 | 2. **Emitter**: Where current exits. 12 | 3. **Base**: Controls the flow of current between the collector and emitter. 13 | 14 | ### **How Transistors Work** 15 | - When a small voltage is applied to the **base**, current flows from the **collector** to the **emitter** (the switch is "on"). 16 | - When no voltage is applied to the base, current stops flowing (the switch is "off"). 17 | - This on/off behavior is the foundation of **binary computation**. 18 | 19 | ### **Transistors vs. Mechanical Switches** 20 | - A transistor is like a **light switch**, but instead of being flipped by hand, it’s controlled by an electrical signal. 21 | - Transistors are incredibly fast and can switch billions of times per second. 22 | 23 | --- 24 | 25 | ## **Binary: The Language of Computers** 26 | Computers understand only **binary** (0s and 1s). Each binary digit is called a **bit**, and a group of 8 bits is called a **byte**. 27 | 28 | ### **How Binary Works** 29 | - **0**: Represents "off" or "false." 30 | - **1**: Represents "on" or "true." 31 | - Binary numbers are used to represent data and instructions. For example: 32 | - The number `5` in binary is `0101`. 33 | - The letter `A` in ASCII is `01000001`. 34 | 35 | --- 36 | 37 | ## **Logic Gates: Combining Transistors** 38 | Logic gates are circuits made of transistors that perform basic logical operations. Here are the most common logic gates: 39 | 40 | ### **1. AND Gate** 41 | - Outputs `1` only if **all inputs are `1`**. 42 | - Example: `1 AND 1 = 1`, `1 AND 0 = 0`. 43 | 44 | ### **2. OR Gate** 45 | - Outputs `1` if **at least one input is `1`**. 46 | - Example: `1 OR 0 = 1`, `0 OR 0 = 0`. 47 | 48 | ### **3. NOT Gate** 49 | - Outputs the **opposite** of the input. 50 | - Example: `NOT 1 = 0`, `NOT 0 = 1`. 51 | 52 | ### **4. XOR Gate** 53 | - Outputs `1` if the inputs are **different**. 54 | - Example: `1 XOR 0 = 1`, `1 XOR 1 = 0`. 55 | 56 | --- 57 | 58 | ## **From Logic Gates to Complex Circuits** 59 | By combining logic gates, we can build more complex circuits: 60 | 61 | ### **1. Half Adder** 62 | - Adds two binary digits and produces a sum and a carry. 63 | - Built using an XOR gate (for the sum) and an AND gate (for the carry). 64 | 65 | ### **2. Full Adder** 66 | - Adds three binary digits (two inputs and a carry-in) and produces a sum and a carry-out. 67 | - Built using two half adders and an OR gate. 68 | 69 | ### **3. Flip-Flop** 70 | - A circuit that stores one bit of data (0 or 1). 71 | - Used to build memory and registers. 72 | 73 | --- 74 | 75 | ## **The CPU: Fetch, Decode, Execute** 76 | The **Central Processing Unit (CPU)** is the brain of the computer. It executes instructions stored in memory by performing the following steps: 77 | 78 | 1. **Fetch**: The CPU fetches the next instruction from memory. 79 | 2. **Decode**: The CPU decodes the instruction to understand what operation to perform. 80 | 3. **Execute**: The CPU performs the operation (e.g., addition, subtraction). 81 | 4. **Writeback**: The CPU stores the result in memory or a register. 82 | 83 | This cycle is called the **Fetch-Decode-Execute Cycle**. 84 | 85 | --- 86 | 87 | ## **Example: Adding Two Numbers** 88 | Here’s how a computer adds two numbers using binary and logic gates: 89 | 90 | ### **Step 1: Represent Numbers in Binary** 91 | - `5` in binary: `0101` 92 | - `3` in binary: `0011` 93 | 94 | ### **Step 2: Perform Binary Addition** 95 | -------------------------------------------------------------------------------- /01_History_of_C/notes/history_of_C.md: -------------------------------------------------------------------------------- 1 | # History of the C Programming Language 2 | 3 | ## **Genesis of C** 4 | The C programming language was developed in the early 1970s by **Dennis Ritchie** at **Bell Labs**. It was created as a successor to the **B language**, which was itself derived from **BCPL** (Basic Combined Programming Language). C was designed to overcome the limitations of B and to provide a more powerful and flexible language for system programming. 5 | 6 | --- 7 | 8 | ## **What Came Before C?** 9 | ### **1. BCPL (Basic Combined Programming Language)**: 10 | - Developed by **Martin Richards** in the mid-1960s. 11 | - A low-level language used for writing compilers and operating systems. 12 | - Lacked data types and was too simplistic for complex programming tasks. 13 | 14 | ### **2. B Language**: 15 | - Developed by **Ken Thompson** at Bell Labs in the late 1960s. 16 | - A stripped-down version of BCPL, designed for the first UNIX operating system. 17 | - Still lacked data types and was not suitable for hardware-specific programming. 18 | 19 | --- 20 | 21 | ## **Major Innovations in C** 22 | C introduced several groundbreaking features that made it a revolutionary language: 23 | 24 | ### **1. Data Types**: 25 | - C introduced **data types** (e.g., `int`, `char`, `float`), allowing programmers to define variables with specific sizes and behaviors. 26 | - This made programs more efficient and easier to understand. 27 | 28 | ### **2. Structured Programming**: 29 | - C supported **control structures** like `if`, `else`, `for`, and `while`, enabling programmers to write modular and readable code. 30 | 31 | ### **3. Portability**: 32 | - C was designed to be **hardware-independent**. Programs written in C could be compiled and run on different systems with minimal changes. 33 | - This was a significant improvement over assembly language, which was tied to specific hardware. 34 | 35 | ### **4. Standard Libraries**: 36 | - C introduced **standard libraries** (e.g., `stdio.h`, `stdlib.h`), providing pre-built functions for common tasks like input/output and memory management. 37 | - For example, `#include ` allowed programmers to use functions like `printf` and `scanf` without having to write them from scratch. 38 | 39 | ### **5. Low-Level Access**: 40 | - C provided **pointers**, giving programmers direct access to memory addresses. 41 | - This made C ideal for system programming, where low-level hardware interaction is required. 42 | 43 | --- 44 | 45 | ## **C and UNIX** 46 | One of the most significant milestones in C’s history was its use to rewrite the **UNIX operating system**: 47 | - Originally, UNIX was written in assembly language, which was difficult to maintain and port to other systems. 48 | - In 1973, UNIX was rewritten in C, making it more portable and easier to modify. 49 | - This demonstrated C’s power and flexibility, leading to its widespread adoption. 50 | 51 | --- 52 | 53 | ## **Added Value of C Programming** 54 | C brought several key advantages to the programming world: 55 | 56 | ### **1. Efficiency**: 57 | - C programs are compiled directly into machine code, making them fast and efficient. 58 | - This was a major improvement over interpreted languages like B. 59 | 60 | ### **2. Simplicity**: 61 | - C’s syntax is concise and easy to learn, making it accessible to both beginners and experienced programmers. 62 | - Features like `return 0;` (indicating successful program execution) simplified error handling. 63 | 64 | ### **3. Flexibility**: 65 | - C can be used for a wide range of applications, from system programming to application development. 66 | - Its low-level features allow programmers to fine-tune performance and memory usage. 67 | 68 | ### **4. Influence on Other Languages**: 69 | - C served as the foundation for many modern programming languages, including **C++**, **Java**, **Python**, and **JavaScript**. 70 | - Concepts like data types, control structures, and libraries were adopted and expanded in these languages. 71 | 72 | --- 73 | 74 | ## **Key Features of C** 75 | Here are some of the features that made C stand out: 76 | 77 | ### **1. `#include `**: 78 | - This directive allows programmers to include the **standard input/output library**, providing functions like `printf` and `scanf`. 79 | - It eliminated the need to write basic I/O functions from scratch, saving time and effort. 80 | 81 | ### **2. `return 0;`**: 82 | - In C, the `main` function returns an integer value to indicate the program’s exit status. 83 | - `return 0;` signifies successful execution, while non-zero values indicate errors. 84 | - This simple convention made error handling more consistent and predictable. 85 | 86 | ### **3. Pointers**: 87 | - Pointers allow direct manipulation of memory addresses, enabling efficient data structures and hardware interaction. 88 | - This feature made C a favorite for system programming and embedded systems. 89 | 90 | ### **4. Portability**: 91 | - C’s hardware-independent design allowed programs to be compiled and run on different systems with minimal changes. 92 | - This was a major advantage over assembly language, which was tied to specific hardware. 93 | 94 | --- 95 | 96 | ## **Conclusion** 97 | The C programming language revolutionized the world of computing by combining **efficiency**, **simplicity**, and **flexibility**. Its innovations, such as data types, standard libraries, and low-level access, made it a powerful tool for system programming and beyond. By rewriting UNIX in C, Dennis Ritchie demonstrated the language’s potential, paving the way for its widespread adoption and influence on modern programming languages. 98 | 99 | --- 100 | 101 | ### **References** 102 | 1. Kernighan, B. W., & Ritchie, D. M. (1988). *The C Programming Language* (2nd ed.). Prentice Hall. 103 | 2. Ritchie, D. M. (1993). *The Development of the C Language*. ACM SIGPLAN Notices, 28(3), 201–208. 104 | 3. Wikipedia. (n.d.). *C (programming language)*. Retrieved from [https://en.wikipedia.org/wiki/C_(programming_language)](https://en.wikipedia.org/wiki/C_(programming_language)) 105 | -------------------------------------------------------------------------------- /02_Hardware_Software_Interaction/exercises/exercices.md: -------------------------------------------------------------------------------- 1 | # Exercises: Hardware and Software Interaction 2 | 3 | Welcome to the **Hardware and Software Interaction** exercises! This directory contains a series of hands-on exercises designed to help you understand how software interacts with hardware. These exercises are foundational for mastering **memory management**, **efficiency**, and **building practical tools** like garbage collectors and the **Sneklang** programming language. 4 | 5 | --- 6 | 7 | ## Objectives 8 | 9 | By completing these exercises, you will: 10 | 1. **Understand the Compilation Process**: Learn how source code is transformed into an executable program. 11 | 2. **Explore Memory Layout**: Discover how a C program is organized in memory. 12 | 3. **Write Assembly Programs**: Gain insight into how high-level code translates to machine instructions. 13 | 4. **Use System Calls**: Learn how programs interact with the operating system and hardware. 14 | 5. **Simulate a CPU**: Build a simple CPU simulator to understand how hardware executes instructions. 15 | 6. **Analyze Binary Files**: Learn how to inspect binary files to understand how programs are stored and executed. 16 | 17 | --- 18 | 19 | ## Exercises 20 | 21 | ### **1. Understanding the Compilation Process** 22 | **Objective**: Learn how source code is transformed into an executable program and how it interacts with hardware. 23 | 24 | - Write a simple C program (`hello.c`) that prints "Hello, World!" to the console. 25 | - Use the `gcc` compiler to compile the program into an executable. 26 | - Investigate the intermediate files generated during compilation. 27 | - Answer questions about the purpose of each step in the compilation process and how the compiler interacts with the hardware. 28 | 29 | --- 30 | 31 | ### **2. Memory Layout of a C Program** 32 | **Objective**: Understand how a C program is organized in memory. 33 | 34 | - Write a C program (`memory_layout.c`) that includes global, static, local, and dynamically allocated variables. 35 | - Print the memory addresses of each variable. 36 | - Answer questions about where each variable is stored and how the memory layout affects program performance. 37 | 38 | --- 39 | 40 | ### **3. Writing a Simple Assembler Program** 41 | **Objective**: Gain insight into how high-level code translates to machine instructions. 42 | 43 | - Write a simple assembly program (`add.s`) that adds two numbers and stores the result in a register. 44 | - Assemble and link the program using `gcc`. 45 | - Compare the assembly code with equivalent C code. 46 | - Answer questions about how the CPU executes assembly instructions and the role of registers in computation. 47 | 48 | --- 49 | 50 | ### **4. Exploring System Calls** 51 | **Objective**: Learn how programs interact with the operating system and hardware. 52 | 53 | - Write a C program (`syscall.c`) that uses the `write` system call to print a message to the console. 54 | - Use the `strace` tool to trace the system calls made by the program. 55 | - Answer questions about the role of system calls in software-hardware interaction and how the operating system facilitates communication between programs and hardware. 56 | 57 | --- 58 | 59 | ### **5. Simulating a CPU** 60 | **Objective**: Build a simple CPU simulator to understand how hardware executes instructions. 61 | 62 | - Write a C program (`cpu_simulator.c`) that simulates a basic CPU with registers, memory space, and support for basic instructions. 63 | - Write a simple program in your custom assembly language and execute it using the simulator. 64 | - Answer questions about how the CPU fetches, decodes, and executes instructions and the limitations of your simulator compared to a real CPU. 65 | 66 | --- 67 | 68 | ### **6. Analyzing Binary Files** 69 | **Objective**: Learn how to inspect binary files to understand how programs are stored and executed. 70 | 71 | - Compile a simple C program (`binary.c`) into an executable. 72 | - Use the `objdump` tool to disassemble the executable. 73 | - Analyze the output and identify different sections of the program. 74 | - Answer questions about how different sections of a program are stored in memory and how the operating system loads and executes a binary file. 75 | 76 | --- 77 | 78 | ## How to Use These Exercises 79 | 80 | 1. **Navigate to the Exercises Directory**: 81 | ```bash 82 | cd ~/introduction-to-c/02_Hardware_Software_Interaction/exercises 83 | 84 | Choose an Exercise: 85 | 86 | Each exercise is documented in a separate Markdown file (e.g., exercise1_compilation_process.md). 87 | 88 | Open the file to view the instructions and questions. 89 | 90 | Complete the Exercises: 91 | 92 | Follow the instructions to write, compile, and run the programs. 93 | 94 | Answer the questions to reinforce your understanding. 95 | 96 | Check Your Work: 97 | 98 | Compare your solutions with the provided examples and notes. 99 | 100 | Use tools like gcc, strace, and objdump to verify your work. 101 | 102 | Contribution Guidelines 103 | 104 | If you have suggestions for improving these exercises or want to add new ones, feel free to contribute: 105 | 106 | Fork the repository. 107 | 108 | Create a new branch for your changes. 109 | 110 | Submit a pull request with a detailed description of your changes. 111 | 112 | Next Steps 113 | 114 | After completing these exercises, you’ll have a solid understanding of how software interacts with hardware. This knowledge will prepare you for the next phase of the course, where you’ll dive deeper into memory management, efficiency, and building practical tools. 115 | 116 | Happy coding! 🚀 117 | 118 | 119 | --- 120 | 121 | ### Steps to Add This to Your Repository: 122 | 1. Navigate to the `exercises` directory: 123 | ```bash 124 | cd ~/introduction-to-c/02_Hardware_Software_Interaction/exercises 125 | 126 | 127 | 2. Open nano to create/edit the README.md file: 128 | 129 | nano README.md 130 | 3. Paste the content above into the nano editor. 131 | 132 | Save and exit: 133 | 134 | Press Ctrl+O to save. 135 | 136 | Press Enter to confirm the filename. 137 | 138 | Press Ctrl+X to exit. 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction to C Programming: Mastering Memory and Building Tools 2 | 3 | Welcome to the **Introduction to C Programming** course! This repository contains comprehensive notes, exercises, and resources to help you learn C programming from scratch, with a focus on **memory management**, **efficiency**, and **building practical tools**. By the end of this course, you’ll be able to: 4 | 1. Understand how and where programs store data. 5 | 2. Write efficient programs in a low-level language. 6 | 3. Build your own garbage collectors. 7 | 4. Contribute to the development of **Sneklang**, a simple programming language. 8 | 9 | --- 10 | 11 | ## Topics Covered 12 | 13 | ### **1. Basics of C Programming** 14 | - **History of C**: Origins and importance in modern programming. 15 | - **Software and Hardware Interaction**: How computers execute code. 16 | - **Logic Gates and Binary**: Basics of binary, hexadecimal, and logic gates. 17 | - **Beyond IDEs: Mastering the Terminal**: Using the terminal to write, compile, and run C programs. 18 | 19 | ### **2. Memory Management in C** 20 | - **Variables and Data Types**: Storing data in memory. 21 | - **Pointers**: Understanding memory addresses. 22 | - **Dynamic Memory Allocation**: `malloc`, `calloc`, `realloc`, and `free`. 23 | - **Stack vs. Heap**: Differences and use cases. 24 | 25 | ### **3. Advanced Data Structures** 26 | - **Structs**: Grouping related data. 27 | - **Enums**: Defining sets of named constants. 28 | - **Unions**: Sharing memory between different data types. 29 | - **Macros**: Using `#define` for code efficiency. 30 | 31 | ### **4. Building Efficient Programs** 32 | - **Memory Layout of a C Program**: Text, data, BSS, heap, and stack segments. 33 | - **Optimizing Code**: Reducing memory usage and improving performance. 34 | - **Debugging Memory Issues**: Using tools like `valgrind`. 35 | 36 | ### **5. Garbage Collection** 37 | - **Introduction to Garbage Collection**: Why it’s needed and how it works. 38 | - **Reference Counting Garbage Collector**: Building a simple garbage collector. 39 | - **Mark and Sweep Garbage Collector**: Building a more advanced garbage collector. 40 | - **Comparing with Go and Java**: Understanding how modern languages handle garbage collection. 41 | 42 | ### **6. Building Sneklang** 43 | - **Introduction to Sneklang**: A simple programming language built in C. 44 | - **Lexical Analysis**: Tokenizing input. 45 | - **Parsing**: Building an abstract syntax tree (AST). 46 | - **Memory Management in Sneklang**: Integrating a garbage collector. 47 | 48 | --- 49 | 50 | ## Course Roadmap 51 | 52 | ### **Phase 1: Foundations of C Programming** 53 | - Learn the basics of C syntax, control flow, and functions. 54 | - Understand how programs interact with memory using variables, pointers, and dynamic memory allocation. 55 | - Practice writing and running C programs using the terminal. 56 | 57 | ### **Phase 2: Advanced Memory Management** 58 | - Dive deeper into memory management with structs, enums, unions, and macros. 59 | - Learn how to optimize programs for efficiency and debug memory issues. 60 | 61 | ### **Phase 3: Building Garbage Collectors** 62 | - Build two garbage collectors: 63 | 1. **Reference Counting**: A simple garbage collector that tracks references to objects. 64 | 2. **Mark and Sweep**: A more advanced garbage collector that identifies and frees unreachable memory. 65 | - Compare these approaches with garbage collection in Go and Java. 66 | 67 | ### **Phase 4: Contributing to Sneklang** 68 | - Learn how programming languages are built by contributing to **Sneklang**. 69 | - Implement features like lexical analysis, parsing, and memory management. 70 | - Integrate a garbage collector into Sneklang so developers don’t have to worry about memory. 71 | 72 | --- 73 | 74 | ## Learning Outcomes 75 | 76 | By the end of this course, you will: 77 | 1. **Understand Memory**: 78 | - Know how programs store and manage data in memory. 79 | - Be able to use pointers and dynamic memory allocation effectively. 80 | 81 | 2. **Write Efficient Programs**: 82 | - Optimize code for performance and memory usage. 83 | - Debug memory-related issues using tools like `valgrind`. 84 | 85 | 3. **Build Practical Tools**: 86 | - Create your own garbage collectors. 87 | - Contribute to the development of a simple programming language (Sneklang). 88 | 89 | 4. **Think Like a Low-Level Programmer**: 90 | - Gain a deep understanding of how low-level languages like C interact with hardware. 91 | - Be prepared to work on systems programming, embedded systems, or language development. 92 | 93 | --- 94 | 95 | ## How to Use This Repository 96 | 97 | This section provides instructions on how to set up and use the course material in this repository. Follow these steps to get started: 98 | 99 | 1. **Clone the Repository**: 100 | - Cloning means creating a local copy of this repository on your computer. 101 | - Run the following command in your terminal (replace `` with the actual URL of this repository): 102 | ```bash 103 | git clone 104 | ``` 105 | - Example: 106 | ```bash 107 | git clone https://github.com/your-username/intro-to-c-programming.git 108 | ``` 109 | - This will create a folder named `intro-to-c-programming` on your computer with all the course material. 110 | 111 | 2. **Navigate to the Course Material**: 112 | - After cloning, navigate into the repository folder: 113 | ```bash 114 | cd intro-to-c-programming 115 | ``` 116 | - Inside the folder, you’ll find subfolders for each topic (e.g., `01_Basics_of_C`, `02_Memory_Management`). 117 | - Start with the first topic (`01_Basics_of_C`) and work your way through the material sequentially. 118 | 119 | 3. **Run Code Examples**: 120 | - Each topic includes code examples to help you understand the concepts. 121 | - To compile and run a C program, use the `gcc` compiler: 122 | ```bash 123 | gcc program.c -o program 124 | ./program 125 | ``` 126 | - Replace `program.c` with the name of the file you want to compile. 127 | 128 | 4. **Complete Exercises**: 129 | - Each topic includes practical exercises to reinforce your understanding. 130 | - Solutions to the exercises are provided in the `solutions` folder. 131 | - Try to solve the exercises on your own before checking the solutions. 132 | 133 | 5. **Contribute**: 134 | - If you find a bug or want to improve the material, you can contribute to the repository. 135 | - To contribute: 136 | 1. Fork the repository (create your own copy on GitHub). 137 | 2. Make your changes. 138 | 3. Submit a pull request to merge your changes into the main repository. 139 | - For more details, check out the [Contributing Guide](CONTRIBUTING.md). 140 | -------------------------------------------------------------------------------- /03_Introduction_to_Linux/notes/brief_introduction_of_linux_commands.md: -------------------------------------------------------------------------------- 1 | Content to Copy: 2 | markdown 3 | Copy 4 | 5 | # Brief Introduction to Linux Commands 6 | 7 | This file provides a comprehensive explanation of basic Linux commands, along with examples and exercises to help you get started with the Linux command line. 8 | 9 | --- 10 | 11 | ## **1. Navigating the File System** 12 | ### **`pwd` (Print Working Directory)** 13 | - Displays the current directory you are in. 14 | - Example: 15 | ```bash 16 | pwd 17 | 18 | ls (List Files) 19 | 20 | Lists files and directories in the current directory. 21 | 22 | Example: 23 | bash 24 | Copy 25 | 26 | ls 27 | 28 | cd (Change Directory) 29 | 30 | Changes the current directory. 31 | 32 | Example: 33 | bash 34 | Copy 35 | 36 | cd /path/to/directory 37 | 38 | 2. File and Directory Operations 39 | touch (Create File) 40 | 41 | Creates an empty file. 42 | 43 | Example: 44 | bash 45 | Copy 46 | 47 | touch example.txt 48 | 49 | mkdir (Make Directory) 50 | 51 | Creates a new directory. 52 | 53 | Example: 54 | bash 55 | Copy 56 | 57 | no cuts just continue in one file stop cutting from pwd..just continue 58 | 59 | Got it! Here’s the complete content for the notes file (brief_introduction_of_linux_commands.md) in one continuous block without any interruptions or formatting breaks. You can copy this directly into your nano editor. 60 | Content to Copy: 61 | markdown 62 | Copy 63 | 64 | # Brief Introduction to Linux Commands 65 | 66 | This file provides a comprehensive explanation of basic Linux commands, along with examples and exercises to help you get started with the Linux command line. 67 | 68 | --- 69 | 70 | ## **1. Navigating the File System** 71 | ### **`pwd` (Print Working Directory)** 72 | - Displays the current directory you are in. 73 | - Example: 74 | ```bash 75 | pwd 76 | 77 | ls (List Files) 78 | 79 | Lists files and directories in the current directory. 80 | 81 | Example: 82 | bash 83 | Copy 84 | 85 | ls 86 | 87 | cd (Change Directory) 88 | 89 | Changes the current directory. 90 | 91 | Example: 92 | bash 93 | Copy 94 | 95 | cd /path/to/directory 96 | 97 | 2. File and Directory Operations 98 | touch (Create File) 99 | 100 | Creates an empty file. 101 | 102 | Example: 103 | bash 104 | Copy 105 | 106 | touch example.txt 107 | 108 | mkdir (Make Directory) 109 | 110 | Creates a new directory. 111 | 112 | Example: 113 | bash 114 | Copy 115 | 116 | mkdir new_directory 117 | 118 | rm (Remove File) 119 | 120 | Deletes a file. 121 | 122 | Example: 123 | bash 124 | Copy 125 | 126 | rm example.txt 127 | 128 | rmdir (Remove Directory) 129 | 130 | Deletes an empty directory. 131 | 132 | Example: 133 | bash 134 | Copy 135 | 136 | rmdir empty_directory 137 | 138 | cp (Copy File or Directory) 139 | 140 | Copies a file or directory. 141 | 142 | Example: 143 | bash 144 | Copy 145 | 146 | cp file1.txt file2.txt 147 | 148 | mv (Move or Rename File or Directory) 149 | 150 | Moves or renames a file or directory. 151 | 152 | Example: 153 | bash 154 | Copy 155 | 156 | mv old_name.txt new_name.txt 157 | 158 | 3. Viewing and Editing Files 159 | cat (Concatenate and Display File Content) 160 | 161 | Displays the contents of a file. 162 | 163 | Example: 164 | bash 165 | Copy 166 | 167 | cat example.txt 168 | 169 | nano (Text Editor) 170 | 171 | Opens a simple text editor to create or edit files. 172 | 173 | Example: 174 | bash 175 | Copy 176 | 177 | nano example.txt 178 | 179 | less (View File Content Page by Page) 180 | 181 | Displays file content one page at a time. 182 | 183 | Example: 184 | bash 185 | Copy 186 | 187 | less example.txt 188 | 189 | 4. System Information 190 | uname (Display System Information) 191 | 192 | Displays information about the system. 193 | 194 | Example: 195 | bash 196 | Copy 197 | 198 | uname -a 199 | 200 | df (Disk Free Space) 201 | 202 | Displays disk space usage. 203 | 204 | Example: 205 | bash 206 | Copy 207 | 208 | df -h 209 | 210 | top (Display Running Processes) 211 | 212 | Displays real-time system processes. 213 | 214 | Example: 215 | bash 216 | Copy 217 | 218 | top 219 | 220 | 5. Miscellaneous Commands 221 | man (Manual Pages) 222 | 223 | Displays the manual page for a command. 224 | 225 | Example: 226 | bash 227 | Copy 228 | 229 | man ls 230 | 231 | clear (Clear Terminal Screen) 232 | 233 | Clears the terminal screen. 234 | 235 | Example: 236 | bash 237 | Copy 238 | 239 | clear 240 | 241 | history (Command History) 242 | 243 | Displays previously executed commands. 244 | 245 | Example: 246 | bash 247 | Copy 248 | 249 | history 250 | 251 | Examples: Basic Linux Commands 252 | 253 | This section provides examples of basic Linux commands to help you practice. 254 | 1. Navigating the File System 255 | 256 | Print the current directory: 257 | bash 258 | Copy 259 | 260 | pwd 261 | 262 | List files in the current directory: 263 | bash 264 | Copy 265 | 266 | ls 267 | 268 | Change to the home directory: 269 | bash 270 | Copy 271 | 272 | cd ~ 273 | 274 | 2. File and Directory Operations 275 | 276 | Create a file: 277 | bash 278 | Copy 279 | 280 | touch example.txt 281 | 282 | Create a directory: 283 | bash 284 | Copy 285 | 286 | mkdir new_directory 287 | 288 | Delete a file: 289 | bash 290 | Copy 291 | 292 | rm example.txt 293 | 294 | 3. Viewing and Editing Files 295 | 296 | Display file content: 297 | bash 298 | Copy 299 | 300 | cat example.txt 301 | 302 | Edit a file: 303 | bash 304 | Copy 305 | 306 | nano example.txt 307 | 308 | 4. System Information 309 | 310 | Display system information: 311 | bash 312 | Copy 313 | 314 | uname -a 315 | 316 | Display disk space usage: 317 | bash 318 | Copy 319 | 320 | df -h 321 | 322 | 5. Miscellaneous Commands 323 | 324 | View the manual for a command: 325 | bash 326 | Copy 327 | 328 | man ls 329 | 330 | Clear the terminal screen: 331 | bash 332 | Copy 333 | 334 | clear 335 | 336 | Exercises: Basic Linux Commands 337 | 338 | This section provides exercises to practice basic Linux commands. 339 | 1. Navigating the File System 340 | 341 | Use the pwd command to print the current directory. 342 | 343 | Use the ls command to list files in the current directory. 344 | 345 | Use the cd command to change to the home directory. 346 | 347 | 2. File and Directory Operations 348 | 349 | Create a file called test.txt using the touch command. 350 | 351 | Create a directory called test_dir using the mkdir command. 352 | 353 | Delete the file test.txt using the rm command. 354 | 355 | 3. Viewing and Editing Files 356 | 357 | Display the contents of a file using the cat command. 358 | 359 | Edit a file using the nano command. 360 | 361 | 4. System Information 362 | 363 | Display system information using the uname command. 364 | 365 | Display disk space usage using the df command. 366 | 367 | 5. Miscellaneous Commands 368 | 369 | View the manual for the ls command using the man command. 370 | 371 | Clear the terminal screen using the clear command. 372 | 373 | Copy 374 | 375 | 376 | --- 377 | 378 | ### How to Use This File: 379 | 1. **Navigate to the `notes` Directory**: 380 | ```bash 381 | cd ~/introduction-to-c/03_Introduction_to_Linux/notes 382 | 383 | Create the File: 384 | bash 385 | Copy 386 | 387 | nano brief_introduction_of_linux_commands.md 388 | 389 | -------------------------------------------------------------------------------- /01_History_of_C/examples/history_examples.md: -------------------------------------------------------------------------------- 1 | # Examples: History of C 2 | 3 | ## Example 1: Hello World in C 4 | This is the classic "Hello, World!" program in C, demonstrating the simplicity and readability of the language. 5 | 6 | ```c 7 | #include 8 | 9 | int main() { 10 | printf("Hello, World!\n"); 11 | return 0; 12 | } 13 | Explanation: 14 | 15 | #include : Includes the standard input/output library, which provides the printf function. 16 | 17 | int main(): The entry point of the program. Every C program must have a main function. 18 | 19 | printf("Hello, World!\n");: Prints "Hello, World!" to the console. The \n adds a newline. 20 | 21 | return 0;: Indicates that the program executed successfully. 22 | 23 | Example 2: Using Data Types in C 24 | 25 | C introduced data types, making programs more efficient and easier to understand. 26 | 27 | #include 28 | 29 | int main() { 30 | int age = 25; // Integer data type 31 | float height = 5.9; // Floating-point data type 32 | char initial = 'J'; // Character data type 33 | 34 | printf("Age: %d\n", age); 35 | printf("Height: %.2f\n", height); 36 | printf("Initial: %c\n", initial); 37 | 38 | return 0; 39 | } 40 | 41 | Explanation: 42 | 43 | int age = 25;: Declares an integer variable named age and assigns it the value 25. 44 | 45 | float height = 5.9;: Declares a floating-point variable named height and assigns it the value 5.9. 46 | 47 | char initial = 'J';: Declares a character variable named initial and assigns it the value 'J'. 48 | 49 | printf: Prints the values of the variables using format specifiers (%d for integers, %f for floats, %c for characters). 50 | 51 | Example 3: Using Pointers in C 52 | 53 | C introduced pointers, allowing direct manipulation of memory addresses. 54 | 55 | Explanation: 56 | 57 | int age = 25;: Declares an integer variable named age and assigns it the value 25. 58 | 59 | float height = 5.9;: Declares a floating-point variable named height and assigns it the value 5.9. 60 | 61 | char initial = 'J';: Declares a character variable named initial and assigns it the value 'J'. 62 | 63 | printf: Prints the values of the variables using format specifiers (%d for integers, %f for floats, %c for characters). 64 | 65 | Example 3: Using Pointers in C 66 | 67 | C introduced pointers, allowing direct manipulation of memory addresses. 68 | 69 | #include 70 | 71 | int main() { 72 | int number = 42; // Declare an integer variable 73 | int *pointer = &number; // Declare a pointer and assign it the address of `number` 74 | 75 | printf("Value of number: %d\n", number); 76 | printf("Address of number: %p\n", (void*)pointer); 77 | printf("Value at address: %d\n", *pointer); 78 | 79 | return 0; 80 | } 81 | 82 | 83 | Explanation: 84 | 85 | int *pointer = &number;: Declares a pointer to an integer and assigns it the address of number. 86 | 87 | %p: Format specifier for printing memory addresses. 88 | 89 | *pointer: Dereferences the pointer to access the value stored at the address it points to. 90 | 91 | Example 4: Using Standard Libraries in C 92 | 93 | C introduced standard libraries, providing pre-built functions for common tasks. 94 | 95 | #include 96 | #include 97 | 98 | int main() { 99 | int *arr = (int *)malloc(5 * sizeof(int)); // Dynamically allocate memory for 5 integers 100 | 101 | if (arr == NULL) { 102 | printf("Memory allocation failed\n"); 103 | return 1; 104 | } 105 | 106 | for (int i = 0; i < 5; i++) { 107 | arr[i] = i + 1; // Assign values to the array 108 | } 109 | 110 | for (int i = 0; i < 5; i++) { 111 | printf("%d ", arr[i]); // Print the array values 112 | } 113 | 114 | free(arr); // Free the allocated memory 115 | return 0; 116 | } 117 | 118 | 119 | Explanation: 120 | 121 | #include : Includes the standard library for memory allocation. 122 | 123 | malloc: Allocates memory dynamically on the heap. 124 | 125 | free: Releases the allocated memory to avoid memory leaks. 126 | 127 | Example 5: Portability of C 128 | 129 | C’s hardware-independent design allows programs to run on different systems with minimal changes. 130 | 131 | #include 132 | 133 | int main() { 134 | #ifdef _WIN32 135 | printf("Running on Windows\n"); 136 | #elif __linux__ 137 | printf("Running on Linux\n"); 138 | #elif __APPLE__ 139 | printf("Running on macOS\n"); 140 | #else 141 | printf("Running on an unknown system\n"); 142 | #endif 143 | 144 | return 0; 145 | } 146 | 147 | Explanation: 148 | 149 | #ifdef, #elif, #else, #endif: Preprocessor directives for conditional compilation. 150 | 151 | This program detects the operating system and prints a message accordingly. 152 | 153 | Example 6: Rewriting UNIX in C 154 | 155 | The following example demonstrates how C’s features made it suitable for rewriting UNIX. 156 | 157 | #include 158 | #include 159 | 160 | int main() { 161 | printf("Process ID: %d\n", getpid()); // Print the process ID 162 | return 0; 163 | } 164 | 165 | Explanation: 166 | 167 | #include : Includes the POSIX API for system calls. 168 | 169 | getpid(): Returns the process ID of the current program. 170 | 171 | This program demonstrates C’s ability to interact with the operating system. 172 | 173 | Example 7: Influence on Modern Languages 174 | 175 | C’s syntax and concepts influenced many modern programming languages. Here’s a comparison with Python: 176 | C: 177 | 178 | #include 179 | 180 | int main() { 181 | int i; 182 | for (i = 0; i < 5; i++) { 183 | printf("%d\n", i); // Print numbers from 0 to 4 184 | } 185 | return 0; 186 | } 187 | 188 | Python: 189 | 190 | for i in range(5): 191 | print(i) # Print numbers from 0 to 4 192 | 193 | 194 | Explanation: 195 | 196 | Both programs print numbers from 0 to 4. 197 | 198 | C uses explicit syntax, while Python is more concise, but the underlying logic is similar. 199 | 200 | Example 8: Using return 0; 201 | 202 | The return 0; statement indicates successful program execution. 203 | 204 | #include 205 | 206 | int main() { 207 | printf("Program executed successfully.\n"); 208 | return 0; // Indicate successful execution 209 | } 210 | 211 | Explanation: 212 | 213 | return 0;: Indicates that the program completed without errors. 214 | 215 | Non-zero values can be used to indicate specific errors. 216 | 217 | Example 9: Low-Level Access with Pointers 218 | 219 | C’s pointers allow direct memory manipulation, making it suitable for system programming. 220 | 221 | #include 222 | 223 | int main() { 224 | int arr[3] = {10, 20, 30}; // Declare and initialize an array 225 | int *ptr = arr; // Declare a pointer and assign it the address of the first element 226 | 227 | for (int i = 0; i < 3; i++) { 228 | printf("Value: %d, Address: %p\n", *ptr, (void*)ptr); // Print value and address 229 | ptr++; // Move the pointer to the next element 230 | } 231 | 232 | return 0; 233 | } 234 | 235 | Explanation: 236 | 237 | int *ptr = arr;: Points to the first element of the array. 238 | 239 | ptr++: Moves the pointer to the next element. 240 | 241 | This program demonstrates how pointers can traverse arrays. 242 | 243 | Example 10: Standard Input/Output 244 | 245 | C’s standard libraries simplify input/output operations. 246 | 247 | #include 248 | 249 | int main() { 250 | char name[50]; // Declare a character array to store the name 251 | printf("Enter your name: "); 252 | scanf("%s", name); // Read a string from the user 253 | printf("Hello, %s!\n", name); // Print a personalized greeting 254 | return 0; 255 | } 256 | 257 | Explanation: 258 | 259 | scanf("%s", name);: Reads a string from the user and stores it in the name array. 260 | 261 | printf("Hello, %s!\n", name);: Prints a personalized greeting using the entered name. 262 | 263 | 264 | --- 265 | 266 | ### **Steps to Save the File** 267 | 268 | 1. Navigate to the `examples` folder: 269 | ```bash 270 | cd ~/introduction-to-c/01_History_of_C/examples/ 271 | 272 | 2. Create the history_examples.md file: 273 | 274 | nano history_examples.md 275 | 276 | 3. Paste the content above into the file. 277 | 278 | 4. Save and exit: 279 | 280 | Press Ctrl+O, then Enter to save. 281 | 282 | Press Ctrl+X to exit. 283 | -------------------------------------------------------------------------------- /04_Basics_of_C/notes/basics_of_c.md: -------------------------------------------------------------------------------- 1 | # Basics of C Programming 2 | 3 | This file provides a comprehensive introduction to the basics of C programming, including syntax, data types, variables, and control structures. 4 | 5 | --- 6 | 7 | ## **1. Structure of a C Program** 8 | A C program typically consists of the following parts: 9 | - **Preprocessor Directives**: Include header files (e.g., `#include `). 10 | - **Main Function**: The entry point of the program (`int main()`). 11 | - **Statements**: Instructions to be executed. 12 | - **Return Statement**: Indicates the end of the program (`return 0;`). 13 | 14 | Example: 15 | ```c 16 | #include 17 | 18 | int main() { 19 | printf("Hello, World!\n"); 20 | return 0; 21 | } 22 | 23 | 2. Data Types 24 | 25 | C supports several basic data types: 26 | 27 | int: Integer values (e.g., 5, -10). 28 | 29 | float: Floating-point values (e.g., 3.14, -0.5). 30 | 31 | char: Single characters (e.g., 'A', 'z'). 32 | 33 | double: Double-precision floating-point values (e.g., 3.14159). 34 | 35 | 36 | 3. Variables 37 | 38 | Variables are used to store data. 39 | 40 | They must be declared with a data type before use. 41 | 42 | Example: 43 | int age = 25; 44 | float price = 19.99; 45 | char grade = 'A'; 46 | 47 | 4. Input and Output 48 | 49 | printf: Prints output to the console. 50 | 51 | scanf: Reads input from the user. 52 | 53 | Example: 54 | #include 55 | 56 | int main() { 57 | int num; 58 | printf("Enter a number: "); 59 | scanf("%d", &num); 60 | printf("You entered: %d\n", num); 61 | return 0; 62 | } 63 | 64 | 65 | 5. Control Structures 66 | If-Else 67 | 68 | Executes a block of code based on a condition. 69 | 70 | Example: 71 | 72 | int num = 10; 73 | if (num > 0) { 74 | printf("Positive number\n"); 75 | } else { 76 | printf("Negative number\n"); 77 | } 78 | 79 | Loops 80 | 81 | for Loop: Repeats a block of code a specific number of times. 82 | 83 | while Loop: Repeats a block of code while a condition is true. 84 | 85 | Example: 86 | 87 | for (int i = 0; i < 5; i++) { 88 | printf("%d\n", i); 89 | } 90 | 91 | int i = 0; 92 | while (i < 5) { 93 | printf("%d\n", i); 94 | i++; 95 | } 96 | 97 | ## **5.1. Control Structures(more explanations)** 98 | ### **If-Else** 99 | - Executes a block of code based on a condition. 100 | - Example: 101 | ```c 102 | int num = 10; 103 | if (num > 0) { 104 | printf("Positive number\n"); 105 | } else { 106 | printf("Negative number\n"); 107 | } 108 | 109 | Loops 110 | 111 | Loops are used to repeat a block of code multiple times. The two most common types of loops in C are for loops and while loops. 112 | for Loop 113 | 114 | Use a for loop when you know exactly how many times you want to repeat the code. 115 | 116 | It consists of three parts: 117 | 118 | Initialization: Executed once at the beginning. 119 | 120 | Condition: Checked before each iteration. 121 | 122 | Update: Executed after each iteration. 123 | 124 | Example: 125 | 126 | for (int i = 0; i < 5; i++) { 127 | printf("%d\n", i); 128 | } 129 | 130 | When to Use: 131 | 132 | When iterating over a known range (e.g., printing numbers from 1 to 10). 133 | 134 | When working with arrays (e.g., accessing each element in an array). 135 | 136 | while Loop 137 | 138 | Use a while loop when you want to repeat the code as long as a condition is true, but you don’t know how many times it will run. 139 | 140 | It consists of a single condition that is checked before each iteration. 141 | 142 | Example: 143 | 144 | int i = 0; 145 | while (i < 5) { 146 | printf("%d\n", i); 147 | i++; 148 | } 149 | 150 | When to Use: 151 | 152 | When the number of iterations is unknown (e.g., reading input until the user enters a specific value). 153 | 154 | When the loop depends on a condition that may change during execution (e.g., processing data until a certain condition is met). 155 | 156 | Key Differences 157 | Feature for Loop while Loop 158 | Use Case Known number of iterations Unknown number of iterations 159 | Initialization Done in the loop header Done before the loop 160 | Condition Check Before each iteration Before each iteration 161 | Update Done in the loop header Done inside the loop body 162 | 163 | 164 | Example: When to Use Each 165 | 166 | for Loop: 167 | 168 | // Print numbers from 1 to 10 169 | for (int i = 1; i <= 10; i++) { 170 | printf("%d\n", i); 171 | } 172 | 173 | while Loop: 174 | 175 | // Keep asking for input until the user enters 0 176 | int num; 177 | printf("Enter a number (0 to exit): "); 178 | scanf("%d", &num); 179 | while (num != 0) { 180 | printf("You entered: %d\n", num); 181 | printf("Enter another number (0 to exit): "); 182 | scanf("%d", &num); 183 | } 184 | 185 | Updated examples.c File (Excerpt for Loops) 186 | 187 | // Example 5: For Loop 188 | void for_loop() { 189 | printf("=== For Loop ===\n"); 190 | printf("Printing numbers from 1 to 5:\n"); 191 | for (int i = 1; i <= 5; i++) { 192 | printf("%d\n", i); 193 | } 194 | printf("\n"); 195 | } 196 | 197 | // Example 6: While Loop 198 | void while_loop() { 199 | printf("=== While Loop ===\n"); 200 | printf("Keep entering numbers (0 to exit):\n"); 201 | int num; 202 | scanf("%d", &num); 203 | while (num != 0) { 204 | printf("You entered: %d\n", num); 205 | printf("Enter another number (0 to exit): "); 206 | scanf("%d", &num); 207 | } 208 | printf("\n"); 209 | } 210 | 211 | 212 | 6. Functions 213 | 214 | Functions are reusable blocks of code. 215 | 216 | They must be declared with a return type and parameters. 217 | 218 | Example: 219 | 7. Arrays 220 | 221 | Arrays store multiple values of the same type. 222 | 223 | Example: 224 | 225 | int numbers[5] = {1, 2, 3, 4, 5}; 226 | for (int i = 0; i < 5; i++) { 227 | printf("%d\n", numbers[i]); 228 | } 229 | 230 | 8. Pointers 231 | 232 | Pointers store memory addresses. 233 | 234 | Example: 235 | int num = 10; 236 | int *ptr = # 237 | printf("Value: %d, Address: %p\n", *ptr, ptr); 238 | 239 | 9. Structures 240 | 241 | Structures allow you to group related data. 242 | 243 | Example: 244 | 245 | struct Person { 246 | char name[50]; 247 | int age; 248 | }; 249 | 250 | int main() { 251 | struct Person p1 = {"John", 25}; 252 | printf("Name: %s, Age: %d\n", p1.name, p1.age); 253 | return 0; 254 | } 255 | 256 | 10. File Handling 257 | 258 | C provides functions to read from and write to files. 259 | 260 | Example: 261 | 262 | #include 263 | 264 | int main() { 265 | FILE *file = fopen("example.txt", "w"); 266 | if (file != NULL) { 267 | fprintf(file, "Hello, File!\n"); 268 | fclose(file); 269 | } 270 | return 0; 271 | } 272 | 273 | 274 | --- 275 | 276 | ### 2. **Populate `examples.c` (Examples)** 277 | Below is the content for the `examples.c` file. It provides **executable code snippets** that align with the notes. 278 | 279 | ```c 280 | #include 281 | 282 | // Example 1: Hello World 283 | void hello_world() { 284 | printf("=== Hello World ===\n"); 285 | printf("Hello, World!\n"); 286 | printf("\n"); 287 | } 288 | 289 | // Example 2: Variables and Data Types 290 | void variables_and_data_types() { 291 | printf("=== Variables and Data Types ===\n"); 292 | int age = 25; 293 | float price = 19.99; 294 | char grade = 'A'; 295 | 296 | printf("Age: %d\n", age); 297 | printf("Price: %.2f\n", price); 298 | printf("Grade: %c\n", grade); 299 | printf("\n"); 300 | } 301 | 302 | // Example 3: Input and Output 303 | void input_and_output() { 304 | printf("=== Input and Output ===\n"); 305 | int num; 306 | printf("Enter a number: "); 307 | scanf("%d", &num); 308 | printf("You entered: %d\n", num); 309 | printf("\n"); 310 | } 311 | 312 | // Example 4: If-Else Statement 313 | void if_else_statement() { 314 | printf("=== If-Else Statement ===\n"); 315 | int num = 10; 316 | if (num > 0) { 317 | printf("Positive number\n"); 318 | } else { 319 | printf("Negative number\n"); 320 | } 321 | printf("\n"); 322 | } 323 | 324 | // Example 5: For Loop 325 | void for_loop() { 326 | printf("=== For Loop ===\n"); 327 | for (int i = 0; i < 5; i++) { 328 | printf("%d\n", i); 329 | } 330 | printf("\n"); 331 | } 332 | 333 | // Example 6: Functions 334 | int add(int a, int b) { 335 | return a + b; 336 | } 337 | 338 | void functions_example() { 339 | printf("=== Functions ===\n"); 340 | int result = add(5, 3); 341 | printf("Result: %d\n", result); 342 | printf("\n"); 343 | } 344 | 345 | // Example 7: Arrays 346 | void arrays_example() { 347 | printf("=== Arrays ===\n"); 348 | int numbers[5] = {1, 2, 3, 4, 5}; 349 | for (int i = 0; i < 5; i++) { 350 | printf("%d\n", numbers[i]); 351 | } 352 | printf("\n"); 353 | } 354 | 355 | // Example 8: Pointers 356 | void pointers_example() { 357 | printf("=== Pointers ===\n"); 358 | int num = 10; 359 | int *ptr = # 360 | printf("Value: %d, Address: %p\n", *ptr, ptr); 361 | printf("\n"); 362 | } 363 | 364 | // Example 9: Structures 365 | struct Person { 366 | char name[50]; 367 | int age; 368 | }; 369 | 370 | void structures_example() { 371 | printf("=== Structures ===\n"); 372 | struct Person p1 = {"John", 25}; 373 | printf("Name: %s, Age: %d\n", p1.name, p1.age); 374 | printf("\n"); 375 | } 376 | 377 | // Example 10: File Handling 378 | void file_handling_example() { 379 | printf("=== File Handling ===\n"); 380 | FILE *file = fopen("example.txt", "w"); 381 | if (file != NULL) { 382 | fprintf(file, "Hello, File!\n"); 383 | fclose(file); 384 | printf("File created and written successfully.\n"); 385 | } else { 386 | printf("Failed to create file.\n"); 387 | } 388 | printf("\n"); 389 | } 390 | 391 | // Main Function to Run All Examples 392 | int main() { 393 | hello_world(); 394 | variables_and_data_types(); 395 | input_and_output(); 396 | if_else_statement(); 397 | for_loop(); 398 | functions_example(); 399 | arrays_example(); 400 | pointers_example(); 401 | structures_example(); 402 | file_handling_example(); 403 | 404 | return 0; 405 | } 406 | 407 | --------------------------------------------------------------------------------