Skip to content

Commit 9628207

Browse files
committed
Add Day 15: Structures and Structure the README.md
1 parent 08b1ab3 commit 9628207

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed

Day 15/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Day 15: Structures in C
2+
3+
1. [Declaring and using structure](structFish.c)
4+
2. [Nested structures](nestedStructures.c)
5+
3. [Updating structure fields](turtleBirthday.c)

Day 15/nestedStructures.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Program to understand
3+
* nested structures in C
4+
*/
5+
6+
#include <stdio.h>
7+
struct exercise {
8+
const char *description;
9+
float duration;
10+
};
11+
struct meal {
12+
const char *ingredients;
13+
float weight;
14+
};
15+
struct preferences {
16+
struct meal food;
17+
struct exercise exercise;
18+
};
19+
struct fish {
20+
const char *name;
21+
const char *species; int teeth;
22+
int age;
23+
struct preferences care;
24+
};
25+
26+
void label(struct fish f) {
27+
printf("Name: %s\n", f.name);
28+
printf("Species: %s\n", f.species);
29+
printf("%d years old, %d teeth\n", f.age, f.teeth);
30+
printf("Feed with %f lbs of %s ", f.care.food.weight, f.care.food.ingredients);
31+
printf("and allow to %s for %f hours.\n", f.care.exercise.description,
32+
f.care.exercise.duration);
33+
return;
34+
}
35+
int main () {
36+
// initialization has to be done in the same order
37+
// as declared in the structure
38+
struct fish snappy = {"Snappy", "Piranha", 69, 4, { {"meat", 0.2},
39+
{"swim in the jacuzzi", 7.5} } };
40+
label(snappy);
41+
return 0;
42+
}

Day 15/structFish.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Defining our own
3+
* data types using structures
4+
*/
5+
6+
#include <stdio.h>
7+
8+
struct fish {
9+
char *name;
10+
char *species;
11+
int teeth;
12+
int age;
13+
};
14+
15+
// prints all the details of a fish
16+
void catalog(struct fish f) {
17+
printf("Fish details:\n");
18+
// to access a particular field/attribute of a structure
19+
// we use the '.' operator
20+
// to access name : f.name
21+
printf("Name: %s\n", f.name);
22+
printf("Species: %s\n", f.species);
23+
printf("Teeth: %d\n", f.teeth);
24+
printf("Age: %d\n", f.age);
25+
}
26+
27+
int main () {
28+
29+
// create a variable of type fish
30+
struct fish snappy = {"Snappy", "SpecieA", 69, 13};
31+
catalog(snappy);
32+
return 0;
33+
}

Day 15/turtleBirthday.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Update values in structure fields
3+
*/
4+
5+
#include <stdio.h>
6+
7+
typedef struct {
8+
const char *name;
9+
const char *species;
10+
int age;
11+
} turtle;
12+
13+
// passing in a pointer to turtle
14+
void happy_birthday(turtle *t) {
15+
// *t.age = *t.age + 1; // THIS IS INCORRECT
16+
// BECAUSE IT EVALUATES TO *(t.age), But (t.age) is not a pointer
17+
// (*t).age = (*t).age + 1;
18+
t->age = t->age + 1;
19+
// t->age means get to the structure that t points to and
20+
// fetch the age field
21+
printf("Happy Birthday %s! You are now %i years old!\n",
22+
t->name, t->age);
23+
}
24+
25+
int main() {
26+
turtle myrtle = {"Myrtle", "Leatherback sea turtle", 99};
27+
happy_birthday(&myrtle);
28+
printf("%s's age is now %i\n", myrtle.name, myrtle.age);
29+
return 0;
30+
}
31+

README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,121 @@
11
# C-Programs
22
This repository contains the programs in C illustrated during the sessions.
3+
4+
## Day 1: Structure of C program and Variables
5+
1. [Hello world](Day 1/hello.c) program which gets completely modified to understand range of values int can store.
6+
2. [Variable declaration](Day 1/variable.c) to understand how to declare variables.
7+
3. [Addition of two numbers](Day 1/add.c) to take two numbers as input and add them!
8+
9+
## Day 2: Data types, I/O, Operators
10+
### Data Types
11+
1. int, char, float, double: [Basic Data Types](Day 2/inputOutput.c)
12+
2. Question on float type: [question1](Day 2/float.c)
13+
2. long and long long: [long](Day 2/long.c)
14+
15+
### Input and Output
16+
1. [scanf](Day 2/scanf.c)
17+
2. [printf](Day 2/printf.c)
18+
19+
### Operators
20+
#### Binary Operators
21+
1. [Arithmetic Operators](Day 2/arithmeticOperators.c)
22+
2. [Assignment Operators](Day 2/assignmentOperator.c)
23+
3. [Logical Operators](Day 2/logicalOperators.c)
24+
#### [Unary Operators](Day 2/unaryOperator.c)
25+
26+
## Day 3: Conditional statements
27+
### [Logical Operators](Day 3/logicalOperators.c)
28+
29+
### If conditional statement
30+
Simple program using if: [if](Day 3/if.c)
31+
32+
### If Else conditional statement
33+
1. Simple program using if...else: [ifElse](Day 3/ifElse.c)
34+
2. Question on finding output of if...else: [question](Day 3/question.c)
35+
36+
## Day 4: More on conditionals
37+
### If Else If ladder
38+
[Program to print grade given marks](Day 4/ifElseIf.c)
39+
40+
### Nested If Else
41+
1. [Motivation](Day 4/nestedIfElseMotivation.c)
42+
2. [Improved version of program above](Day 4/nestedIfElse.c)
43+
44+
### Switch Case statement
45+
[Program to print day of week given day number](Day 4/switch.c)
46+
47+
### Side Learnings
48+
1. [ASCII values for character](Day 4/asciiValues.c)
49+
2. [Bitwise Operators](Day 4/bitwiseOperators.c)
50+
51+
## Day 5: Getting started with Loops
52+
1. [Why do we need loops?](Day 5/loopMotivation.c)
53+
2. [For loop](Day 5/forLoop.c)
54+
3. [Exercise based on for loop](Day 5/exerciseForLoop.c)
55+
56+
## Day 6: More on Loops
57+
1. [Factorial of n using while loop](Day 6/whileLoop.c)
58+
2. [Find sum of digits of a number](Day 6/sumDigits.c)
59+
3. [do...while loop](Day 6/doWhile.c)
60+
4. [Infinite loops](Day 6/infiniteLoop.c)
61+
62+
### Nested loops
63+
1. [Multiplication table using nested loops](Day 6/nestedLoops.c)
64+
65+
## Day 7: Jump statements
66+
### Break, continue and goto
67+
1. [Example program to understand break](Day 7/break.c)
68+
2. [Example program to understand continue](Day 7/continue.c)
69+
3. [Example program to understand goto](Day 7/goto.c)
70+
71+
### Usage of break
72+
1. [Check whether given number is prime](Day 7/prime.c)
73+
74+
### Creating your own functions
75+
1. [Sum of first n prime numbers](Day 7/sumOfNPrimes.c)
76+
77+
## Day 8: Introduction to Pointers
78+
1. [Nested Loop: Pattern printing](Day 8/nestedLoopPattern.c)
79+
2. [Two functions to swap numbers](Day 8/swap.c)
80+
81+
The first function doesn't work. We try to understand why that doesn't work.
82+
An introduction to pointers and memory is provided.
83+
Referencing and Dereferencing operators are discussed.
84+
85+
## Day 9: Introduction to Arrays
86+
1. [Understanding object and value contexts](Day 9/pointers1.c)
87+
2. [Introduction to arrays](Day 9/arrays.c)
88+
3. [Array Initialization](Day 9/arrayInitialization.c)
89+
4. [Character Array](Day 9/charArray.c)
90+
91+
## Day 10: Pointer arithmetic and Array examples
92+
1. [Character array vs String in C](Day 10/charArrayVsString.c)
93+
2. [Understanding various declarations](Day 10/allDeclarations.c)
94+
3. [Pointer arithmetic](Day 10/pointerArithmetic.c)
95+
4. [Array value rule](Day 10/arrayValueRule.c)
96+
5. [Count no. of words in a line](Day 10/countWordsInLine.c)
97+
98+
## Day 11: Functions in C
99+
1. [Call by value](Day 11/callByValue.c)
100+
2. [Simulated Call by reference](Day 11/callByReferenceSimulated.c)
101+
3. [Fibonacci Function](Day 11/fibonacciFunction.c)
102+
4. [Max of three numbers function](Day 11/maxOfThreeNumbers.c)
103+
5. [Recursive functions](Day 11/recursiveFunction.c)
104+
105+
## Day 13: Passing arrays to functions and 2D arrays introduction
106+
1. [Passing arrays to functions](Day 13/sumOfArray.c)
107+
2. [Reference variables in C++](Day 13/referenceVariables.cpp)
108+
3. [Introduction to 2D arrays](Day 13/printMatrix.c)
109+
110+
## Day 14: Array of arrays, Typecasting, Macros, Static variables
111+
1. [Understand array of arrays](Day 14/arrayOfArrays.c)
112+
2. [Implicit Typecasting](Day 14/typecasting.c)
113+
3. [Explicity Typecasting](Day 14/avgOfThreeNumbers.c)
114+
4. [Constants in C][Day 14/constant.c]
115+
5. [Macros](Day 14/macros.c)
116+
6. [Static variables](Day 14/staticVariable.c)
117+
118+
## Day 15: Structures in C
119+
1. [Declaring and using structure](Day 15/structFish.c)
120+
2. [Nested structures](Day 15/nestedStructures.c)
121+
3. [Updating structure fields](Day 15/turtleBirthday.c)

0 commit comments

Comments
 (0)