-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
213 lines (173 loc) · 4.56 KB
/
main.c
File metadata and controls
213 lines (173 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <stdio.h>
void helloWorld();
void testingBasicOperators();
void creatingBasicVariablesAndArrays();
void printingMyNameAsString();
void conditionals();
void iterationStatements();
void passByValue();
//Structure, the "alternative" to classes
struct point {
int x;
int y;
};
struct line {
struct point start;
struct point end;
};
//Union
union value {
int i;
float f;
};
//The infamous goTo, infinite loop
//int main(int argc, char*argv[]) {
// int x = 5;
// int z=3;
// goToDestination:if (z=3){
// printf("z is %i \n",z++);
// goto goToDestination;
// }
// return 0;
//}
//Running all the small programs written below
int main(int argc, char*argv[]) {
printf("Calling 'helloWorld' function \n");
helloWorld();
printf("Calling 'testingBasicOperators' function \n");
testingBasicOperators();
printf("Calling 'creatingBasicVariablesAndArrays' function \n");
creatingBasicVariablesAndArrays();
printf("Calling 'printingMyNameAsString' function \n");
printingMyNameAsString();
printf("Calling 'conditionals' function \n");
conditionals();
printf("Calling 'iterationStatements' function \n");
iterationStatements();
printf("Calling 'passByValue' function \n");
passByValue();
printf("Experimenting with structured types \n");
printf("Structure: \n");
struct point origin;
origin.x = 0;
origin.y = 0;
printf("origin.x=%i and origin.y=%i\n",origin.x,origin.y);
struct line line1;
line1.start.x = 1;
line1.start.y = 2;
line1.end.x = 3;
line1.end.y = 4;
printf("line1.start.x=%i, line1.start.y=%i, line1.end.x=%i and line1.end.y=%i \n",line1.start.x,line1.start.y,line1.end.x,line1.end.y);
printf("Union: \n");
union value v;
v.i = 123; // Set v to bits of integer 123
printf("%i\n", v.i);
//ToDo find out why the code below prints 0.00000 when the book stated otherwise
printf("%f\n", v.f); // Set v to bits of integer 123
return 0;
}
//Classic hello world
void helloWorld() {
printf("Hello, World!\n");
printf("\n");
}
//Basic operators
void testingBasicOperators() {
printf("%d\n", 3 < 2); // 0
printf("%d\n", 2 < 3); // 1
printf("%d\n", 1 && 0); // 0
printf("%d\n", 1 || 0); // 1
printf("%d\n", 0 && 123); // 0
printf("%d\n", 0 || 123); // 1
printf("%d\n", 1 < 2 && 2 < 3); // 1
printf("\n");
}
//Basic variables and arrays
void creatingBasicVariablesAndArrays() {
int x = 1;
float f = 3.14159;
int numbers[3] = { 1, 2, 3 };
char letters[5] = { 'a', 'b', 'c', 'd', 'z' };
printf("x is %i and f is %f and the first element of numbers is %i and the first element of letters is %c \n",x,f,numbers[0],letters[0]);
numbers[1] = 99; // { 1, 99, 3 }
numbers[2] += 1;
printf("numbers[1] = %i and numbers[2] = %i \n",numbers[1],numbers[2]);
int multidimensionalArray [1][1];
multidimensionalArray [0][0]=5;
printf("Element in position 0,0 of multidimensional array : %i \n", multidimensionalArray[0][0]);
printf("\n");
}
//Strings
void printingMyNameAsString() {
char name[] ="Peter";
printf("Just printing my name: %s \n",name);
printf("\n");
}
//Conditionals
void conditionals() {
int x = 0;
if (x) {
printf("x is true \n");
} else {
printf("x is false \n");
}
int y = 1;
if (y) {
printf("y is true \n");
} else {
printf("y is false \n");
}
int z = 2;
switch (z) {
case 1:
printf("First case \n");
case 2:
printf("Second case \n");
case 3:
printf("Third case \n");
case 4:
printf("Fourth case \n");
break;
case 5:
printf("Fifth case \n");
}
printf("\n");
}
//Iteration statements
void iterationStatements() {
int x=5;
while (x){
printf("x is %i \n",x);
x--;
}
do{
printf("In the do-while x is %i \n",x);
}while(x);
for(int i=0;i<7;i++){
if (i==3){
printf("i is 3 so a continue command will be executed \n");
continue;
}
printf("i is %i \n",i);
}
printf("\n");
}
//Basic function use
int double_it(int x) {
x = 2*x;
return x;
}
void passByValue() {
int i = 10;
int j = double_it(i);
printf("i=%d, j=%d\n", i, j);
printf("\n");
}
//Input from user
//int main(int argc, char*argv[]) {
// int i;
// float f;
// char name[50];
// scanf("%d %f %s", &i, &f, name);
// return 0;
//}