0% found this document useful (0 votes)
3 views

C_Programming_Concepts

The document provides an overview of key C programming concepts, including functions, branching (if-else and switch-case), and looping (for and while loops). It also covers matrix operations such as addition and multiplication using arrays, with example code snippets and their outputs. Each section illustrates how to implement these concepts in C programming effectively.

Uploaded by

pspraveen3126
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C_Programming_Concepts

The document provides an overview of key C programming concepts, including functions, branching (if-else and switch-case), and looping (for and while loops). It also covers matrix operations such as addition and multiplication using arrays, with example code snippets and their outputs. Each section illustrates how to implement these concepts in C programming effectively.

Uploaded by

pspraveen3126
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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`

### Branching and Looping in C


#### If-Else Statement
```c
#include <stdio.h>

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`

#### Looping Statements


- **For Loop Example**
```c
#include <stdio.h>

int main() {
for (int i = 1; i <= 5; i++)
printf("%d ", i);

return 0;
}
```
Output: `1 2 3 4 5`

- **While Loop Example**


```c
#include <stdio.h>

int main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
return 0;
}
```
Output: `1 2 3 4 5`

### Matrix Operations using Arrays


#### Matrix Addition
```c
#include <stdio.h>

int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2];

for (int i = 0; i < 2; i++)


for (int j = 0; j < 2; j++)
C[i][j] = A[i][j] + B[i][j];

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
```

#### Matrix Multiplication


```c
#include <stdio.h>

int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2] = {0};

for (int i = 0; i < 2; i++)


for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
C[i][j] += A[i][k] * B[k][j];

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
```

You might also like