Skip to content

Commit 499501b

Browse files
committed
Add Day 9: Intro to arrays
1 parent 29ba9f7 commit 499501b

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

Diff for: Day 9/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Programs for Day 9
2+
1. [Understanding object and value contexts](pointers1.c)
3+
2. [Introduction to arrays](arrays.c)
4+
3. [Array Initialization](arrayInitialization.c)
5+
4. [Character Array](charArray.c)

Diff for: Day 9/arrayInitialization.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* An introduction to array
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
9+
// in the statement below, 5 is an initializer
10+
int ordinaryVariable = 5;
11+
12+
// in the statement below, {1, 2, 3, 4, 5} is an
13+
// initializer list
14+
int n[5] = {1, 2, 3, 4, 5};
15+
16+
for (int i = 0; i < 5; i ++) {
17+
printf("%d ", n[i]);
18+
}
19+
printf("\n");
20+
21+
// less elements in initializer list
22+
int a[5] = {1, 2, 3};
23+
for (int i = 0; i < 5; i ++) {
24+
printf("%d ", a[i]);
25+
}
26+
printf("\n");
27+
28+
// we can omit the size if we know the complete initializer list
29+
int c[] = {1, 2, 3};
30+
for (int i = 0; i < 3; i ++) {
31+
printf("%d ", c[i]);
32+
}
33+
printf("\n");
34+
35+
return 0;
36+
}

Diff for: Day 9/arrays.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* An introduction to arrays
3+
* Reverse 10 numbers entered by the user
4+
*/
5+
6+
#include <stdio.h>
7+
8+
int main () {
9+
10+
// array declaration, 10 integers which are collectively
11+
// referred by the variable n
12+
int n[10];
13+
14+
// the indexes start from 0 and till (size-1)
15+
for (int i = 0; i < 10; i ++) {
16+
scanf("%d", &n[i]);
17+
}
18+
19+
for (int i = 9; i >= 0; i --) {
20+
printf("%d ", n[i]);
21+
}
22+
23+
return 0;
24+
}

Diff for: Day 9/charArray.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Example of character array
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
9+
// specifically, null character '\0'
10+
// is required at the end of character array in C
11+
12+
/*
13+
char name[] = {'K', 'y', 'a', 'm', 'a', ' ', 'P', 'a', 'v', 'a', 'n', 'k', 'a', 'l', 'y', 'a', 'n', '\0'};
14+
*/
15+
16+
char name[] = "Kyama Pavankalyan";
17+
// print the first letter of the name
18+
printf("%c\n", name[0]);
19+
20+
// print the entire name
21+
printf("%s\n", name);
22+
return 0;
23+
}

Diff for: Day 9/pointers1.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* lvalue expression is the one that is one the left hand side
3+
* of an assignment operation
4+
* two contexts: value context and object context
5+
* value context: the expression is evaluated
6+
* object context: we get to the memory location where the object is stored
7+
* lvalue expression are in object context
8+
*/
9+
10+
#include <stdio.h>
11+
12+
int main () {
13+
14+
// integer declaration
15+
// whenever we declare a variable, it gets some memory location
16+
int x, y;
17+
18+
// &x => x is an object context
19+
// we get the location where x resides in the memory
20+
// using the & operator
21+
// but the &x as an entire expression is in value context
22+
y = &x;
23+
24+
// y will now store the address of the variable x
25+
26+
printf("%d", y);
27+
return 0;
28+
}

0 commit comments

Comments
 (0)