Skip to content

Commit 08b1ab3

Browse files
committed
Add Day 14: Array of array, Typecasting, Scopes in C
1 parent 4c039ba commit 08b1ab3

File tree

7 files changed

+134
-0
lines changed

7 files changed

+134
-0
lines changed

Day 14/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Programs for Day 14
2+
3+
1. [Understand array of arrays](arrayOfArrays.c)
4+
2. [Implicit Typecasting](typecasting.c)
5+
3. [Explicity Typecasting](avgOfThreeNumbers.c)
6+
4. [Constants in C][constant.c]
7+
5. [Macros](macros.c)
8+
6. [Static variables](staticVariable.c)

Day 14/arrayOfArrays.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Program to print month name given the number (1-12)
3+
* using array of arrays concept in C
4+
*/
5+
6+
#include <stdio.h>
7+
8+
int main () {
9+
10+
// month_name is an array of arrays
11+
// it can have different length strings in each row
12+
char *month_names[] = {
13+
"January", "February", "March", "April", "May",
14+
"June", "July", "August", "September",
15+
"October", "November", "December" };
16+
printf("Please enter the month no (1-12): ");
17+
int month_no;
18+
scanf("%d", &month_no);
19+
20+
// check for validity of the input
21+
if (month_no >= 1 && month_no <= 12) {
22+
printf("Month is: %s\n", month_names[month_no - 1]);
23+
} else {
24+
printf("Invalid input\n");
25+
}
26+
27+
return 0;
28+
}

Day 14/avgOfThreeNumbers.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Program to compute avg of three numbers
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
9+
int num1, num2, num3;
10+
scanf("%d %d %d", &num1, &num2, &num3);
11+
12+
double avg = (double) (num1 + num2 + num3)/3;
13+
printf("Average of these numbers is: %f\n", avg);
14+
return 0;
15+
}

Day 14/constant.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Program to understand constants in C
3+
*/
4+
5+
#include <stdio.h>
6+
7+
// #define PI 3.141
8+
9+
int main () {
10+
11+
const float PI = 3.141;
12+
// float const PI = 3.141;
13+
int radius = 3;
14+
15+
// PI = 3.14; // it will generator an error, that constant cannot be modified
16+
float area = PI*radius*radius;
17+
printf("Area: %f\n", area);
18+
return 0;
19+
}

Day 14/macros.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Program to understand macros in C
3+
*/
4+
5+
#include <stdio.h>
6+
7+
#define AREA(l, b) (l*b)
8+
#define sqr1(y) y*y
9+
#define sqr2(y) (y)*(y)
10+
11+
int main () {
12+
13+
int l1 = 10, l2 = 5;
14+
int area = AREA(l1, l2);
15+
printf("Area: %d\n", area);
16+
17+
int macro1Res = sqr1(5+3);
18+
int macro2Res = sqr2(5+3);
19+
printf("Square of (5+3) using\nMacro 1: %d\nMacro 2: %d\n", macro1Res, macro2Res);
20+
return 0;
21+
}

Day 14/staticVariable.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Program to understand the static variables in C
3+
*/
4+
5+
6+
#include <stdio.h>
7+
8+
/* Function declaration */
9+
void display();
10+
11+
int main() {
12+
13+
display();
14+
display();
15+
display();
16+
// printf("Can we access n2: %d\n", n2);
17+
return 0;
18+
}
19+
20+
/* Function definition */
21+
void display() {
22+
int n1 = 10;
23+
// value of a static variable persists across different function calls
24+
static int n2 = 10;
25+
26+
printf("Local n1 = %d, Static n2 = %d\n", n1, n2);
27+
28+
n1++; // Increment local variable
29+
n2++; // Increment static variable
30+
}
31+

Day 14/typecasting.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Program to understand typecasting in C
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
char ch = 'A';
9+
int res = ch + 10;
10+
printf("res: %d\n", res);
11+
return 0;
12+
}

0 commit comments

Comments
 (0)