C_Programming_Concepts
C_Programming_Concepts
### Functions in C
A function in C is a block of code that performs a specific task and is reusable. It improves modularity and reduces code repetitio
Example:
```c
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main() {
int result = add(5, 10);
printf("Sum: %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
```
Output: `Sum: 15`
int main() {
int num = 10;
if (num > 0)
printf("Number is positive.\n");
else
printf("Number is negative.\n");
return 0;
}
```
Output: `Number is positive.`
#### Switch-Case Example
```c
#include <stdio.h>
int main() {
int choice = 2;
switch (choice) {
case 1:
printf("Choice is 1\n");
break;
case 2:
printf("Choice is 2\n");
break;
default:
printf("Invalid choice\n");
}
return 0;
}
```
Output: `Choice is 2`
int main() {
for (int i = 1; i <= 5; i++)
printf("%d ", i);
return 0;
}
```
Output: `1 2 3 4 5`
int main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
return 0;
}
```
Output: `1 2 3 4 5`
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2];
printf("Resultant Matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}
```
Output:
```
Resultant Matrix:
68
10 12
```
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2] = {0};
printf("Resultant Matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}
```
Output:
```
Resultant Matrix:
19 22
43 50
```