├── 1-getting-started-with-c-programming.md ├── 10-Ternary Operator.md ├── 11-Switch Statement in C.md ├── 12-while loop in C.md ├── 13-for Loop.md ├── 14. break and continue.md ├── 15. C functions.md ├── 16. C variable scoope and storage class.md ├── 17. C Standard Library Function.md ├── 18. C Recursion.md ├── 19. C Array.md ├── 2-C-Variables.md ├── 20. C Multidimensional arrays.md ├── 21. C strings.md ├── 22. C String Functions.md ├── 23. C Pointers.md ├── 24. C pointers and arrays. C pointers and arrays.md ├── 25. C Pointers and Functions.md ├── 26. C Struct.md ├── 27. C enum.md ├── 28. Dynamic Memory Allocation.md ├── 29. File Handling.md ├── 3-data-types.md ├── 4.Take-Input.md ├── 5-Comment.md ├── 6-C Operators.md ├── 7-Type-Conversion.md ├── 8-C Boolean.md └── 9-If Else Statement in C.md /1-getting-started-with-c-programming.md: -------------------------------------------------------------------------------- 1 | # 1 Getting Started With C Programming 2 | 3 | Video Link: [https://youtu.be/KnvbUiSxvbM](https://youtu.be/KnvbUiSxvbM) 4 | Tutorial Link: [https://www.programiz.com/c-programming](https://www.programiz.com/c-programming) 5 | 6 | Hello World is a simple program that is often used to introduce a new programming language to beginners. 7 | 8 | ## Hello World Program in C 9 | 10 | ```c 11 | #include 12 | 13 | int main() { 14 | printf("Hello, World!"); 15 | 16 | return 0; 17 | } 18 | ``` 19 | 20 | **Output** 21 | 22 | ``` 23 | Hello, World! 24 | ``` -------------------------------------------------------------------------------- /10-Ternary Operator.md: -------------------------------------------------------------------------------- 1 | # Ternary Operator in C 2 | Video Link: [https://youtu.be/05xv2nMj6Ls](https://youtu.be/05xv2nMj6Ls) 3 | Tutorial Link: [https://www.programiz.com/c-programming]() 4 | 5 | ## Syntax : 6 | 7 | ```c 8 | test_condition ? expression1 : expression2; 9 | ``` 10 | 11 | ## Example: Ternary Operator 12 | ### Example 1: 13 | ```c 14 | #include 15 | 16 | int main() { 17 | 18 | int age = 15; 19 | 20 | (age >= 18) ? printf("You can vote") : printf("You cannot vote"); 21 | 22 | return 0; 23 | } 24 | 25 | ``` 26 | 27 | **Output** 28 | ``` 29 | You cannot vote 30 | 31 | ``` 32 | ### Example 2: 33 | ```c 34 | #include 35 | 36 | int main() { 37 | 38 | int age = 24; 39 | 40 | (age >= 18) ? printf("You can vote") : printf("You cannot vote"); 41 | 42 | return 0; 43 | } 44 | 45 | ``` 46 | 47 | **Output** 48 | ``` 49 | You can vote 50 | ``` 51 | ## Example 3 : Using variable in ternary operator 52 | 53 | ```c 54 | #include 55 | 56 | int main() { 57 | 58 | char operator = '+'; 59 | 60 | int num1 = 8; 61 | int num2 = 7; 62 | 63 | int result = (operator == '+') ? (num1 + num2) : (num1 - num2); 64 | printf("%d", result); 65 | 66 | return 0; 67 | } 68 | 69 | ``` 70 | **Output** 71 | ``` 72 | 15 73 | ``` 74 | 75 | --- 76 | 77 | ## Programming Task 78 | 79 | Q. Can you create a program to check whether a number is odd or even? To create this program, create a variable named number and assign a value to it. Then using a ternary operator check if the number variable is odd or even. 80 | 81 | - If number is odd, print "The number is Odd" 82 | - If number is even, print "The number is Even" 83 | 84 | ## Solution : 85 | ```c 86 | #include 87 | 88 | int main() { 89 | 90 | int number; 91 | printf("Enter a number: "); 92 | scanf("%d", &number); 93 | 94 | (number % 2 == 0) ? printf("It is even ") : printf("It is odd"); 95 | 96 | return 0; 97 | } 98 | ``` 99 | **Output** 100 | ``` 101 | Enter a number: 5 102 | It is odd 103 | ``` 104 | --- 105 | 106 | ## Programiz Quiz 107 | 108 | Q. What is the correct ternary equivalent of the following if...else statement? 109 | 110 | 111 | ```c 112 | if (5 > 3) { 113 | result = 9; 114 | } 115 | else { 116 | result = 3; 117 | } 118 | ``` 119 | 120 | **Options:** 121 | 1. result = 5 > 3 ? 3 : 5; 122 | 1. 5 > 3 ? result = 5 : 3; 123 | 1. result = 5 > 3 ? 5 : 3; 124 | 1. 5 > 3 ? result = 3 : 5; 125 | 126 | **Answer: 3** -------------------------------------------------------------------------------- /11-Switch Statement in C.md: -------------------------------------------------------------------------------- 1 | # Switch Statement in C Programming 2 | Video Link: [https://youtu.be/u6mb8NNwojA](https://youtu.be/u6mb8NNwojA) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-switch-case-statement](https://www.programiz.com/c-programming/c-switch-case-statement) 4 | 5 | ### Syntax : 6 | 7 | ```c 8 | switch(variable/expression) { 9 | case value1: 10 | // body of case 1 11 | break; 12 | 13 | case value2: 14 | // body of case 2 15 | break; 16 | 17 | case valueN: 18 | // body of case N 19 | break; 20 | 21 | default: 22 | // body of default 23 | } 24 | 25 | ``` 26 | 27 | ### Example 1 : print the day of a week 28 | ```c 29 | #include 30 | 31 | int main() { 32 | 33 | int number; 34 | printf("Enter a number between 1 to 7: "); 35 | scanf("%d", &number); 36 | 37 | switch(number) { 38 | case 1: 39 | printf("Sunday"); 40 | break; 41 | 42 | case 2: 43 | printf("Monday"); 44 | break; 45 | 46 | case 3: 47 | printf("Tuesday"); 48 | break; 49 | 50 | case 4: 51 | printf("Wednesday"); 52 | break; 53 | 54 | case 5: 55 | printf("Thursday"); 56 | break; 57 | 58 | case 6: 59 | printf("Friday"); 60 | break; 61 | 62 | case 7: 63 | printf("Saturday"); 64 | break; 65 | 66 | default: 67 | printf("Invalid Number"); 68 | } 69 | 70 | return 0; 71 | } 72 | 73 | ``` 74 | 75 | **Output** 76 | ``` 77 | Enter a number between 1 to 7: 5 78 | Thursday 79 | 80 | ``` 81 | ### Example 2 : Removing break statement from previous example 82 | ```c 83 | #include 84 | 85 | int main() { 86 | 87 | int number; 88 | printf("Enter a number between 1 to 7: "); 89 | scanf("%d", &number); 90 | 91 | switch(number) { 92 | case 1: 93 | printf("Sunday "); 94 | 95 | case 2: 96 | printf("Monday "); 97 | 98 | case 3: 99 | printf("Tuesday "); 100 | 101 | case 4: 102 | printf("Wednesday "); 103 | 104 | case 5: 105 | printf("Thursday "); 106 | 107 | case 6: 108 | printf("Friday "); 109 | 110 | case 7: 111 | printf("Saturday "); 112 | 113 | default: 114 | printf("Invalid Number"); 115 | } 116 | 117 | return 0; 118 | } 119 | 120 | ``` 121 | 122 | **Output** 123 | ``` 124 | Enter a number between 1 to 7: 5 125 | Thursday Friday Saturday Invalid Number 126 | 127 | ``` 128 | ## switch with multiple case 129 | 130 | ```c 131 | #include 132 | 133 | int main() { 134 | 135 | int number; 136 | printf("Enter a number between 1 to 7: "); 137 | scanf("%d", &number); 138 | 139 | switch(number) { 140 | case 2: 141 | case 3: 142 | case 4: 143 | case 5: 144 | case 6: 145 | printf("Weekday"); 146 | break; 147 | 148 | case 1: 149 | case 7: 150 | printf("Weekend"); 151 | break; 152 | 153 | default: 154 | printf("Invalid Number"); 155 | } 156 | 157 | return 0; 158 | } 159 | 160 | ``` 161 | **Output** 162 | ``` 163 | Enter a number between 1 to 7: 4 164 | Weekday 165 | 166 | ``` 167 | 168 | ## Simple Calculator 169 | 170 | ```c 171 | #include 172 | 173 | int main() { 174 | 175 | char operator; 176 | printf("Choose an operator ['+', '-', '*', '/']: "); 177 | scanf("%c", &operator); 178 | 179 | double num1, num2; 180 | 181 | printf("Enter first number: "); 182 | scanf("%lf", &num1); 183 | 184 | printf("Enter second number: "); 185 | scanf("%lf", &num2); 186 | 187 | double result; 188 | 189 | switch(operator) { 190 | case '+': 191 | result = num1 + num2; 192 | break; 193 | case '-': 194 | result = num1 - num2; 195 | break; 196 | case '/': 197 | result = num1 / num2; 198 | break; 199 | case '*': 200 | result = num1 * num2; 201 | break; 202 | 203 | default: 204 | printf("Invalid Operator"); 205 | } 206 | 207 | printf("%.2lf", result); 208 | 209 | return 0; 210 | } 211 | 212 | ``` 213 | **Output** 214 | ``` 215 | Choose an operator ['+', '-', '*', '/']: + 216 | Enter first number: 8 217 | Enter second number: 12 218 | 20.00 219 | 220 | ``` 221 | --- 222 | 223 | ## Programming Task 224 | 225 | Q. Use the switch statement to create a program that will find the month based on the number input. Here, take the input number from 1 to 12. And, print the corresponding month based on the input value. 226 | 227 | - If number is 1, print January 228 | - If number is 2, print February 229 | - If number is 3, print March, and so on. 230 | 231 | ## Solution : 232 | ```c 233 | #include 234 | 235 | int main() { 236 | 237 | int number; 238 | printf("Enter a number of a month between 1 to 12: "); 239 | scanf("%d", &number); 240 | 241 | switch (number) { 242 | case 1: 243 | printf("January"); 244 | break; 245 | case 2: 246 | printf("February"); 247 | break; 248 | case 3: 249 | printf("March"); 250 | break; 251 | case 4: 252 | printf("April"); 253 | break; 254 | case 5: 255 | printf("May"); 256 | break; 257 | case 6: 258 | printf("June"); 259 | break; 260 | case 7: 261 | printf("July"); 262 | break; 263 | case 8: 264 | printf("August"); 265 | break; 266 | case 9: 267 | printf("September"); 268 | break; 269 | case 10: 270 | printf("October"); 271 | break; 272 | case 11: 273 | printf("November"); 274 | break; 275 | case 12: 276 | printf("December"); 277 | break; 278 | default: 279 | printf("Enter number between 1 to 12 only"); 280 | } 281 | 282 | return 0; 283 | } 284 | ``` 285 | 286 | **Output** 287 | ``` 288 | Enter a number of a month between 1 to 12: 44 289 | Enter number between 1 to 12 only 290 | ``` 291 | --- 292 | 293 | ## Programiz Quiz 294 | 295 | Q. Which of the cases is executed in the following code? 296 | 297 | ```c 298 | int number = 4; 299 | 300 | switch(number) { 301 | case 1: 302 | case 2: 303 | default: 304 | } 305 | 306 | ``` 307 | 308 | **Options:** 309 | 1. case 1 310 | 1. case 2 311 | 1. case 4 312 | 1. default 313 | 314 | **Answer: 4** -------------------------------------------------------------------------------- /12-while loop in C.md: -------------------------------------------------------------------------------- 1 | # while loop in C Programming 2 | Video Link: [https://youtu.be/WgS_SF1VrEk](https://youtu.be/WgS_SF1VrEk) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-do-while-loops](https://www.programiz.com/c-programming/c-do-while-loops) 4 | 5 | ### Syntax : 6 | 7 | ```c 8 | while (condition) { 9 | // statements inside while 10 | } 11 | 12 | 13 | ``` 14 | 15 | ### Example 1 : infinite while loop 16 | ```c 17 | #include 18 | 19 | int main() { 20 | 21 | while (1 < 5) { 22 | printf("while loop in C \n"); 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | ``` 29 | 30 | **Output** 31 | ``` 32 | while loop in C 33 | while loop in C 34 | while loop in C 35 | while loop in C 36 | .... 37 | 38 | ``` 39 | 40 | ### Example 2 : while loop 41 | ```c 42 | #include 43 | 44 | int main() { 45 | 46 | int count = 1; 47 | 48 | while (count < 5) { 49 | printf("while loop in C \n"); 50 | printf("Count = %d \n", count); 51 | count = count + 1; 52 | } 53 | 54 | return 0; 55 | } 56 | 57 | ``` 58 | 59 | **Output** 60 | ``` 61 | while loop in C 62 | Count = 1 63 | while loop in C 64 | Count = 2 65 | while loop in C 66 | Count = 3 67 | while loop in C 68 | Count = 4 69 | 70 | ``` 71 | ## Create Multiplication Table 72 | 73 | ```c 74 | #include 75 | 76 | int main() { 77 | 78 | int number; 79 | printf("Enter the number: "); 80 | scanf("%d", &number); 81 | 82 | int count = 1; 83 | 84 | while (count <= 10) { 85 | int product = number * count; 86 | printf(" %d * %d = %d \n", number, count, product); 87 | count = count + 1; 88 | } 89 | 90 | return 0; 91 | } 92 | 93 | 94 | ``` 95 | **Output** 96 | ``` 97 | Enter the number: 8 98 | 8 * 1 = 8 99 | 8 * 2 = 16 100 | 8 * 3 = 24 101 | 8 * 4 = 32 102 | 8 * 5 = 40 103 | 8 * 6 = 48 104 | 8 * 7 = 56 105 | 8 * 8 = 64 106 | 8 * 9 = 72 107 | 8 * 10 = 80 108 | 109 | ``` 110 | 111 | ## do...while Loop 112 | 113 | **Syntax :** 114 | 115 | ```c 116 | do { 117 | // body of loop 118 | } while(condition); 119 | ``` 120 | ### Example 1: do..while 121 | ```c 122 | #include 123 | 124 | int main() { 125 | 126 | int count = 1; 127 | 128 | do { 129 | printf("%d \n", count); 130 | count = count + 1; 131 | } while (count < 5); 132 | 133 | return 0; 134 | } 135 | 136 | 137 | ``` 138 | **Output** 139 | ``` 140 | 1 141 | 2 142 | 3 143 | 4 144 | 145 | ``` 146 | ### Example 2: do...while loop with false condition 147 | ```c 148 | #include 149 | 150 | int main() { 151 | 152 | int count = 5; 153 | 154 | do { 155 | printf("%d \n", count); 156 | count = count + 1; 157 | } while (count < 5); 158 | 159 | return 0; 160 | } 161 | 162 | ``` 163 | 164 | **Output** 165 | ``` 166 | 5 167 | ``` 168 | --- 169 | 170 | ## Programming Task 171 | 172 | Q. Can you use the while loop to print the multiplication table for the given number. Print the number from 10 to 1. So the output would be something like this. 173 | 174 | 175 | 9 * 10 = 90 176 | 9 * 9 = 81 177 | 9 * 8 = 72 178 | 9 * 7 = 63 179 | 9 * 6 = 54 180 | 9 * 5 = 45 181 | 9 * 4 = 36 182 | 9 * 3 = 27 183 | 9 * 2 = 18 184 | 9 * 1 = 9 185 | 186 | 187 | ## Solution : 188 | ```c 189 | #include 190 | 191 | int main() { 192 | int number; 193 | printf("Enter the number: "); 194 | scanf("%d", &number); 195 | 196 | int count = 10; 197 | 198 | while(count >= 1){ 199 | int product = number * count; 200 | printf("%d * %d = %d\n", number, count, product); 201 | count = count - 1; 202 | } 203 | return 0; 204 | } 205 | 206 | ``` 207 | **Output** 208 | ``` 209 | Enter the number: 9 210 | 9 * 10 = 90 211 | 9 * 9 = 81 212 | 9 * 8 = 72 213 | 9 * 7 = 63 214 | 9 * 6 = 54 215 | 9 * 5 = 45 216 | 9 * 4 = 36 217 | 9 * 3 = 27 218 | 9 * 2 = 18 219 | 9 * 1 = 9 220 | ``` 221 | --- 222 | 223 | ## Programiz Quiz 224 | 225 | Q. Which of the following causes an infinite loop? 226 | 227 | **Options:** 228 | 1. while (1) {...} 229 | 230 | 1. int i = 3; 231 | while (i < 4) {...} 232 | 233 | 1. int i = 3; 234 | do { 235 | ...} while(i < 4) 236 | 237 | 1. All of the above 238 | 239 | **Answer: 4** -------------------------------------------------------------------------------- /13-for Loop.md: -------------------------------------------------------------------------------- 1 | # for Loop in C Programming 2 | Video Link: [https://youtu.be/WgS_SF1VrEk](https://youtu.be/WgS_SF1VrEk) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-for-loop](https://www.programiz.com/c-programming/c-for-loop) 4 | 5 | **Syntax** : 6 | 7 | ```c 8 | for (initializationExpression; testExpression; updateExpression) { 9 | // code inside the for loop 10 | } 11 | 12 | ``` 13 | 14 | ### Example 1 : print numbers from 0 to 9 15 | ```c 16 | #include 17 | 18 | int main() { 19 | 20 | for (int i = 0; i < 10; i++) { 21 | printf("%d ", i); 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | ``` 28 | 29 | **Output** 30 | ``` 31 | 0 1 2 3 4 5 6 7 8 9 32 | 33 | ``` 34 | 35 | ### Example 2 : print a message multiple times 36 | ```c 37 | #include 38 | 39 | int main() { 40 | 41 | for (int i = 0; i < 10; i++) { 42 | printf("Emergency condition \n"); 43 | } 44 | 45 | return 0; 46 | } 47 | 48 | ``` 49 | 50 | **Output** 51 | ``` 52 | Emergency condition 53 | Emergency condition 54 | Emergency condition 55 | Emergency condition 56 | Emergency condition 57 | Emergency condition 58 | Emergency condition 59 | Emergency condition 60 | Emergency condition 61 | Emergency condition 62 | 63 | ``` 64 | ### Sum of all the numbers from 1 to 100 65 | 66 | ```c 67 | #include 68 | 69 | int main() { 70 | 71 | int sum = 0; 72 | 73 | for (int i = 1; i <= 100; i++) { 74 | sum = sum + i; 75 | } 76 | 77 | printf("Sum: %d", sum); 78 | return 0; 79 | } 80 | 81 | ``` 82 | **Output** 83 | ``` 84 | Sum: 5050 85 | ``` 86 | 87 | ### Sum of Even Numbers 88 | 89 | ```c 90 | #include 91 | 92 | int main() { 93 | 94 | int sum = 0; 95 | 96 | for (int i = 2; i <= 100; i = i + 2) { 97 | sum = sum + i; 98 | } 99 | 100 | printf("Sum: %d", sum); 101 | return 0; 102 | } 103 | 104 | ``` 105 | **Output** 106 | ``` 107 | Sum: 2550 108 | 109 | ``` 110 | --- 111 | 112 | ## Programming Task 113 | 114 | Q. Can you use the for loop to compute the sum of only odd numbers from 1 to 100. In each iteration, you need to add an odd number to the sum. 115 | 116 | 117 | sum = sum + 1 118 | sum = sum + 3 119 | sum = sum + 5 120 | .... 121 | .... 122 | sum = sum + 99 123 | 124 | ## Solution : 125 | ```c 126 | #include 127 | 128 | int main() { 129 | int sum = 0; 130 | for(int i = 1; i <= 100; i = i+2) { 131 | sum = sum + i; 132 | } 133 | printf("Sum of odd numbers from 1 to 100: %d", sum); 134 | 135 | return 0; 136 | } 137 | ``` 138 | **Output** 139 | ``` 140 | Sum of odd numbers from 1 to 100: 2500 141 | ``` 142 | --- 143 | 144 | ## Programiz Quiz 145 | 146 | Q. In the for loop below, which is the update statement? 147 | 148 | ```c 149 | for (int i = 1; i < 5; i++) {...} 150 | ``` 151 | 152 | **Options:** 153 | 1. int i = 1 154 | 155 | 1. i < 5 156 | 157 | 1. i++ 158 | 159 | 1. for 160 | 161 | **Answer: 3** -------------------------------------------------------------------------------- /14. break and continue.md: -------------------------------------------------------------------------------- 1 | # break and continue in C Programming 2 | Video Link: [https://youtu.be/DpPf9XskST8](https://youtu.be/DpPf9XskST8) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-break-continue-statement](https://www.programiz.com/c-programming/c-break-continue-statement) 4 | 5 | 6 | ### break inside the loop 7 | ```c 8 | #include 9 | 10 | int main() { 11 | 12 | for (int i = 1; i <= 5; i++) { 13 | printf("%d \n", i); 14 | break; 15 | } 16 | 17 | return 0; 18 | } 19 | 20 | ``` 21 | 22 | **Output** 23 | ``` 24 | 1 25 | 26 | ``` 27 | 28 | ### break statement with decision making statement 29 | ```c 30 | #include 31 | 32 | int main() { 33 | 34 | for (int i = 1; i <= 5; i++) { 35 | 36 | if (i == 3) { 37 | break; 38 | } 39 | 40 | printf("%d \n", i); 41 | } 42 | 43 | return 0; 44 | } 45 | 46 | ``` 47 | 48 | **Output** 49 | ``` 50 | 1 51 | 2 52 | ``` 53 | ## break with while loop 54 | 55 | ```c 56 | #include 57 | 58 | int main() { 59 | 60 | while (1) { 61 | int number; 62 | printf("Enter a number: "); 63 | scanf("%d", &number); 64 | 65 | if (number < 0) { 66 | break; 67 | } 68 | 69 | printf("%d \n", number); 70 | } 71 | 72 | return 0; 73 | } 74 | 75 | ``` 76 | **Output** 77 | ``` 78 | Enter a number: 5 79 | 5 80 | Enter a number: 9 81 | 9 82 | Enter a number: -4 83 | 84 | ``` 85 | 86 | ## continue Statement 87 | ```c 88 | #include 89 | 90 | int main() { 91 | 92 | for (int i = 1; i <= 5; i++) { 93 | 94 | if (i == 3) { 95 | continue; 96 | } 97 | 98 | printf("%d \n", i); 99 | } 100 | 101 | return 0; 102 | } 103 | 104 | ``` 105 | **Output** 106 | ``` 107 | 1 108 | 2 109 | 4 110 | 5 111 | 112 | ``` 113 | ## Example: break and continue 114 | ```c 115 | #include 116 | 117 | int main() { 118 | 119 | while (1) { 120 | int number; 121 | printf("Enter a number: "); 122 | scanf("%d", &number); 123 | 124 | if (number <= 0) { 125 | break; 126 | } 127 | 128 | if ((number % 2) != 0) { 129 | continue; 130 | } 131 | 132 | printf("%d \n", number); 133 | } 134 | 135 | return 0; 136 | } 137 | ``` 138 | **Output** 139 | ``` 140 | Enter a number: 4 141 | 4 142 | Enter a number: 7 143 | Enter a number: 32 144 | 32 145 | Enter a number: -3 146 | 147 | ``` 148 | 149 | 150 | --- 151 | 152 | ## Programming Task 153 | 154 | Q. Can you write a program that takes an input from the user and prints it if the value is a negative odd number? 155 | - if the input value is positive, end the loop with message, Positive Value. 156 | - And, if the input value is negative even, skip the value with message, Negative Even. 157 | 158 | So, our output will look like : 159 | 160 | Enter a number: -23 161 | -23 162 | Enter a number: -22 163 | Negative Even 164 | Enter a number: 23 165 | 166 | 167 | ## Solution : 168 | ```c 169 | #include 170 | 171 | int main() { 172 | 173 | while(1) { 174 | 175 | int number; 176 | printf("\nEnter any number: "); 177 | scanf("%d", &number); 178 | 179 | if (number < 0 && number % 2 != 0) { 180 | printf("%d", number); 181 | } 182 | 183 | else if (number >= 0) { 184 | printf("Positive Value"); 185 | break; 186 | } 187 | 188 | else { 189 | printf("Negative Even"); 190 | continue; 191 | } 192 | } 193 | 194 | return 0; 195 | } 196 | ``` 197 | **Output** 198 | ``` 199 | Enter any number: -8 200 | Negative Even 201 | Enter any number: -3 202 | -3 203 | Enter any number: 8 204 | Positive Value 205 | ``` 206 | --- 207 | 208 | ## Programiz Quiz 209 | 210 | Q. Which of the following keywords is used to skip the current iteration of a loop ? 211 | 212 | 213 | **Options:** 214 | 1. skip 215 | 216 | 1. continue 217 | 218 | 1. break 219 | 220 | 1. loop 221 | 222 | **Answer: 2** -------------------------------------------------------------------------------- /15. C functions.md: -------------------------------------------------------------------------------- 1 | # C Functions 2 | Video Link: [https://youtu.be/Npo1u37lcg8](https://youtu.be/Npo1u37lcg8) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-functions](https://www.programiz.com/c-programming/c-functions) 4 | 5 | **Syntax** 6 | 7 | ``` 8 | returnType functionName() { 9 | ... 10 | ... 11 | } 12 | ``` 13 | 14 | ### Funtion : Example 1 15 | ```c 16 | #include 17 | 18 | void greet() { 19 | printf("Good Morning\n"); 20 | } 21 | 22 | int main() { 23 | 24 | greet(); 25 | greet(); 26 | greet(); 27 | 28 | return 0; 29 | } 30 | 31 | ``` 32 | 33 | **Output** 34 | ``` 35 | Good Morning 36 | Good Morning 37 | Good Morning 38 | 39 | ``` 40 | 41 | ## Function Parameters 42 | ### Example : 1 43 | ```c 44 | #include 45 | 46 | void calculateSquare(int number) { 47 | int square = number * number; 48 | printf("Square of %d is %d \n", number, square); 49 | } 50 | 51 | int main() { 52 | 53 | calculateSquare(5); 54 | 55 | return 0; 56 | } 57 | 58 | ``` 59 | 60 | **Output** 61 | ``` 62 | Square of 5 is 25 63 | ``` 64 | ### Example : 2 65 | 66 | ```c 67 | #include 68 | 69 | void addNumbers(int number1, int number2) { 70 | int sum = number1 + number2; 71 | printf("Sum of %d and %d is %d", number1, number2, sum); 72 | } 73 | 74 | 75 | int main() { 76 | 77 | addNumbers(8, 9); 78 | 79 | return 0; 80 | } 81 | 82 | 83 | ``` 84 | **Output** 85 | ``` 86 | Sum of 8 and 9 is 17 87 | ``` 88 | 89 | ## Return Types 90 | ```c 91 | #include 92 | 93 | int addNumbers(int number1, int number2) { 94 | int sum = number1 + number2; 95 | return sum; 96 | } 97 | 98 | 99 | int main() { 100 | 101 | int result = addNumbers(8, 9); 102 | printf("Result = %d", result); 103 | 104 | return 0; 105 | } 106 | 107 | ``` 108 | **Output** 109 | ``` 110 | Result = 17 111 | 112 | ``` 113 | ## Function Prototype 114 | ```c 115 | #include 116 | 117 | int addNumbers(int number1, int number2); 118 | 119 | int main() { 120 | 121 | int result = addNumbers(8, 9); 122 | printf("Result = %d", result); 123 | 124 | return 0; 125 | } 126 | 127 | int addNumbers(int number1, int number2) { 128 | int sum = number1 + number2; 129 | return sum; 130 | printf("After Return"); 131 | } 132 | 133 | ``` 134 | **Output** 135 | ``` 136 | Result = 17 137 | ``` 138 | 139 | ## Standard Library Function 140 | ```c 141 | #include 142 | #include 143 | 144 | int main() { 145 | 146 | float result = sqrt(25); 147 | 148 | printf("Square Root is %f", result); 149 | 150 | return 0; 151 | } 152 | 153 | ``` 154 | 155 | **Output** 156 | ``` 157 | Square Root is 5.00000 158 | ``` 159 | 160 | --- 161 | 162 | ## Programming Task 163 | 164 | Can you create a function that 165 | - takes two numbers 166 | - performs the multiplication of two numbers 167 | - returns the result 168 | 169 | --- 170 | 171 | ## Programiz Quiz 172 | 173 | Q. What is the correct way to call the following function? 174 | 175 | ```c 176 | int test(double num1, int num2) { 177 | ... 178 | } 179 | ``` 180 | 181 | **Options:** 182 | 1. test(8.3, 2); 183 | 184 | 1. test (2, 8.3); 185 | 186 | 1. int result = test(8.3, 2); 187 | 188 | 1. int result = test(2, 8.3); 189 | 190 | **Answer: 3** -------------------------------------------------------------------------------- /16. C variable scoope and storage class.md: -------------------------------------------------------------------------------- 1 | # C Variable Scope and Storage Class 2 | Video Link: [https://youtu.be/ej-GOnj7mj0](https://youtu.be/ej-GOnj7mj0) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-storage-class](https://www.programiz.com/c-programming/c-storage-class) 4 | 5 | ## Local Scope 6 | ```c 7 | #include 8 | 9 | int addNumbers(int number1, int number2) { 10 | int result = number1 + number2; 11 | return result; 12 | } 13 | 14 | int main() { 15 | 16 | int sum = addNumbers(5, 6); 17 | printf("Result = %d", sum); 18 | 19 | return 0; 20 | } 21 | 22 | ``` 23 | 24 | **Output** 25 | ``` 26 | Result = 11 27 | ``` 28 | 29 | ## Global Scope 30 | 31 | ```c 32 | #include 33 | 34 | int result; 35 | 36 | void addNumbers(int number1, int number2) { 37 | result = number1 + number2; 38 | } 39 | 40 | int main() { 41 | 42 | addNumbers(5, 6); 43 | printf("Result = %d", result); 44 | 45 | return 0; 46 | } 47 | 48 | ``` 49 | **Output** 50 | ``` 51 | Result = 11 52 | ``` 53 | 54 | --- 55 | 56 | ## Programiz Quiz 57 | 58 | Q. In the following code, which variable is a global variable? 59 | ```c 60 | #include 61 | 62 | double discount = 33.3; 63 | 64 | int computeFee(int time) { 65 | double discountAmount; 66 | double fee = 33600.0; 67 | } 68 | 69 | ``` 70 | 71 | **Options:** 72 | 1. time 73 | 74 | 1. discount 75 | 76 | 1. fee 77 | 78 | 1. discountAmount 79 | 80 | **Answer: 2** -------------------------------------------------------------------------------- /17. C Standard Library Function.md: -------------------------------------------------------------------------------- 1 | # C Standard Library Functions 2 | Video Link: [https://youtu.be/OJvwk3pLK34](https://youtu.be/OJvwk3pLK34) 3 | Tutorial Link: [https://www.programiz.com/c-programming/library-function 4 | ](https://www.programiz.com/c-programming/library-function 5 | ) 6 | 7 | ## Math Header File 8 | **Example 1:** 9 | ```c 10 | #include 11 | #include 12 | 13 | int main() { 14 | 15 | int num = 25; 16 | printf("Square root is %lf", sqrt(num)); 17 | 18 | return 0; 19 | } 20 | 21 | ``` 22 | 23 | **Output** 24 | ``` 25 | Square root is 5.000000 26 | ``` 27 | 28 | **Example 2:** 29 | ```c 30 | #include 31 | #include 32 | 33 | int main() { 34 | 35 | int num = 27; 36 | printf("Cube root is %lf", cbrt(num)); 37 | 38 | return 0; 39 | } 40 | ``` 41 | 42 | **Output** 43 | ``` 44 | Cube root is 3.000000 45 | ``` 46 | **Example 3:** 47 | ```c 48 | #include 49 | #include 50 | 51 | int main() { 52 | 53 | int a = 5; 54 | int b = 2; 55 | 56 | double result = pow(a, b); 57 | printf("Power: %lf", result); 58 | 59 | return 0; 60 | } 61 | ``` 62 | **Output** 63 | ``` 64 | Power: 25.000000 65 | ``` 66 | 67 | ## Ctype Header File 68 | ```c 69 | #include 70 | #include 71 | 72 | int main() { 73 | 74 | char alph = 'e'; 75 | 76 | char upper = toupper(alph); 77 | printf("%c", upper); 78 | 79 | char lower = tolower(upper); 80 | printf("\n%c", lower); 81 | 82 | return 0; 83 | } 84 | 85 | ``` 86 | 87 | **Output** 88 | ``` 89 | E 90 | e 91 | ``` 92 | --- 93 | ## Programming Task 94 | Create a program that computes the result of a number raised to the power of the square root of a number. So first. 95 | - Take input from user 96 | - Compute the square root of a number using sqrt() 97 | - Compute the power of the number raised to the power of its square root 98 | - Print the result 99 | 100 | ## Solution : 101 | ```c 102 | #include 103 | #include 104 | 105 | int main() { 106 | double num; 107 | 108 | printf("Enter a number: "); 109 | scanf("%lf", &num); 110 | 111 | double square = sqrt(num); 112 | double power = pow(num, square); 113 | 114 | printf("The result is: %.2lf", power); 115 | 116 | return 0; 117 | } 118 | ``` 119 | **Output** 120 | ``` 121 | Enter a number: 9 122 | The result is: 729.000000 123 | ``` 124 | --- 125 | 126 | ## Programiz Quiz 127 | 128 | Q. What is the correct way to include a library file in our program? 129 | 130 | 131 | **Options:** 132 | 1. Math.sqrt() 133 | 134 | 1. #include 135 | 136 | 1. #include Math.sqrt() 137 | 138 | 1. math.sqrt() 139 | 140 | **Answer: 2** -------------------------------------------------------------------------------- /18. C Recursion.md: -------------------------------------------------------------------------------- 1 | # C Recursion 2 | Video Link: [https://youtu.be/61T0cZ8KyGM](https://youtu.be/61T0cZ8KyGM) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-recursion 4 | ](https://www.programiz.com/c-programming/c-recursion 5 | ) 6 | 7 | ## Example: C Recursion 8 | 9 | ```c 10 | #include 11 | int sum(int n); 12 | 13 | int main() { 14 | int number, result; 15 | 16 | printf("Enter a positive integer: "); 17 | scanf("%d", &number); 18 | 19 | result = sum(number); 20 | 21 | printf("sum = %d", result); 22 | return 0; 23 | } 24 | 25 | int sum(int n) { 26 | if (n != 0) { 27 | // sum() function calls itself 28 | return n + sum(n-1); 29 | } 30 | else { 31 | return n; 32 | } 33 | } 34 | 35 | ``` 36 | 37 | **Output** 38 | ``` 39 | Enter a positive integer: 4 40 | sum = 10 41 | 42 | ``` 43 | 44 | 45 | --- 46 | ## Programming Task 47 | Create a program that computes the factorial of a number. 48 | - Take an input number from the user 49 | - Create a function that takes the number as a parameter. 50 | - Inside the function check if the number is greater than 0. 51 | - If true, return number multiplied by a recursive call to the function with parameter 1 less than number 52 | - Else, return 1 53 | 54 | ## Solution : 55 | ```c 56 | #include 57 | 58 | int fact (int n); 59 | 60 | int main() { 61 | 62 | int number; 63 | printf("Enter any number: "); 64 | scanf("%d", &number); 65 | 66 | if(number > 0) { 67 | int result = fact(number); 68 | printf("The Factorial is : %d", result); 69 | } 70 | else { 71 | printf("Enter positive value"); 72 | } 73 | 74 | return 0; 75 | } 76 | 77 | int fact (int n) { 78 | 79 | if(n == 1 || n==0) { 80 | return 1; 81 | } 82 | else { 83 | return n * fact(n-1); 84 | } 85 | 86 | } 87 | 88 | ``` 89 | **Output** 90 | ``` 91 | Enter any number: 7 92 | The Factorial is : 5040 93 | ``` 94 | --- 95 | 96 | ## Programiz Quiz 97 | 98 | Q. Which of the following statements is false? 99 | 100 | **Options:** 101 | 1. A function that calls itself is called recursive function 102 | 1. Conditional statement prevents infinite recursive call 103 | 1. Recursive Function always return some value 104 | 1. None of the above 105 | 106 | **Answer: 3** 107 | 108 | 109 | -------------------------------------------------------------------------------- /19. C Array.md: -------------------------------------------------------------------------------- 1 | # C Arrays 2 | Video Link: [https://youtu.be/MOeGnamlUP4](https://youtu.be/MOeGnamlUP4) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-arrays](https://www.programiz.com/c-programming/c-arrays 4 | ) 5 | 6 | ## Syntax for array declaration 7 | 8 | ``` 9 | datatype arrayName[arraySize]; 10 | 11 | ``` 12 | ## Assign Value to Array 13 | ```c 14 | #include 15 | 16 | int main() { 17 | 18 | int age[5] = {21, 29, 25, 32, 17}; 19 | 20 | printf("%d ", age[0]); 21 | printf("%d ", age[1]); 22 | printf("%d ", age[2]); 23 | printf("%d ", age[3]); 24 | printf("%d ", age[4]); 25 | 26 | return 0; 27 | } 28 | 29 | ``` 30 | **Output** 31 | ``` 32 | 21 29 25 32 17 33 | ``` 34 | ## Assign Values using index number 35 | 36 | ```c 37 | #include 38 | 39 | int main() { 40 | 41 | int age[5]; 42 | 43 | printf("Enter 5 input values: "); 44 | 45 | scanf("%d", &age[0]); 46 | scanf("%d", &age[1]); 47 | scanf("%d", &age[2]); 48 | scanf("%d", &age[3]); 49 | scanf("%d", &age[4]); 50 | 51 | printf("%d ", age[0]); 52 | printf("%d ", age[1]); 53 | printf("%d ", age[2]); 54 | printf("%d ", age[3]); 55 | printf("%d ", age[4]); 56 | 57 | return 0; 58 | } 59 | 60 | ``` 61 | **Output** 62 | ``` 63 | Enter 5 input values: 21 64 | 29 65 | 25 66 | 32 67 | 17 68 | 21 29 25 32 17 69 | 70 | ``` 71 | 72 | ## Change Array Elements 73 | ```c 74 | #include 75 | 76 | int main() { 77 | 78 | int age[5] = {21, 29, 25, 32, 17}; 79 | 80 | age[2] = 26; 81 | printf("%d ", age[2]); 82 | 83 | return 0; 84 | } 85 | ``` 86 | **Output** 87 | ``` 88 | 26 89 | 90 | ``` 91 | 92 | ## Loop and Array 93 | ```c 94 | #include 95 | 96 | int main() { 97 | 98 | int age[5]; 99 | 100 | printf("Enter 5 input values: "); 101 | 102 | for (int i = 0; i < 5; ++i) { 103 | scanf("%d", &age[i]); 104 | } 105 | 106 | for (int i = 0; i < 5; ++i) { 107 | printf("%d ", age[i]); 108 | } 109 | 110 | return 0; 111 | } 112 | ``` 113 | **Output** 114 | ``` 115 | Enter 5 input values: 44 116 | 3 117 | 53 118 | 52 119 | 66 120 | 44 3 53 52 66 121 | ``` 122 | 123 | ## Index Out of Bound Error 124 | ```c 125 | #include 126 | 127 | int main() { 128 | 129 | int age[5]; 130 | 131 | printf("Enter 5 input values: "); 132 | 133 | for (int i = 0; i < 5; ++i) { 134 | scanf("%d", &age[i]); 135 | } 136 | 137 | for (int i = 0; i < 5 6; ++i) { 138 | printf("%d ", age[i]); 139 | } 140 | 141 | return 0; 142 | } 143 | 144 | ``` 145 | **Output** 146 | ``` 147 | Enter 5 input values: 12 148 | 13 149 | 14 150 | 15 151 | 16 152 | 12 13 14 15 16 32765 153 | ``` 154 | --- 155 | ## Programming Task 156 | Create a program that computes the average marks of a student. 157 | - Create an array that stores the marks of 5 subjects 158 | - Compute the total marks by adding all the marks 159 | - Divide the total marks by total number to subjects 160 | - Print the average marks 161 | 162 | 163 | ## Solution : 164 | ```c 165 | #include 166 | 167 | int main() { 168 | int i; 169 | double marks[5]; 170 | printf("Enter marks:\n"); 171 | 172 | for(i = 0; i < 5; i++) { 173 | scanf("%lf", &marks[i]); 174 | } 175 | 176 | double sum = 0; 177 | for(i = 0; i < 5; i++) { 178 | sum = marks[i] + sum; 179 | } 180 | 181 | double avg_marks = sum/5; 182 | printf("The average marks of a student: %.2lf", avg_marks); 183 | 184 | return 0; 185 | } 186 | ``` 187 | **Output** 188 | ``` 189 | Enter marks: 190 | 50 191 | 34 192 | 33 193 | 23.7 194 | 12.1 195 | The average marks of a student: 30.56 196 | ``` 197 | --- 198 | 199 | ## Programiz Quiz 200 | 201 | Q. Which value will we get when we print num[4] from the following array? 202 | ```c 203 | int num[5] = {2, 3, 5}; 204 | ``` 205 | 206 | **Options:** 207 | 1. 2 208 | 1. 5 209 | 1. 0 210 | 1. Random Value 211 | 212 | 213 | **Answer: 3** 214 | 215 | 216 | -------------------------------------------------------------------------------- /2-C-Variables.md: -------------------------------------------------------------------------------- 1 | # C Variables 2 | 3 | Video Link: [https://youtu.be/h4VBpylsjJc](https://youtu.be/h4VBpylsjJc) 4 | Tutorial Link: [https://www.programiz.com/c-programming/c-variables-constants](https://www.programiz.com/c-programming/c-variables-constants) 5 | 6 | 7 | ## Print Line of Text 8 | 9 | ```c 10 | #include 11 | 12 | int main() { 13 | 14 | int age = 25; 15 | printf("C Programming"); 16 | 17 | return 0; 18 | } 19 | ``` 20 | 21 | **Output** 22 | 23 | ``` 24 | C Programming 25 | ``` 26 | 27 | --- 28 | ## Print Variable 29 | 30 | ```c 31 | #include 32 | 33 | int main() { 34 | 35 | int age = 25; 36 | printf("%d", age); 37 | 38 | return 0; 39 | } 40 | ``` 41 | 42 | **Output** 43 | 44 | ``` 45 | 25 46 | ``` 47 | 48 | --- 49 | ## Print Text and Variable Together 50 | 51 | ```c 52 | #include 53 | 54 | int main() { 55 | 56 | int age = 25; 57 | printf("Age: %d", age); 58 | 59 | return 0; 60 | } 61 | ``` 62 | 63 | **Output** 64 | 65 | ``` 66 | Age: 25 67 | ``` 68 | 69 | --- 70 | ## Change Value of Variable 71 | 72 | ```c 73 | #include 74 | 75 | int main() { 76 | 77 | int age = 25; 78 | printf("Age: %d ", age); 79 | 80 | age = 31; 81 | printf("New age: %d", age); 82 | 83 | return 0; 84 | } 85 | ``` 86 | 87 | **Output** 88 | 89 | ``` 90 | Age: 25 New age: 31 91 | ``` 92 | 93 | --- 94 | ## Change Value of Variable Example 2 95 | 96 | ```c 97 | #include 98 | 99 | int main() { 100 | 101 | int age = 25; 102 | printf("Age: %d", age); 103 | 104 | age = 31; 105 | printf("\nNew age value: %d", age); 106 | 107 | return 0; 108 | } 109 | ``` 110 | 111 | **Output** 112 | 113 | ``` 114 | Age: 25 115 | New age value: 31 116 | ``` 117 | 118 | --- 119 | ## Assign one variable to another variale 120 | 121 | ```c 122 | #include 123 | 124 | int main() { 125 | 126 | int firstNumber = 33; 127 | printf("first Number = %d ", firstNumber); 128 | 129 | int secondNumber = firstNumber; 130 | printf("\nsecond Number = %d", secondNumber); 131 | 132 | return 0; 133 | } 134 | ``` 135 | 136 | **Output** 137 | 138 | ``` 139 | first Number = 33 140 | second Number = 33 141 | ``` 142 | 143 | ## Declare Multiple Variables at once 144 | 145 | ```c 146 | #include 147 | 148 | int main() { 149 | int variable1, variable2; 150 | return 0; 151 | } 152 | ``` 153 | 154 | ```c 155 | #include 156 | 157 | int main() { 158 | int variable1, variable2 = 25; 159 | return 0; 160 | } 161 | ``` 162 | 163 | --- 164 | ## Programiz Quiz 165 | 166 | **Q. Can you guess the output of this program?** 167 | 168 | ```c 169 | #include 170 | 171 | int main() { 172 | 173 | int number1 = 33; 174 | printf("%d ", number1); 175 | printf("%d", number2); 176 | 177 | return 0; 178 | } 179 | ``` 180 | 181 | **Options** 182 | a. 33 183 | b. 3333 184 | c. 33 33 185 | d. '33.0' 186 | 187 | 188 | **Correct Answer: c** -------------------------------------------------------------------------------- /20. C Multidimensional arrays.md: -------------------------------------------------------------------------------- 1 | # C Multidimensional Arrays 2 | Video Link: [https://youtu.be/ATA6dYDz954](https://youtu.be/ATA6dYDz954) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-multi-dimensional-arrays 4 | ](https://www.programiz.com/c-programming/c-multi-dimensional-arrays 5 | ) 6 | 7 | ### Declaration multidimensional array 8 | 9 | ``` 10 | int arr[2][3]; 11 | 12 | ``` 13 | ### Initialization of multidimensional array 14 | ``` 15 | int arr[2][3] = { {1, 3, 5}, {2, 4, 6} }; 16 | 17 | 18 | ``` 19 | 20 | ## Access Array Elements 21 | 22 | ```c 23 | #include 24 | 25 | int main() { 26 | 27 | int arr[2][3] = { {1, 3, 5}, {2, 4, 6} }; 28 | 29 | printf("%d\n", arr[0][0]); 30 | printf("%d", arr[1][2]); 31 | 32 | return 0; 33 | } 34 | 35 | ``` 36 | **Output** 37 | ``` 38 | 1 39 | 6 40 | ``` 41 | 42 | ## Change Array Indexes 43 | ```c 44 | #include 45 | 46 | int main() { 47 | 48 | int arr[2][3] = { {1, 3, 5}, {2, 4, 6} }; 49 | 50 | arr[0][2] = 7; 51 | arr[1][1] = 8; 52 | 53 | printf("%d\n", arr[0][2]); 54 | printf("%d", arr[1][1]); 55 | 56 | return 0; 57 | } 58 | 59 | ``` 60 | **Output** 61 | ``` 62 | 7 63 | 8 64 | ``` 65 | 66 | ## Multidimensional Array and for loop 67 | 68 | ```c 69 | #include 70 | 71 | int main() { 72 | 73 | int arr[2][3] = { {1, 3, 5}, {2, 4, 6} }; 74 | 75 | for (int i = 0; i < 2; ++i) { 76 | for(int j = 0; j < 3; ++j) { 77 | printf("%d ", arr[i][j]); 78 | } 79 | printf("\n"); 80 | } 81 | 82 | return 0; 83 | } 84 | 85 | 86 | ``` 87 | **Output** 88 | ``` 89 | 1 3 5 90 | 2 4 6 91 | ``` 92 | --- 93 | 94 | --- 95 | 96 | ## Programiz Quiz 97 | 98 | Q. Which value will we get when we print arr[1][4] from the following array? 99 | ```c 100 | int arr[1][4] = { {3, 6, 9, 12}, {2, 4, 6, 8} }; 101 | ``` 102 | 103 | **Options:** 104 | 1. 82 105 | 1. 5 106 | 1. 0 107 | 1. Random Value 108 | 109 | 110 | **Answer: 4** 111 | 112 | 113 | -------------------------------------------------------------------------------- /21. C strings.md: -------------------------------------------------------------------------------- 1 | # C Strings 2 | Video Link: [https://youtu.be/l7zI3nswO1g](https://youtu.be/l7zI3nswO1g) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-strings 4 | ](https://www.programiz.com/c-programming/c-strings 5 | ) 6 | 7 | ## C Strings 8 | 9 | ```c 10 | #include 11 | 12 | int main() { 13 | 14 | char str[] = "Programiz"; 15 | printf("%s", str); 16 | 17 | return 0; 18 | } 19 | ``` 20 | **Output** 21 | ``` 22 | Programiz 23 | ``` 24 | 25 | ## String Input 26 | **Example 1:** 27 | ```c 28 | #include 29 | 30 | int main() { 31 | 32 | char str[20]; 33 | 34 | printf("Enter your name: "); 35 | scanf("%s", str); 36 | 37 | printf("%s", str); 38 | 39 | return 0; 40 | } 41 | ``` 42 | **Output** 43 | ``` 44 | Enter your name: Padma Manandhar 45 | Padma 46 | ``` 47 | **Example 2:** 48 | ```c 49 | #include 50 | 51 | int main() { 52 | 53 | char str[20]; 54 | 55 | printf("Enter your name: "); 56 | fgets(str, sizeof(str), stdin); 57 | 58 | printf("%s", str); 59 | 60 | return 0; 61 | } 62 | ``` 63 | 64 | **Output** 65 | ``` 66 | Enter your name: Padma Manandhar 67 | Padma Manandhar 68 | 69 | ``` 70 | ## Access Characters of a String 71 | 72 | ```c 73 | #include 74 | 75 | int main() { 76 | char str[] = "Programiz"; 77 | 78 | printf("%c\n", str[0]); 79 | printf("%c\n", str[1]); 80 | printf("%c\n", str[2]); 81 | printf("%c", str[3]); 82 | 83 | return 0; 84 | } 85 | ``` 86 | **Output** 87 | ``` 88 | P 89 | r 90 | o 91 | g 92 | ``` 93 | 94 | ## Change Characters of String 95 | ```c 96 | #include 97 | 98 | int main() { 99 | char str[] = "Programiz"; 100 | 101 | str[2] = 'O'; 102 | str[4] = 'R'; 103 | 104 | printf("%s", str); 105 | 106 | return 0; 107 | } 108 | 109 | ``` 110 | **Output** 111 | ``` 112 | PrOgRamiz 113 | ``` 114 | 115 | ## Programming Task 116 | Create a program that takes your full name as input and prints your name. Then, change the first letter of your name to X. 117 | - If your name is John Williams, it will become Xohn Williams. 118 | - If your name is Julie Bing, it will become Xulie Bing. 119 | 120 | **Solution** 121 | ```c 122 | #include 123 | 124 | int main() { 125 | char name[30]; 126 | 127 | printf("Enter your full name: "); 128 | fgets(name, sizeof(name), stdin); 129 | 130 | name[0] = 'X'; 131 | printf("%s", name); 132 | 133 | return 0; 134 | } 135 | ``` 136 | 137 | **Output** 138 | ``` 139 | Enter your full name: John Williams 140 | Xohn Williams 141 | 142 | ``` 143 | --- 144 | 145 | ## Programiz Quiz 146 | 147 | Q. What will be the value of the name variable if we provide Jack Sparrow as input value? 148 | 149 | ```c 150 | char name[20]; 151 | scanf("%s", &name); 152 | ``` 153 | 154 | **Options:** 155 | 1. Jack Sparrow 156 | 1. Sparrow 157 | 1. Jack 158 | 1. Error 159 | 160 | 161 | **Answer: 3** 162 | 163 | 164 | -------------------------------------------------------------------------------- /22. C String Functions.md: -------------------------------------------------------------------------------- 1 | # C String Functions 2 | Video Link: [https://youtu.be/XdnmsKUvGsc](https://youtu.be/XdnmsKUvGsc) 3 | Tutorial Link: [https://www.programiz.com/c-programming/string-handling-functions](https://www.programiz.com/c-programming/string-handling-functions) 4 | 5 | ## C String 6 | 7 | ```c 8 | #include 9 | 10 | int main() { 11 | 12 | char language[] = "C Programming"; 13 | 14 | printf("%s", language); 15 | 16 | return 0; 17 | } 18 | ``` 19 | **Output** 20 | ``` 21 | C Programming 22 | ``` 23 | 24 | ## 1. strlen() Function 25 | ```c 26 | #include 27 | #include 28 | 29 | int main() { 30 | 31 | char language[] = "C Programming"; 32 | 33 | printf("%s", language); 34 | 35 | printf("\nLength: %zu", strlen(language)); 36 | 37 | return 0; 38 | } 39 | ``` 40 | **Output** 41 | ``` 42 | C Programming 43 | Length: 13 44 | 45 | ``` 46 | ## 2. strcpy() Function 47 | 48 | ```c 49 | #include 50 | #include 51 | 52 | int main() { 53 | char food[] = "Pizza"; 54 | 55 | char bestFood[strlen(food)]; 56 | 57 | strcpy(bestFood, food); 58 | 59 | printf("%s", bestFood); 60 | 61 | return 0; 62 | } 63 | ``` 64 | **Output** 65 | ``` 66 | Pizza 67 | ``` 68 | 69 | ## 3. strcat() Function 70 | ```c 71 | #include 72 | #include 73 | 74 | int main() { 75 | 76 | char text1[] = "Hey, "; 77 | char text2[] = "How are you?"; 78 | 79 | strcat(text1, text2); 80 | 81 | printf("%s", text1); 82 | 83 | return 0; 84 | } 85 | 86 | ``` 87 | **Output** 88 | ``` 89 | Hey, How are you? 90 | ``` 91 | ## 4. strcmp() Function 92 | ```c 93 | #include 94 | #include 95 | 96 | int main() { 97 | 98 | char text1[] = "abcd"; 99 | char text2[] = "cdef"; 100 | 101 | int result = strcmp(text1, text2); 102 | 103 | printf("%d", result); 104 | 105 | return 0; 106 | } 107 | 108 | ``` 109 | **Output** 110 | ``` 111 | -2 112 | ``` 113 | 114 | 115 | ## Programming Task 116 | Create a program to compare two strings and print the larger strings.Here is how the program should work: 117 | - Get two string input from the user using fgets() 118 | - Compare the length of both the strings using strlen() 119 | - Print the larger string 120 | 121 | **Solution** 122 | ```c 123 | #include 124 | #include 125 | 126 | int main() { 127 | 128 | char food1[20]; 129 | char food2[20]; 130 | 131 | printf("Enter the first food: "); 132 | fgets(food1, sizeof(food1), stdin); 133 | 134 | printf("Enter the second food: "); 135 | fgets(food2, sizeof(food2), stdin); 136 | 137 | if (strlen(food1) > strlen(food2)) { 138 | printf("Longest Food Name: %s", food1); 139 | } 140 | else { 141 | printf("Longest Food Name: %s\n", food2); 142 | } 143 | 144 | return 0; 145 | } 146 | ``` 147 | 148 | **Output** 149 | ``` 150 | Enter the first food: Coffee 151 | Enter the second food: Tea 152 | Longest Food Name: Coffee 153 | Length: 7 154 | 155 | ``` 156 | --- 157 | 158 | ## Programiz Quiz 159 | 160 | Q. Which of the following functions is used to join two strings? 161 | 162 | 163 | **Options:** 164 | 1. strjoin() 165 | 1. strcat() 166 | 1. join() 167 | 1. cat() 168 | 169 | 170 | **Answer: 2** 171 | 172 | 173 | -------------------------------------------------------------------------------- /23. C Pointers.md: -------------------------------------------------------------------------------- 1 | # C Pointers 2 | Video Link: [https://youtu.be/KGhacRRMnDw](https://youtu.be/KGhacRRMnDw) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-pointers](https://www.programiz.com/c-programming/c-pointers) 4 | 5 | ## Pointer Variables 6 | 7 | ```c 8 | #include 9 | 10 | int main() { 11 | 12 | int age = 25; 13 | 14 | printf("%p", &age); 15 | 16 | int* ptr = &age; 17 | 18 | printf("\n%p", ptr); 19 | 20 | return 0; 21 | } 22 | 23 | ``` 24 | **Output** 25 | ``` 26 | 0x7ffc875fe0ec 27 | 0x7ffc875fe0ec 28 | ``` 29 | 30 | ## Access Value using Pointer 31 | ```c 32 | #include 33 | 34 | int main() { 35 | 36 | int age = 25; 37 | 38 | int* ptr = &age; 39 | 40 | printf("Address: %p\n", ptr); 41 | 42 | printf("Value: %d", *ptr); 43 | 44 | return 0; 45 | } 46 | 47 | ``` 48 | **Output** 49 | ``` 50 | Address: 0x7fff7e4319bc 51 | Value: 25 52 | 53 | 54 | ``` 55 | ## Change Value using Pointer 56 | 57 | ```c 58 | #include 59 | 60 | int main() { 61 | 62 | int age = 25; 63 | 64 | int* ptr = &age; 65 | 66 | *ptr = 31; 67 | 68 | printf("%d", age); 69 | 70 | return 0; 71 | } 72 | 73 | ``` 74 | **Output** 75 | ``` 76 | 31 77 | ``` 78 | --- 79 | 80 | ## Programming Task 81 | Create a program to change the value of a variable using a pointer. Here's how the program should work: 82 | - Get input value for a double variable salary. 83 | - Assign the address of salary to a double pointer. 84 | 85 | Now use the pointer to 86 | - print the value of salary, 87 | - increase the salary by 2 times, 88 | - print the new salary. 89 | 90 | **Solution** 91 | ```c 92 | #include 93 | 94 | int main() { 95 | double salary; 96 | 97 | printf("Enter your salary: "); 98 | scanf("%lf", &salary); 99 | 100 | double* ptr = &salary; 101 | 102 | printf("Your Salary: %.2lf\n", *ptr); 103 | 104 | double new_salary = *ptr * 2; 105 | 106 | printf("Your double salary: %.2lf", new_salary); 107 | } 108 | ``` 109 | 110 | **Output** 111 | ``` 112 | Enter your salary: 28000.5 113 | Your Salary: 28000.50 114 | Your double Salary: 56001.00 115 | ``` 116 | --- 117 | 118 | ## Programiz Quiz 119 | 120 | Q. Which of the following is valid for variable a and pointer p? 121 | 122 | **Options:** 123 | 1. *p = &a 124 | 1. p = a 125 | 1. *p = a 126 | 1. p = *a 127 | 128 | 129 | **Answer: 3** 130 | 131 | 132 | -------------------------------------------------------------------------------- /24. C pointers and arrays. C pointers and arrays.md: -------------------------------------------------------------------------------- 1 | # C Pointers and Arrays 2 | Video Link: [https://youtu.be/LscgaBzlGdE](https://youtu.be/LscgaBzlGdE) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-pointers-arrays](https://www.programiz.com/c-programming/c-pointers-arrays) 4 | 5 | ## C Pointers and Array 6 | 7 | ```c 8 | ##include 9 | 10 | int main() { 11 | 12 | int numbers[5] = {1, 3, 5, 7, 9}; 13 | 14 | for (int i = 0; i < 5; ++i) { 15 | printf("%d = %p\n", numbers[i], &numbers[i]); 16 | } 17 | 18 | return 0; 19 | } 20 | 21 | ``` 22 | **Output** 23 | ``` 24 | 1 = 0x7ffce02ace30 25 | 3 = 0x7ffce02ace34 26 | 5 = 0x7ffce02ace38 27 | 7 = 0x7ffce02ace3c 28 | 9 = 0x7ffce02ace40 29 | 30 | ``` 31 | ## Using name of the array to print the address 32 | ```c 33 | #include 34 | 35 | int main() { 36 | 37 | int numbers[5] = {1, 3, 5, 7, 9}; 38 | 39 | for (int i = 0; i < 5; ++i) { 40 | printf("%d = %p\n", numbers[i], numbers+i); 41 | } 42 | 43 | return 0; 44 | } 45 | 46 | ``` 47 | **Output** 48 | ``` 49 | 1 = 0x7fff55603c30 50 | 3 = 0x7fff55603c34 51 | 5 = 0x7fff55603c38 52 | 7 = 0x7fff55603c3c 53 | 9 = 0x7fff55603c40 54 | 55 | ``` 56 | ## Access array elements using pointer 57 | 58 | ```c 59 | #include 60 | 61 | int main() { 62 | 63 | int numbers[5] = {1, 3, 5, 7, 9}; 64 | 65 | for (int i = 0; i < 5; ++i) { 66 | printf("%d = %p\n", *(numbers+i), numbers+i); 67 | } 68 | 69 | return 0; 70 | } 71 | 72 | ``` 73 | **Output** 74 | ``` 75 | 1 = 0x7ffd2398cc80 76 | 3 = 0x7ffd2398cc84 77 | 5 = 0x7ffd2398cc88 78 | 7 = 0x7ffd2398cc8c 79 | 9 = 0x7ffd2398cc90 80 | 81 | ``` 82 | 83 | ## Change array elements using pointers 84 | 85 | ```c 86 | #include 87 | 88 | int main() { 89 | 90 | int numbers[5] = {1, 3, 5, 7, 9}; 91 | 92 | *numbers = 2; 93 | *(numbers+4) = 11; 94 | 95 | printf("First Element: %d\n", *numbers); 96 | printf("Last Element: %d", *(numbers+4)); 97 | 98 | return 0; 99 | } 100 | 101 | ``` 102 | **Output** 103 | ``` 104 | First Element: 2 105 | Last Element: 11 106 | 107 | ``` 108 | --- 109 | 110 | ## Programming Task 111 | Create a program to find the largest element of an array using pointers. 112 | 113 | int arr[] = {34, 12, 21, 54, 48}; 114 | 115 | - assign the first element of the array to a largest variable using pointer 116 | int largest = *arr 117 | 118 | - Run a for loop to access each element of the array. 119 | - Compare largest with each array element using pointer 120 | largest < *(arr + i) 121 | - If the largest variable is smaller than a element, assign the array value to largest 122 | largest = *(arr + i) 123 | 124 | **Solution** 125 | ```c 126 | #include 127 | 128 | int main() { 129 | int arr[5] = {34, 12, 21, 54, 48}; 130 | 131 | int largest = *arr; 132 | 133 | for(int i = 1; i < 5; i++) { 134 | if(largest < *(arr + i)) { 135 | largest = *(arr + i); 136 | } 137 | } 138 | printf("The largest number is: %d", largest); 139 | 140 | return 0; 141 | } 142 | ``` 143 | 144 | **Output** 145 | ``` 146 | The largest number is: 54 147 | ``` 148 | --- 149 | 150 | ## Programiz Quiz 151 | 152 | Q. In an array, int ages[5], what does ages represent? 153 | 154 | **Options:** 155 | 1. ages[4] 156 | 1. ages[0] 157 | 1. &ages[4] 158 | 1. &ages[0] 159 | 160 | **Answer: 4** 161 | 162 | 163 | -------------------------------------------------------------------------------- /25. C Pointers and Functions.md: -------------------------------------------------------------------------------- 1 | # C Pointers and Functions 2 | Video Link: [https://youtu.be/JYHpD9huNR4](https://youtu.be/JYHpD9huNR4) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-pointer-functions](https://www.programiz.com/c-programming/c-pointer-functions) 4 | 5 | ## C Pointers and Functions 6 | 7 | ```c 8 | #include 9 | 10 | void findValue(int* num) { 11 | 12 | *num = 39; 13 | } 14 | 15 | int main() { 16 | 17 | int number = 21; 18 | 19 | findValue(&number); 20 | 21 | printf("Number: %d", number); 22 | 23 | return 0; 24 | } 25 | 26 | ``` 27 | **Output** 28 | ``` 29 | Number: 39 30 | 31 | ``` 32 | ## Find the square of a number 33 | ```c 34 | #include 35 | 36 | void findSquare(int* number) { 37 | 38 | int square = *number * *number; 39 | 40 | *number = square; 41 | } 42 | 43 | int main() { 44 | 45 | int number = 21; 46 | 47 | findSquare(&number); 48 | 49 | printf("Square is %d", number); 50 | 51 | return 0; 52 | } 53 | 54 | ``` 55 | **Output** 56 | ``` 57 | Square is 441 58 | 59 | ``` 60 | ## Return pointers from a function 61 | 62 | ```c 63 | #include 64 | 65 | int* findSquare(int* number) { 66 | 67 | int square = *number * *number; 68 | 69 | *number = square; 70 | 71 | return number; 72 | } 73 | 74 | int main() { 75 | 76 | int number = 21; 77 | 78 | int* result = findSquare(&number); 79 | 80 | printf("Result is %d", *result); 81 | 82 | return 0; 83 | } 84 | 85 | ``` 86 | **Output** 87 | ``` 88 | Result is 441 89 | 90 | ``` 91 | ## Add two numbers 92 | 93 | ```c 94 | #include 95 | 96 | int* addNumbers(int* num1, int* num2, int* sum) { 97 | 98 | *sum = *num1 + *num2; 99 | 100 | return sum; 101 | } 102 | 103 | int main() { 104 | 105 | int number1 = 32; 106 | int number2 = 18; 107 | 108 | int sum; 109 | 110 | int* result = addNumbers(&number1, &number2, &sum); 111 | 112 | printf("Sum is %d", *result); 113 | 114 | return 0; 115 | } 116 | 117 | ``` 118 | **Output** 119 | ``` 120 | Sum is 50 121 | 122 | ``` 123 | --- 124 | 125 | ## Programming Task 126 | Create a program to find the multiplication of two numbers using a function and pointers. 127 | 128 | Here's how your program should work: 129 | 130 | - Create a function that accepts three pointers. 131 | 132 | - Inside the function multiply values represented by two pointers and assign the result to the address of the third pointer. 133 | 134 | - Inside the main function, create three variables, two variables with values 13 and 9 and the third variable to store their product. 135 | 136 | - Call the function with addresses of the 3 variables as arguments. 137 | 138 | - Store the returned value inside a pointer and print the value pointed by the returned address. 139 | 140 | **Solution** 141 | ```c 142 | #include 143 | 144 | int* multiplication (int* a, int* b, int* c) { 145 | *c = *a * *b; 146 | return c; 147 | } 148 | 149 | int main() { 150 | 151 | int num1 = 13; 152 | int num2 = 9; 153 | int multiple; 154 | 155 | int* result = multiplication(&num1, &num2, &multiple); 156 | 157 | printf("The multiplication is: %d", *result); 158 | 159 | return 0; 160 | } 161 | ``` 162 | 163 | **Output** 164 | ``` 165 | The multiplication is: 117 166 | ``` 167 | --- 168 | 169 | ## Programiz Quiz 170 | 171 | Q. What type of value does the following function return? 172 | 173 | **Options:** 174 | 1. Integer value 175 | 1. Address of integer variable 176 | 1. Double value 177 | 1. Address of double variable 178 | 179 | **Answer: 4** 180 | 181 | 182 | -------------------------------------------------------------------------------- /26. C Struct.md: -------------------------------------------------------------------------------- 1 | # C Struct 2 | Video Link: [https://youtu.be/gt9YPl6O9ZM](https://youtu.be/gt9YPl6O9ZM) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-structures](https://www.programiz.com/c-programming/c-structures) 4 | 5 | ## C Struct 6 | 7 | ```c 8 | #include 9 | 10 | struct Person { 11 | double salary; 12 | int age; 13 | }; 14 | 15 | int main() { 16 | 17 | struct Person person1; 18 | 19 | person1.age = 25; 20 | person1.salary = 4321.78; 21 | printf("Age of person1: %d\n", person1.age); 22 | printf("Salary of person1: %.2lf", person1.salary); 23 | 24 | return 0; 25 | } 26 | 27 | ``` 28 | **Output** 29 | ``` 30 | Age of person1: 25 31 | Salary of person1: 4321.78 32 | 33 | ``` 34 | **Create the struct variable and initialize members in the same line** 35 | ```c 36 | #include 37 | 38 | struct Person { 39 | double salary; 40 | int age; 41 | }; 42 | 43 | int main() { 44 | 45 | struct Person person1 = {.age = 25, .salary = 4321.78}; 46 | struct Person person2 = {.age = 31, .salary = 78943.2}; 47 | 48 | printf("Age of person1: %d\n", person1.age); 49 | printf("Salary of person1: %.2lf", person1.salary); 50 | 51 | printf("\nAge of person2: %d\n", person2.age); 52 | printf("Salary of person2: %.2lf", person2.salary); 53 | 54 | return 0; 55 | } 56 | 57 | ``` 58 | **Output** 59 | ``` 60 | Age of person1: 25 61 | Salary of person1: 4321.78 62 | Age of person2: 31 63 | Salary of person2: 78943.20 64 | ``` 65 | ## typedef in Struct 66 | 67 | ```c 68 | #include 69 | 70 | typedef struct Person { 71 | double salary; 72 | int age; 73 | } person; 74 | 75 | int main() { 76 | 77 | person person1; 78 | 79 | person1.age = 25; 80 | person1.salary = 4321.78; 81 | printf("Age of person1: %d\n", person1.age); 82 | printf("Salary of person1: %.2lf", person1.salary); 83 | 84 | return 0; 85 | } 86 | 87 | ``` 88 | **Output** 89 | ``` 90 | Age of person1: 25 91 | Salary of person1: 4321.78 92 | 93 | ``` 94 | ## Sum of Complex Numbers 95 | 96 | ```c 97 | #include 98 | 99 | typedef struct Complex { 100 | double real; 101 | double imagine; 102 | } complex; 103 | 104 | int main() { 105 | 106 | complex c1 = {.real = 21.87, .imagine = 30}; 107 | complex c2 = {.real = 13.34, .imagine = 112.23}; 108 | 109 | complex sum; 110 | 111 | sum.real = c1.real + c2.real; 112 | sum.imagine = c1.imagine + c2.imagine; 113 | 114 | printf("Result is %.2lf + %.2lfi", sum.real, sum.imagine); 115 | 116 | return 0; 117 | } 118 | 119 | ``` 120 | **Output** 121 | ``` 122 | Result is 35.21 + 142.23i 123 | ``` 124 | --- 125 | 126 | ## Programming Task 127 | Create a program to find the differences between three complex numbers. 128 | 129 | Perform the subtraction between complex numbers by subtracting the real part of one complex number from other complex numbers and same for the imaginary part too. 130 | 131 | **Solution** 132 | ```c 133 | #include 134 | 135 | typedef struct Complex { 136 | double real; 137 | double imagine; 138 | } complex; 139 | 140 | int main() { 141 | 142 | complex c1 = {.real = 28.24, .imagine = 40}; 143 | complex c2 = {.real = 36.3, .imagine = 19.1}; 144 | complex subtract; 145 | 146 | subtract.real = c1.real - c2.real; 147 | subtract.imagine = c1.imagine - c2.imagine; 148 | 149 | printf("Result is %.2lf + %.2lfi", subtract.real, subtract.imagine); 150 | 151 | return 0; 152 | } 153 | ``` 154 | 155 | **Output** 156 | ``` 157 | Result is -8.06 + 20.90i 158 | ``` 159 | --- 160 | 161 | ## Programiz Quiz 162 | 163 | Q. What is the name of the variable of the following struct? 164 | 165 | struct Test { 166 | double testScore; 167 | int testCount; 168 | } firstTest; 169 | 170 | 171 | **Options:** 172 | 1. testScore 173 | 1. struct 174 | 1. firstTest 175 | 1. Test 176 | 177 | **Answer: 3** 178 | 179 | 180 | -------------------------------------------------------------------------------- /27. C enum.md: -------------------------------------------------------------------------------- 1 | # Enumerations in C 2 | Video Link: [https://youtu.be/-N212LgGqik](https://youtu.be/-N212LgGqik) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-enumeration](https://www.programiz.com/c-programming/c-enumeration) 4 | 5 | ## Integral Constants 6 | 7 | ```c 8 | #include 9 | 10 | enum Size { 11 | Small, 12 | Medium, 13 | Large, 14 | ExtraLarge 15 | }; 16 | 17 | int main() { 18 | 19 | enum Size shoeSize; 20 | 21 | shoeSize = ExtraLarge; 22 | 23 | printf("%d", shoeSize); 24 | 25 | return 0; 26 | } 27 | 28 | ``` 29 | **Output** 30 | ``` 31 | 3 32 | 33 | ``` 34 | **Change value from ExtraLarge to Small** 35 | ```c 36 | #include 37 | 38 | enum Size { 39 | Small, 40 | Medium, 41 | Large, 42 | ExtraLarge 43 | }; 44 | 45 | int main() { 46 | 47 | enum Size shoeSize; 48 | 49 | shoeSize = Small; 50 | 51 | printf("%d", shoeSize); 52 | 53 | return 0; 54 | } 55 | 56 | ``` 57 | **Output** 58 | ``` 59 | 0 60 | ``` 61 | ## Change value of Integral Constants 62 | 63 | ```c 64 | #include 65 | 66 | enum Size { 67 | Small = 27, 68 | Medium = 31, 69 | Large = 35, 70 | ExtraLarge = 40 71 | }; 72 | 73 | int main() { 74 | 75 | enum Size shoeSize1 = Small; 76 | enum Size shoeSize2 = Medium; 77 | enum Size shoeSize3 = Large; 78 | enum Size shoeSize4 = ExtraLarge; 79 | 80 | printf("%d\n", shoeSize1); 81 | printf("%d\n", shoeSize2); 82 | printf("%d\n", shoeSize3); 83 | printf("%d", shoeSize4); 84 | 85 | return 0; 86 | } 87 | 88 | ``` 89 | **Output** 90 | ``` 91 | 27 92 | 31 93 | 35 94 | 40 95 | ``` 96 | 97 | **Create enum variables while defining the num** 98 | 99 | ```c 100 | #include 101 | 102 | enum Size { 103 | Small, 104 | Medium, 105 | Large, 106 | ExtraLarge 107 | } shoeSize; 108 | 109 | int main() { 110 | 111 | shoeSize = Medium; 112 | printf("%d", shoeSize); 113 | 114 | return 0; 115 | } 116 | 117 | ``` 118 | **Output** 119 | ``` 120 | 1 121 | ``` 122 | 123 | --- 124 | 125 | ## Programming Task 126 | Create an enum with enum constants Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday. 127 | 128 | Inside the main function() 129 | 130 | - Create two enum variables named weekend1 and weekend2. 131 | - Assign values Sunday and Saturday to the variables respectively. 132 | - Print the values of weekend1 and weekend2. 133 | 134 | **Solution** 135 | ```c 136 | #include 137 | 138 | enum Day { 139 | Sunday, 140 | Monday, 141 | Tuesday, 142 | Wednesday, 143 | Thursday, 144 | Friday, 145 | Saturday 146 | }; 147 | 148 | int main() { 149 | 150 | enum Day weekend1 = Sunday; 151 | enum Day weekend2 = Saturday; 152 | 153 | printf("%d \n%d", weekend1, weekend2); 154 | 155 | return 0; 156 | } 157 | ``` 158 | 159 | **Output** 160 | ``` 161 | 0 162 | 6 163 | ``` 164 | --- 165 | 166 | ## Programiz Quiz 167 | 168 | Q. What is the name of the variable in the following enum? 169 | 170 | enum Weekends { 171 | Friday, 172 | Saturday, 173 | Sunday 174 | } currentDay; 175 | 176 | **Options:** 177 | 1. enum 178 | 1. Weekends 179 | 1. currentDay 180 | 1. Friday 181 | 182 | **Answer: 3** 183 | 184 | 185 | -------------------------------------------------------------------------------- /28. Dynamic Memory Allocation.md: -------------------------------------------------------------------------------- 1 | # Dynamic Memory Allocation 2 | Video Link: [https://youtu.be/Dn87Bna23TQ](https://youtu.be/Dn87Bna23TQ) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-dynamic-memory-allocation](https://www.programiz.com/c-programming/c-dynamic-memory-allocation) 4 | 5 | ## malloc() and free() Function 6 | 7 | ```c 8 | #include 9 | 10 | int main() { 11 | 12 | int n = 4; 13 | 14 | int* ptr; 15 | 16 | ptr = (int*) malloc(n * sizeof(int)); 17 | 18 | if (ptr == NULL) { 19 | printf("Memory cannot be allocated"); 20 | return 0; 21 | } 22 | 23 | printf("Enter input values:\n"); 24 | for(int i = 0; i < n; ++i) { 25 | scanf("%d", ptr + i); 26 | } 27 | 28 | printf("Input Values\n"); 29 | for(int i = 0; i < n; ++i) { 30 | printf("%d\n", *(ptr + i)); 31 | } 32 | 33 | free(ptr); 34 | 35 | return 0; 36 | } 37 | ``` 38 | **Output** 39 | ``` 40 | Enter input values: 41 | 12 42 | 21 43 | 13 44 | 9 45 | Input Values 46 | 12 47 | 21 48 | 13 49 | 9 50 | ``` 51 | ## realloc() Function 52 | 53 | ```c 54 | #include 55 | 56 | int main() { 57 | 58 | int n = 4; 59 | 60 | int* ptr; 61 | 62 | ptr = (int*) malloc(n * sizeof(int)); 63 | 64 | if (ptr == NULL) { 65 | printf("Memory cannot be allocated"); 66 | return 0; 67 | } 68 | 69 | printf("Allocated Memory\n"); 70 | for (int i = 0; i < n; ++i) { 71 | printf("%p\n", ptr + i); 72 | } 73 | 74 | n = 6; 75 | 76 | ptr = realloc (ptr, n * sizeof(int)); 77 | 78 | printf("Newly Allocated Memory\n"); 79 | for(int i = 0; i < n; ++i) { 80 | printf("%p\n", ptr + i); 81 | } 82 | 83 | return 0; 84 | } 85 | ``` 86 | **Output** 87 | ``` 88 | Allocated Memory 89 | 0x559442b682a0 90 | 0x559442b682a4 91 | 0x559442b682a8 92 | 0x559442b682ac 93 | Newly Allocated Memory 94 | 0x559442b682a0 95 | 0x559442b682a4 96 | 0x559442b682a8 97 | 0x559442b682ac 98 | 0x559442b682b0 99 | 0x559442b682b4 100 | ``` 101 | --- 102 | 103 | ## Programming Task 104 | Create a program that dynamically changes the size of an array. 105 | 106 | - Create a pointer array like this 107 | int* ages; 108 | - Allocate 4 spaces for the array using the malloc() function 109 | - Get 4 input values for the array. 110 | - Print the array elements using the pointer. 111 | - Resize the array to store 6 elements using realloc() 112 | - Assign 32 and 59 as the 5th and 6th elements. 113 | Hint 114 | ages[4] = 32; 115 | ages[5] = 49; 116 | - Then finally Print all 6 array elements. 117 | 118 | **Solution** 119 | ```c 120 | #include 121 | 122 | int main() { 123 | 124 | int* ages; 125 | int n = 4; 126 | 127 | ages = (int*)malloc(n * sizeof(int)); 128 | 129 | if(ages == NULL) { 130 | printf("Memory cannot be allocated"); 131 | return 0; 132 | } 133 | 134 | printf("Enter input values:\n"); 135 | for (int i = 0; i < n; ++i) { 136 | scanf("%d", ages+i); 137 | } 138 | 139 | printf("Input Values are:\n"); 140 | for (int i = 0; i < n; ++i) { 141 | printf("%d\n", *(ages + i)); 142 | } 143 | 144 | n = 6; 145 | 146 | ages[4] = 32; 147 | ages[5] = 49; 148 | 149 | ages = realloc(ages, n * sizeof(int)); 150 | 151 | printf("After reallocation:\n"); 152 | for(int i = 0; i < n; ++i) { 153 | printf("%d\n", *(ages+i)); 154 | } 155 | 156 | free(ages); 157 | return 0; 158 | } 159 | 160 | ``` 161 | 162 | **Output** 163 | ``` 164 | Enter input values: 165 | 11 166 | 14 167 | 13 168 | 15 169 | Input Values are: 170 | 11 171 | 14 172 | 13 173 | 15 174 | After reallocation: 175 | 11 176 | 14 177 | 13 178 | 15 179 | 32 180 | 49 181 | 182 | ``` 183 | --- 184 | 185 | ## Programiz Quiz 186 | 187 | Q. What is the correct way to allocate memory to store 12 double data? 188 | 189 | **Options:** 190 | 1. malloc(20 * double) 191 | 1. (double*) malloc(12 * sizeof(double)) 192 | 1. (double) malloc(20 * sizeof(double)) 193 | 1. (double*) malloc( sizeof(20 * int)) 194 | 195 | **Answer: 2** 196 | 197 | 198 | -------------------------------------------------------------------------------- /29. File Handling.md: -------------------------------------------------------------------------------- 1 | # File Handling 2 | Video Link: [https://youtu.be/MQIF-WMUOL8](https://youtu.be/MQIF-WMUOL8) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-file-input-output](https://www.programiz.com/c-programming/c-file-input-output) 4 | 5 | ## Open a File 6 | ```c 7 | #include 8 | 9 | int main() { 10 | 11 | FILE* fptr; 12 | 13 | fptr = fopen("test.txt", "r"); 14 | 15 | if (fptr != NULL) { 16 | printf("File Open Successful"); 17 | } 18 | else { 19 | printf("File Open Unsuccessful"); 20 | } 21 | 22 | return 0; 23 | } 24 | 25 | ``` 26 | **Output** 27 | ``` 28 | File Open Successful 29 | ``` 30 | ## Read File Content 31 | 32 | ```c 33 | #include 34 | 35 | int main() { 36 | 37 | FILE* fptr; 38 | 39 | fptr = fopen("file.txt", "r"); 40 | 41 | char content[1000]; 42 | 43 | if(fptr != NULL) { 44 | 45 | while (fgets(content, 1000, fptr)) { 46 | printf("%s", content); 47 | } 48 | 49 | } 50 | else { 51 | printf("File opening is unsuccessful"); 52 | } 53 | 54 | return 0; 55 | } 56 | ``` 57 | **Output** 58 | ``` 59 | This is the text file. 60 | C Programming File Handling is awesome. 61 | 62 | ``` 63 | 64 | ## Closing File 65 | 66 | ```c 67 | #include 68 | 69 | int main() { 70 | 71 | FILE* fptr; 72 | 73 | fptr = fopen("file.txt", "r"); 74 | 75 | char content[1000]; 76 | 77 | if(fptr != NULL) { 78 | 79 | while (fgets(content, 1000, fptr)) { 80 | printf("%s", content); 81 | } 82 | 83 | } 84 | else { 85 | printf("File opening is unsuccessful"); 86 | } 87 | 88 | fclose(fptr); 89 | 90 | return 0; 91 | } 92 | ``` 93 | **Output** 94 | ``` 95 | This is the text file. 96 | C Programming File Handling is awesome. 97 | 98 | ``` 99 | ## Write Content to File 100 | 101 | ```c 102 | #include 103 | 104 | int main() { 105 | 106 | FILE* fptr; 107 | 108 | fptr = fopen("newFile.txt", "w"); 109 | 110 | fputs("New message 1\n", fptr); 111 | fputs("New message 2", fptr); 112 | 113 | fclose(fptr); 114 | 115 | return 0; 116 | } 117 | ``` 118 | **Output** 119 | ``` 120 | You can see the content of the file is replaced by new content. 121 | 122 | ``` 123 | --- 124 | 125 | ## Programming Task 126 | 127 | - Create a new file in write mode and add content 128 | - C is a fun programming language. 129 | - And, I love using C language 130 | - Close the file 131 | - Again open the file in read mode and read the content of the file 132 | 133 | **Solution** 134 | ```c 135 | #include 136 | 137 | int main() { 138 | 139 | FILE* fileptr; 140 | 141 | fileptr = fopen("file.txt", "w"); 142 | fputs("C is a fun programming language\n", fileptr); 143 | fputs("I love using C language", fileptr); 144 | 145 | fclose(fileptr); 146 | 147 | fileptr = fopen("file.txt", "r"); 148 | char content[1000]; 149 | 150 | if(fileptr != NULL) { 151 | while(fgets(content, 1000, fileptr)) { 152 | printf("%s", content); 153 | } 154 | } 155 | 156 | else { 157 | printf("File reading is unsuccessful"); 158 | } 159 | 160 | fclose(fileptr); 161 | 162 | return 0; 163 | } 164 | 165 | ``` 166 | 167 | **Output** 168 | ``` 169 | C is a fun programming language 170 | I love using C language 171 | ``` 172 | --- 173 | 174 | ## Programiz Quiz 175 | 176 | Q. What is the correct way to declare the file pointer in C Programming? 177 | 178 | **Options:** 179 | 1. file* pointer; 180 | 1. FILE* pointer; 181 | 1. int* pointer; 182 | 1. FILE pointer; 183 | 184 | **Answer: 2** 185 | 186 | 187 | -------------------------------------------------------------------------------- /3-data-types.md: -------------------------------------------------------------------------------- 1 | # C Data Types 2 | 3 | Video Link: [https://youtu.be/sARaqR0hRI4](https://youtu.be/sARaqR0hRI4) 4 | Tutorial Link: [https://www.programiz.com/c-programming/c-data-types](https://www.programiz.com/c-programming/c-data-types) 5 | 6 | ## Data Type: int 7 | 8 | ```c 9 | #include 10 | 11 | int main() { 12 | 13 | int age = 10; 14 | printf("%d", age); 15 | 16 | return 0; 17 | } 18 | ``` 19 | 20 | **Output** 21 | 22 | ``` 23 | 10 24 | ``` 25 | 26 | --- 27 | ## Print variables and values together 28 | 29 | ```c 30 | #include 31 | 32 | int main() { 33 | 34 | int age = 10; 35 | printf("Age = %d", number); 36 | 37 | return 0; 38 | } 39 | ``` 40 | 41 | **Output** 42 | 43 | ``` 44 | Age = 10 45 | ``` 46 | 47 | --- 48 | ## Data Type: double and float 49 | 50 | ```c 51 | #include 52 | 53 | int main() { 54 | 55 | double number = 12.45; 56 | printf("%lf", number); 57 | 58 | return 0; 59 | } 60 | ``` 61 | 62 | **Output** 63 | 64 | ``` 65 | 12.450000 66 | ``` 67 | ## Print formatted double output 68 | 69 | ```c 70 | #include 71 | 72 | int main() { 73 | 74 | double number = 12.45; 75 | printf("%.2lf", number); 76 | 77 | return 0; 78 | } 79 | ``` 80 | 81 | **Output** 82 | 83 | ``` 84 | 12.45 85 | ``` 86 | --- 87 | ## Print float and double 88 | 89 | ```c 90 | #include 91 | 92 | int main() { 93 | 94 | double number = 12.45; 95 | float number1 = 10.9f; 96 | 97 | printf("%.2lf", number); 98 | printf("\n%f", number1); 99 | 100 | return 0; 101 | } 102 | ``` 103 | 104 | **Output** 105 | 106 | ``` 107 | 12.45 108 | 10.900000 109 | ``` 110 | 111 | --- 112 | ## Print formatted double and float type 113 | 114 | ```c 115 | #include 116 | 117 | int main() { 118 | 119 | double number = 12.45; 120 | float number1 = 10.9f; 121 | 122 | printf("%.2lf", number); 123 | printf("\n%.1f", number1); 124 | 125 | return 0; 126 | } 127 | ``` 128 | 129 | **Output** 130 | 131 | ``` 132 | 12.45 133 | 10.9 134 | ``` 135 | 136 | --- 137 | ## double with exponential number 138 | 139 | ```c 140 | #include 141 | 142 | int main() { 143 | 144 | double number = 5.5e6; 145 | printf("%lf", number); 146 | 147 | return 0; 148 | } 149 | ``` 150 | 151 | **Output** 152 | 153 | ``` 154 | 5500000.000000 155 | ``` 156 | 157 | --- 158 | ## Data Type: char 159 | 160 | ```c 161 | #include 162 | 163 | int main() { 164 | 165 | char character = 'z'; 166 | printf("%c", character); 167 | 168 | return 0; 169 | } 170 | ``` 171 | 172 | **Output** 173 | 174 | ``` 175 | z 176 | ``` 177 | 178 | --- 179 | ## Print numeric value of characters 180 | 181 | ```c 182 | #include 183 | 184 | int main() { 185 | 186 | char character = 'z'; 187 | printf("%c", character); 188 | printf(" %d", character); 189 | 190 | return 0; 191 | } 192 | ``` 193 | 194 | **Output** 195 | 196 | ``` 197 | z 122 198 | ``` 199 | 200 | --- 201 | ## sizeof() Data Types 202 | 203 | ```c 204 | #include 205 | 206 | int main() { 207 | 208 | int age; 209 | double number; 210 | 211 | printf("int size = %zu", sizeof (age)); 212 | printf("\ndouble size = %zu", sizeof(number)); 213 | 214 | return 0; 215 | } 216 | ``` 217 | 218 | **Output** 219 | 220 | ``` 221 | int size = 4 222 | double size = 8 223 | ``` 224 | 225 | --- 226 | ## Programiz Quiz 227 | 228 | Q. What is the output of the following code? 229 | 230 | ```c 231 | #include 232 | 233 | int main() { 234 | int a = 5; 235 | float a = 9.3; 236 | 237 | printf("%d", a); 238 | } 239 | ``` 240 | **Options** 241 | - a. 5 242 | - b. 9.3 243 | - c. 14.3 244 | - d. Error 245 | 246 | **Answer d** -------------------------------------------------------------------------------- /4.Take-Input.md: -------------------------------------------------------------------------------- 1 | # Take Input in C 2 | Video Link: [https://youtu.be/17gp5DJEyiw](https://youtu.be/17gp5DJEyiw) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-input-output](https://www.programiz.com/c-programming/c-input-output) 4 | 5 | ## Take Input 6 | ```c 7 | #include 8 | 9 | int main() { 10 | 11 | int age; 12 | 13 | scanf("%d", &age); 14 | 15 | printf("Age = %d", age); 16 | 17 | return 0; 18 | } 19 | ``` 20 | 21 | **Output** 22 | 23 | ``` 24 | 22 25 | Age = 22 26 | ``` 27 | 28 | 29 | #### Show message to ask for input 30 | 31 | ```c 32 | #include 33 | 34 | int main() { 35 | 36 | int age; 37 | 38 | printf("Enter input value: "); 39 | scanf("%d", &age); 40 | 41 | printf("Age = %d", age); 42 | 43 | return 0; 44 | } 45 | ``` 46 | 47 | **Output** 48 | 49 | ``` 50 | Enter input value: 22 51 | Age = 22 52 | ``` 53 | 54 | --- 55 | ## Take double input 56 | 57 | ```c 58 | #include 59 | 60 | int main() { 61 | 62 | double number; 63 | char alphabet; 64 | 65 | printf("Enter double value: "); 66 | scanf("%lf", &number); 67 | 68 | printf("Enter character value: "); 69 | scanf("\n%c", &alphabet); 70 | 71 | printf("Number: %lf", number); 72 | printf("\nCharacter: %c", alphabet); 73 | 74 | return 0; 75 | } 76 | ``` 77 | 78 | **Output** 79 | 80 | ``` 81 | Enter double value: 22.1 82 | Enter character value: z 83 | Number: 22.100000 84 | Character: z 85 | ``` 86 | 87 | --- 88 | ## Take multiple Input 89 | 90 | ```c 91 | #include 92 | 93 | int main() { 94 | 95 | double number; 96 | char alphabet; 97 | 98 | printf("Enter input values: "); 99 | scanf("%lf %c", &number, &alphabet); 100 | 101 | printf("Number: %lf", number); 102 | printf("\nCharacter: %c", alphabet); 103 | 104 | return 0; 105 | } 106 | ``` 107 | 108 | **Output** 109 | 110 | ``` 111 | Enter input values: 30.6 112 | c 113 | Number: 30.600000 114 | Character: c 115 | ``` 116 | 117 | --- 118 | ## Programiz Quiz 119 | Q. What is the correct way to take double input? 120 | 121 | **Options** 122 | 1. scanf("%d", &input); 123 | 2. scanf("%f", &input); 124 | 3. scanf("%lf", &input); 125 | 4. scanf("%c", &input); 126 | 127 | **Answer: 3** -------------------------------------------------------------------------------- /5-Comment.md: -------------------------------------------------------------------------------- 1 | # Take Input in C 2 | Video Link: [https://youtu.be/L2H2rtCLB-0](https://youtu.be/L2H2rtCLB-0) 3 | Tutorial Link: [https://www.programiz.com/c-programming]() 4 | 5 | ## Comments in C 6 | ```c 7 | #include 8 | 9 | int main() { 10 | 11 | // create a variable 12 | int data = 34; 13 | 14 | // print the value of data variable 15 | printf("Data = %d", data); 16 | 17 | return 0; 18 | } 19 | 20 | ``` 21 | 22 | **Output** 23 | 24 | ``` 25 | Data = 34 26 | ``` 27 | 28 | 29 | #### Comment and code can be on the same line like this 30 | 31 | 32 | ```c 33 | #include 34 | 35 | int main() { 36 | 37 | int data = 34; // create a variable 38 | 39 | printf("Data = %d", data); // print the variable 40 | 41 | return 0; 42 | } 43 | 44 | ``` 45 | 46 | **Output** 47 | 48 | ``` 49 | Data = 34 50 | 51 | ``` 52 | 53 | --- 54 | ## Prevent Executing Code Using Comments 55 | 56 | ```c 57 | #include 58 | 59 | int main() { 60 | 61 | int age; 62 | double height; 63 | 64 | printf("Enter the age: "); 65 | scanf("%d", &age); 66 | 67 | printf("Enter the height: "); 68 | scanf("%lf", &height); 69 | 70 | printf("Age = %d", age); 71 | printf("\nHeight = %.1lf", height); 72 | 73 | return 0; 74 | } 75 | 76 | ``` 77 | 78 | **Suppose, height input is not required so we will comment it out** 79 | 80 | ```c 81 | #include 82 | 83 | int main() { 84 | 85 | int age; 86 | // double height; 87 | 88 | printf("Enter the age: "); 89 | scanf("%d", &age); 90 | 91 | // printf("Enter the height: "); 92 | // scanf("%lf", &height); 93 | 94 | printf("Age = %d", age); 95 | // printf("\nHeight = %.1lf", height); 96 | 97 | return 0; 98 | } 99 | 100 | ``` 101 | **Output** 102 | 103 | ``` 104 | Enter the age: 29 105 | Age = 29 106 | 107 | ``` 108 | --- 109 | ## Multiline Comments in C Programming 110 | 111 | ```c 112 | This program takes age input from the user 113 | It stores it in the age variable 114 | And, print the value using printf() 115 | 116 | #include 117 | 118 | int main() { 119 | 120 | int age; 121 | 122 | printf("Enter the age: "); 123 | scanf("%d", &age); 124 | 125 | printf("Age = %d", age); 126 | 127 | return 0; 128 | } 129 | 130 | ``` 131 | 132 | **Multiline commenting above program** 133 | 134 | 135 | ``` 136 | /* This program takes age input from the user 137 | It stores it in the age variable 138 | And, print the value using printf() */ 139 | 140 | #include 141 | 142 | int main() { 143 | 144 | int age; 145 | 146 | printf("Enter the age: "); 147 | scanf("%d", &age); 148 | 149 | printf("Age = %d", age); 150 | 151 | return 0; 152 | } 153 | 154 | ``` 155 | **Output** 156 | 157 | ``` 158 | Enter the age: 29 159 | Age = 29 160 | 161 | ``` 162 | ## Keyboard Shortcut 163 | ### Cmd + shift + / for multiline comments 164 | 165 | ``` 166 | /* This program takes age input from the user 167 | It stores it in the age variable 168 | And, print the value using printf() */ 169 | 170 | #include 171 | 172 | int main() { 173 | 174 | int age; 175 | 176 | printf("Enter the age: "); 177 | scanf("%d", &age); 178 | 179 | printf("Age = %d", age); 180 | 181 | return 0; 182 | } 183 | 184 | ``` 185 | ### Cmd + / for singleline comment 186 | 187 | ```c 188 | // This program takes age input from the user 189 | // It stores it in the age variable 190 | // And, print the value using printf() 191 | 192 | #include 193 | 194 | int main() { 195 | 196 | int age; 197 | 198 | printf("Enter the age: "); 199 | scanf("%d", &age); 200 | 201 | printf("Age = %d", age); 202 | 203 | return 0; 204 | } 205 | 206 | ``` 207 | 208 | --- 209 | ## Programiz Quiz 210 | Q. What is the correct way to comment in C programming? 211 | 212 | **Options** 213 | 1. // 214 | 2. \# 215 | 3. \ 216 | 4. All of the above 217 | 218 | **Answer: 1** -------------------------------------------------------------------------------- /6-C Operators.md: -------------------------------------------------------------------------------- 1 | # C Operators 2 | Video Link: [https://youtu.be/_57FcSBtJNU](https://youtu.be/_57FcSBtJNU) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-operators](https://www.programiz.com/c-programming/c-operators) 4 | 5 | ## Arithmetic Operator 6 | ```c 7 | Addition + 8 | Subtraction - 9 | Multiplication * 10 | Division / 11 | Remainder % 12 | Increment ++ 13 | Decrement -- 14 | ``` 15 | ## Addition Operators 16 | #### Addition of int variables: 17 | ```c 18 | #include 19 | 20 | int main() { 21 | 22 | int x = 12; 23 | int result = x + 8; 24 | 25 | printf("%d", result); 26 | return 0; 27 | } 28 | ``` 29 | Another way: 30 | ```c 31 | #include 32 | 33 | int main() { 34 | 35 | int x = 12; 36 | 37 | printf("%d", x+8); 38 | return 0; 39 | } 40 | 41 | ``` 42 | **Output** 43 | ``` 44 | 20 45 | ``` 46 | #### Addition of double variables: 47 | ```c 48 | #include 49 | 50 | int main() { 51 | 52 | double x = 12.57; 53 | double result = x + 8.67; 54 | 55 | printf("%.2lf", result); 56 | return 0; 57 | } 58 | ``` 59 | **Output** 60 | ``` 61 | 21.24 62 | ``` 63 | #### Addition of double and int variable: 64 | ```c 65 | #include 66 | 67 | int main() { 68 | 69 | double x = 12.57; 70 | int y = 8; 71 | double result = x + y; 72 | 73 | printf("%.2lf", result); 74 | return 0; 75 | } 76 | ``` 77 | **Output** 78 | ``` 79 | 20.57 80 | ``` 81 | 82 | ## Division Operator 83 | #### Division with integer numbers: 84 | ```c 85 | #include 86 | 87 | int main() { 88 | 89 | int x = 12; 90 | int result = x / 8; 91 | 92 | printf("%d", result); 93 | return 0; 94 | } 95 | ``` 96 | 97 | **Output** 98 | 99 | ``` 100 | 1 101 | ``` 102 | #### Division with floating-point numbers: 103 | ```c 104 | #include 105 | 106 | int main() { 107 | double x = 12.00; 108 | double result = x / 8.00; 109 | 110 | printf("%.2lf", result); 111 | return 0; 112 | } 113 | ``` 114 | **Output** 115 | ``` 116 | 1.50 117 | ``` 118 | 119 | --- 120 | ## Remainder Operator 121 | 122 | ```c 123 | #include 124 | 125 | int main() { 126 | 127 | int x = 12; 128 | int result = x % 8; 129 | 130 | printf("%d", result); 131 | return 0; 132 | } 133 | ``` 134 | **Output** 135 | ``` 136 | 4 137 | ``` 138 | ## Increment and decrement Operator 139 | #### Increment Operator 140 | ```c 141 | #include 142 | 143 | int main() { 144 | 145 | int x = 12; 146 | 147 | printf("%d", ++x); 148 | return 0; 149 | } 150 | ``` 151 | **Output** 152 | ```c 153 | 13 154 | ``` 155 | #### Decrement Operator 156 | ```c 157 | #include 158 | 159 | int main() { 160 | 161 | int x = 12; 162 | 163 | printf("%d ", --x); 164 | return 0; 165 | } 166 | ``` 167 | **Output** 168 | ```c 169 | 11 170 | ``` 171 | 172 | ## Multiple Operators 173 | ```c 174 | #include 175 | 176 | int main() { 177 | 178 | int x = (4 / 2) + (6 * 5) - 1; 179 | 180 | printf(" %d", x); 181 | return 0; 182 | } 183 | ``` 184 | **Output** 185 | ```c 186 | 31 187 | ``` 188 | 189 | 190 | --- 191 | ## Programiz Quiz 192 | Q. What is the value of x in the following code? 193 | ```c 194 | int x = 5 + 2 * 9 / 3 - 3; 195 | ``` 196 | **Options** 197 | 1. 18 198 | 1. 11 199 | 1. 8 200 | 1. Error 201 | 202 | **Answer: 3** -------------------------------------------------------------------------------- /7-Type-Conversion.md: -------------------------------------------------------------------------------- 1 | # Type Conversion in C Programming 2 | Video Link: [https://youtu.be/xi2wf0Zy2Y4](https://youtu.be/xi2wf0Zy2Y4) 3 | Tutorial Link: [https://www.programiz.com/c-programming]() 4 | 5 | ### Add two integers 6 | 7 | ```c 8 | #include 9 | 10 | int main() { 11 | 12 | int a = 5; 13 | int b = 9; 14 | 15 | int result = a + b; 16 | 17 | printf("%d", result); 18 | return 0; 19 | } 20 | 21 | ``` 22 | **Output** 23 | ``` 24 | 14 25 | ``` 26 | ### char to int Conversion 27 | ```c 28 | #include 29 | 30 | int main() { 31 | 32 | char a = '5'; 33 | int b = 9; 34 | 35 | int result = a + b; 36 | 37 | printf("%d", result); 38 | return 0; 39 | } 40 | 41 | ``` 42 | **Output** 43 | ``` 44 | 62 45 | ``` 46 | ### double to int Conversion 47 | ```c 48 | #include 49 | 50 | int main() { 51 | 52 | double a = 5.67; 53 | int b = 9; 54 | 55 | int result = a + b; 56 | 57 | printf("%d", result); 58 | return 0; 59 | } 60 | 61 | ``` 62 | **Output** 63 | ``` 64 | 14 65 | ``` 66 | 67 | ### int to double Conversion 68 | ```c 69 | #include 70 | 71 | int main() { 72 | 73 | double a = 5.67; 74 | int b = 9; 75 | 76 | double result = a + b; 77 | 78 | printf("%lf", result); 79 | return 0; 80 | } 81 | 82 | ``` 83 | 84 | **Output** 85 | 86 | ``` 87 | 14.670000 88 | ``` 89 | ## Data Type Hierarchy 90 | 91 | - long double 92 | - double 93 | - float 94 | - long 95 | - int 96 | - short 97 | - char 98 | 99 | 100 | --- 101 | ## Higher data type demoted to lower data type 102 | 103 | ```c 104 | #include 105 | 106 | int main() { 107 | 108 | int a = 5.67; 109 | 110 | printf("%d", a); 111 | return 0; 112 | } 113 | 114 | ``` 115 | **Output** 116 | ``` 117 | 5 118 | ``` 119 | ## Explicit Type Conversion 120 | 121 | ### Convert double data type to int 122 | 123 | ```c 124 | #include 125 | 126 | int main() { 127 | 128 | double a = 5.67; 129 | int b = 9; 130 | 131 | double result = (int)a + b; 132 | 133 | printf("%lf", result); 134 | return 0; 135 | } 136 | 137 | ``` 138 | **Output** 139 | ```c 140 | 14.000000 141 | 142 | ``` 143 | ### Change data type of result variable to int 144 | ```c 145 | #include 146 | 147 | int main() { 148 | 149 | double a = 5.67; 150 | int b = 9; 151 | 152 | int result = (int)a + b; 153 | 154 | printf("%d", result); 155 | return 0; 156 | } 157 | 158 | ``` 159 | **Output** 160 | ```c 161 | 14 162 | ``` 163 | 164 | ### Change data type of result variable to double 165 | 166 | ```c 167 | #include 168 | 169 | int main() { 170 | 171 | int a = 9; 172 | int b = 2; 173 | 174 | double result = a / b; 175 | 176 | printf("%lf", result); 177 | return 0; 178 | } 179 | 180 | ``` 181 | **Output** 182 | ```c 183 | 4.000000 184 | 185 | ``` 186 | ### Division by converting int to double 187 | 188 | ```c 189 | #include 190 | 191 | int main() { 192 | 193 | int a = 9; 194 | int b = 2; 195 | 196 | double result = (double)a / b; 197 | 198 | printf("%lf", result); 199 | return 0; 200 | } 201 | 202 | 203 | ``` 204 | **Output** 205 | ```c 206 | 4.500000 207 | ``` 208 | 209 | 210 | 211 | --- 212 | ## Programiz Quiz 213 | Q. What is the value of result in the following statement? 214 | ```c 215 | int result = '8' + 12; 216 | 217 | ``` 218 | **Options** 219 | 1. 20 220 | 1. 68 221 | 1. Error 222 | 1. 12650 223 | 224 | **Answer: 2** -------------------------------------------------------------------------------- /8-C Boolean.md: -------------------------------------------------------------------------------- 1 | # C Booleans and Comparison Operator 2 | Video Link: [https://youtu.be/TybmJxXRV80](https://youtu.be/TybmJxXRV80) 3 | Tutorial Link: [https://www.programiz.com/c-programming]() 4 | 5 | ## C Booleans 6 | 7 | ```c 8 | #include 9 | #include 10 | 11 | int main() { 12 | 13 | bool value1 = true; 14 | bool value2 = false; 15 | 16 | printf("%d ", value1); 17 | printf(" %d", value2); 18 | 19 | return 0; 20 | } 21 | 22 | ``` 23 | **Output** 24 | ``` 25 | 1 0 26 | ``` 27 | ## Comparison Operator 28 | ```c 29 | > Greater than 30 | < Less than 31 | == Equal to 32 | >= Greater than or equal to 33 | <= Less than or equal to 34 | != Not equal to 35 | ``` 36 | ## Greater Than Operator 37 | ```c 38 | #include 39 | #include 40 | 41 | int main() { 42 | 43 | bool value = (12 > 9); 44 | 45 | printf("%d ", value); 46 | 47 | return 0; 48 | } 49 | 50 | ``` 51 | **Output** 52 | ``` 53 | 1 54 | ``` 55 | 56 | ### Change value from 12 to 9 57 | 58 | ```c 59 | #include 60 | #include 61 | 62 | int main() { 63 | 64 | bool value = (5 > 9); 65 | 66 | printf("%d ", value); 67 | 68 | return 0; 69 | } 70 | 71 | ``` 72 | **Output** 73 | ``` 74 | 0 75 | ``` 76 | 77 | ## Less than Operator 78 | ```c 79 | #include 80 | #include 81 | 82 | int main() { 83 | 84 | bool value = (5 < 9); 85 | 86 | printf("%d ", value); 87 | 88 | return 0; 89 | } 90 | 91 | ``` 92 | 93 | **Output** 94 | ``` 95 | 1 96 | ``` 97 | 98 | ### Change value from 5 to 9 99 | 100 | ```c 101 | #include 102 | #include 103 | 104 | int main() { 105 | 106 | bool value = (9 < 9); 107 | 108 | printf("%d ", value); 109 | 110 | return 0; 111 | } 112 | ``` 113 | 114 | **Output** 115 | 116 | ``` 117 | 0 118 | ``` 119 | 120 | ## Equal to Operator 121 | 122 | ```c 123 | #include 124 | #include 125 | 126 | int main() { 127 | 128 | bool value = (9 == 9); 129 | 130 | printf("%d ", value); 131 | 132 | return 0; 133 | } 134 | ``` 135 | **Output** 136 | 137 | ``` 138 | 1 139 | ``` 140 | ### Change value from 9 to 6 141 | 142 | ```c 143 | #include 144 | #include 145 | 146 | int main() { 147 | 148 | bool value = (9 == 6); 149 | 150 | printf("%d ", value); 151 | 152 | return 0; 153 | } 154 | 155 | ``` 156 | **Output** 157 | ``` 158 | 0 159 | ``` 160 | ## Not Equal to Operator 161 | 162 | ```c 163 | #include 164 | #include 165 | 166 | int main() { 167 | 168 | bool value = (9 != 6); 169 | 170 | printf("%d ", value); 171 | 172 | return 0; 173 | } 174 | ``` 175 | **Output** 176 | ``` 177 | 1 178 | ``` 179 | 180 | ## Greater Than or Equal To Operator 181 | 182 | ```c 183 | #include 184 | #include 185 | 186 | int main() { 187 | 188 | bool value = (9 >= 6); 189 | 190 | printf("%d ", value); 191 | 192 | return 0; 193 | } 194 | 195 | ``` 196 | 197 | **Output** 198 | ```c 199 | 1 200 | ``` 201 | ## Less Than or Equal To Operator 202 | 203 | ```c 204 | #include 205 | #include 206 | 207 | int main() { 208 | 209 | bool value = (9 <= 6); 210 | 211 | printf("%d ", value); 212 | 213 | return 0; 214 | } 215 | 216 | ``` 217 | **Output** 218 | ```c 219 | 0 220 | ``` 221 | 222 | ### Comparator operators with floating point numbers 223 | ```c 224 | #include 225 | #include 226 | 227 | int main() { 228 | 229 | bool value = (9.34 <= 6.87); 230 | 231 | printf("%d ", value); 232 | 233 | return 0; 234 | } 235 | 236 | ``` 237 | **Output** 238 | ```c 239 | 0 240 | ``` 241 | 242 | ## Comparison between variables 243 | 244 | ```c 245 | #include 246 | #include 247 | 248 | int main() { 249 | 250 | int num1 = 9; 251 | int num2 = 6; 252 | 253 | bool value = num1 > num2; 254 | 255 | printf("%d ", value); 256 | 257 | return 0; 258 | } 259 | ``` 260 | 261 | **Output** 262 | ```c 263 | 1 264 | ``` 265 | ### Comparison operators with a variable and value 266 | ```c 267 | #include 268 | #include 269 | 270 | int main() { 271 | 272 | int num1 = 9; 273 | 274 | bool value = num1 > 6; 275 | 276 | printf("%d ", value); 277 | 278 | return 0; 279 | } 280 | ``` 281 | 282 | **Output** 283 | ```c 284 | 1 285 | ``` 286 | ## Logical Operators in C 287 | 288 | ```c 289 | && Logical AND 290 | || Logical OR 291 | ! Logical Not 292 | ``` 293 | ## AND Operator 294 | 295 | ```c 296 | #include 297 | #include 298 | 299 | int main() { 300 | 301 | int age = 18; 302 | double height = 6.3; 303 | 304 | bool result = (age >= 18) && (height > 6.0); 305 | 306 | printf("%d ", ); 307 | 308 | return 0; 309 | } 310 | 311 | ``` 312 | **Output** 313 | ```c 314 | 1 315 | ``` 316 | ### Change the value of age to 16 317 | 318 | ```c 319 | #include 320 | #include 321 | 322 | int main() { 323 | 324 | int age = 16; 325 | double height = 6.3; 326 | 327 | bool result = (age >= 18) && (height > 6.0); 328 | 329 | printf("%d ", ); 330 | 331 | return 0; 332 | } 333 | 334 | ``` 335 | **Output** 336 | ```c 337 | 0 338 | ``` 339 | 340 | ## OR Operator 341 | 342 | ```c 343 | #include 344 | #include 345 | 346 | int main() { 347 | 348 | int age = 16; 349 | double height = 6.3; 350 | 351 | bool value = (age >= 18) || (height > 6.0); 352 | 353 | printf("%d ", value); 354 | 355 | return 0; 356 | } 357 | ``` 358 | **Output** 359 | ```c 360 | 1 361 | ``` 362 | 363 | ## NOT Operator 364 | 365 | ```c 366 | #include 367 | #include 368 | 369 | int main() { 370 | 371 | int age = 16; 372 | 373 | bool value = !(age >= 18); 374 | 375 | printf("%d ", value); 376 | 377 | return 0; 378 | } 379 | ``` 380 | **Output** 381 | ```c 382 | 1 383 | ``` 384 | 385 | ### Change the operator >= to <= 386 | 387 | ```c 388 | #include 389 | #include 390 | 391 | int main() { 392 | 393 | int age = 16; 394 | 395 | bool value = !(age <= 18); 396 | 397 | printf("%d ", value); 398 | 399 | return 0; 400 | } 401 | ``` 402 | **Output** 403 | ```c 404 | 0 405 | ``` 406 | 407 | --- 408 | ## Programiz Quiz 409 | Q. Which of the following code returns false? 410 | 411 | **Options:** 412 | 1. 9 >= 9 413 | 1. 9 > 9 414 | 1. 9 <= 9 415 | 1. 9 == 9 416 | 417 | **Answer: 2** -------------------------------------------------------------------------------- /9-If Else Statement in C.md: -------------------------------------------------------------------------------- 1 | # C Booleans and Comparison Operator 2 | Video Link: [https://youtu.be/K8mntKyBJGc](https://youtu.be/K8mntKyBJGc) 3 | Tutorial Link: [https://www.programiz.com/c-programming/c-if-else-statement](https://www.programiz.com/c-programming/c-if-else-statement) 4 | 5 | ## if Statement in C Programming 6 | 7 | ```c 8 | Syntax : 9 | 10 | if (test_condition) { 11 | // body of if statement 12 | } 13 | ``` 14 | 15 | ### Example 1: if Statement 16 | ```c 17 | #include 18 | 19 | int main() { 20 | int age; 21 | 22 | printf("Enter your age: "); 23 | scanf("%d", &age); 24 | 25 | if (age >= 18) { 26 | printf("You are eligible to vote"); 27 | } 28 | 29 | return 0; 30 | } 31 | ``` 32 | 33 | **Output** 34 | ``` 35 | Enter your age: 31 36 | You are eligible to vote 37 | ``` 38 | ### Example 2: if Statement 39 | ```c 40 | #include 41 | 42 | int main() { 43 | int age; 44 | 45 | printf("Enter your age: "); 46 | scanf("%d", &age); 47 | 48 | if (age >= 18) { 49 | printf("You are eligible to vote"); 50 | } 51 | 52 | if (age < 18) { 53 | printf("Sorry, you are not eligible to vote"); 54 | } 55 | 56 | return 0; 57 | } 58 | ``` 59 | 60 | **Output** 61 | ``` 62 | Enter your age: 15 63 | Sorry, you are not eligible to vote 64 | ``` 65 | ## if...else Statement 66 | 67 | ```c 68 | Syntax: 69 | 70 | if (test_condition) { 71 | // statements inside if body 72 | } 73 | else { 74 | // statements inside else body 75 | } 76 | ``` 77 | 78 | ### Example : if .. else Statement 79 | 80 | ```c 81 | #include 82 | 83 | int main() { 84 | 85 | int age = 15; 86 | 87 | if (age >= 18) { 88 | printf("You are eligible to vote"); 89 | } 90 | 91 | else { 92 | printf("Sorry, you are not eligible to vote"); 93 | } 94 | 95 | return 0; 96 | } 97 | ``` 98 | 99 | **Output** 100 | ``` 101 | Sorry, you are not eligible to vote 102 | ``` 103 | 104 | ## else if Statement 105 | 106 | ```c 107 | Syntax: 108 | 109 | if (test_condition1) { 110 | // statements1 111 | } 112 | else if (test_condition2){ 113 | // statements2 114 | } 115 | else { 116 | // statements3 117 | } 118 | 119 | ``` 120 | ### Example 1: else if statement 121 | 122 | ```c 123 | #include 124 | 125 | int main() { 126 | 127 | int age = 130; 128 | 129 | if (age > 120) { 130 | printf("Invalid Age"); 131 | } 132 | 133 | else if (age < 0) { 134 | printf("Invalid age"); 135 | } 136 | 137 | else if (age >= 18) { 138 | printf("You are eligible to vote"); 139 | } 140 | 141 | else { 142 | printf("Sorry, you are not eligible to vote"); 143 | } 144 | 145 | return 0; 146 | } 147 | ``` 148 | 149 | **Output** 150 | ``` 151 | Invalid Age 152 | ``` 153 | 154 | ### Example 2: else if statement 155 | 156 | ```c 157 | #include 158 | 159 | int main() { 160 | 161 | int age = -4; 162 | 163 | if (age > 120) { 164 | printf("Invalid Age"); 165 | } 166 | 167 | else if (age < 0) { 168 | printf("Invalid age"); 169 | } 170 | 171 | else if (age >= 18) { 172 | printf("You are eligible to vote"); 173 | } 174 | 175 | else { 176 | printf("Sorry, you are not eligible to vote"); 177 | } 178 | 179 | return 0; 180 | } 181 | ``` 182 | **Output** 183 | 184 | ``` 185 | Invalid age 186 | ``` 187 | ### Example 3: Using logical operator to combine the conditions 188 | 189 | ```c 190 | #include 191 | 192 | int main() { 193 | 194 | int age = -1; 195 | 196 | if (age > 120 || age < 0) { 197 | printf("Invalid Age"); 198 | } 199 | 200 | else if (age >= 18) { 201 | printf("You are eligible to vote"); 202 | } 203 | 204 | else { 205 | printf("Sorry, you are not eligible to vote"); 206 | } 207 | 208 | return 0; 209 | } 210 | 211 | ``` 212 | **Output** 213 | ``` 214 | Invalid Age 215 | ``` 216 | --- 217 | 218 | ## Programming Task 219 | 220 | Q. Can you create a program to check whether a number is positive or negative or 0? 221 | 222 | - If number is positive, print "The number is positive" 223 | - If number is negative, print "The number is negative" 224 | - (and) If number is 0, print "The number is 0" 225 | 226 | ### Solution : 227 | ```c 228 | #include 229 | 230 | int main() { 231 | 232 | double number; 233 | printf("Enter a number: "); 234 | scanf("%lf", &number); 235 | 236 | if (number > 0) { 237 | printf("The number is positive"); 238 | } 239 | else if (number < 0) { 240 | printf("The number is negative"); 241 | } 242 | else { 243 | printf("The number is 0"); 244 | } 245 | 246 | return 0; 247 | } 248 | ``` 249 | **Output** 250 | ``` 251 | Enter a number: -8 252 | The number is negative 253 | 254 | ``` 255 | --- 256 | 257 | ## Programiz Quiz 258 | Q. What is the output of the following code? 259 | 260 | ```c 261 | #include 262 | 263 | int main() { 264 | 265 | int a = 5; 266 | if (!(a % 2 == 0)) { 267 | printf("Inside If"); 268 | } 269 | else { 270 | printf("Inside else"); 271 | } 272 | return 0; 273 | } 274 | ``` 275 | **Options:** 276 | 1. Inside if 277 | 1. Inside else 278 | 1. Inside if 279 | Inside else 280 | 1. Error 281 | 282 | **Answer: 1** --------------------------------------------------------------------------------