-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearning_c.c
273 lines (204 loc) · 6.73 KB
/
learning_c.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
Learning C from W3Schools
*/
#include <stdio.h> // header file for getting input and output
#include <stdbool.h> // header file for importing booleans
#include <string.h> // header file for string functions for operations
/*int main(){
// ! arithmetic operators
int sum1 = 100 + 50;
int difference = sum1 - 250;
int multiplication = difference * sum1;
int division = 50/5;
int modulus = 15/2;
int incremential = modulus++;
int decrement = incremential--;
// all operators are fairly similar to Java. && for and, || Or, and ! for Not.
// ! Booleans
bool isProgrammingFun = true; // returns 1 (true)
bool isFishTasty = false; // returns 0 (false)
printf("%d %d\n", isProgrammingFun, isFishTasty);
//examples
int myAge = 18;
int votingAge = 18;
//! condition statements
if (myAge>=votingAge)
{
printf("Old enough to vote!");
} else{
printf("Not old enough to vote.");
}
//* Short Hand If
//instead of:
int time = 20;
if (time<18){
printf("Good day");
} else {
printf("Good evening");
} // you can write this instead:
int time2 = 30;
(time<30) ? printf("Good Day.") : printf("Good evening");
// (condition) ? expressionTrue : expressionFalse;
return 0;
}
*/
// int main()
// {
/* //! switch statements (like java)
// example:
int day = 6;
switch(day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid day of the week");
break;
}
//! While and Do-While Loop:
// while (condition) {code block};
// do {code block} while(condition);
// examples :
int i = 0;
while (i < 10)
{
printf("%d",i);
i++;
}
do
{
printf("%d",i);
i++;
} while(i < 5);
//! For loops and Breaks/Continues
int j;
for (j = 0; j < 10; j++)
{
if (j ==6)
{
break;
}
printf("%d\n",j);
}
//! Arrays and Elements of Array
int my_num_array[] = {1,2,3,4,5}; // initializing an array
printf("%d\n", my_num_array[2]); // accessing the [2nd element\3rd Number]
// changing array elements
my_num_array[3] = 73;
// loop through array
int k = 0;
for (k=0; k<4; k++)
{
printf("%d \n", my_num_array[k]);
}
// set array size
int myNumArrS[4];
// getting an array size
printf("%lu\n", sizeof(my_num_array)); //* returns the byte size rather than the amount of elements - uses lu for unsigned long int
//you can use sizeof to get an array length:
int length = sizeof(my_num_array) / sizeof(my_num_array[0]); // works by taking the bytesize of the entire array and dividing it over the size of one individual array element to get the total amount of elements
int num = 0;
for (num=0; num < length; num++)
{
printf("%d\n", my_num_array[num]);
}
*/
/* //! Two Dimensional Arrays (matrices)
// type variable[rows][columns] = { {row 0 column values}, {row 1 column values} };
int matrix[2][3] = { {1,2,3}, {3,4,5,6} };
// getting an element of a matrix -
printf("%d\n", matrix[0][2]); // matrix[row value][column value]
// looping through a 2D array
int num2,num3;
for (num2=0; num2 < 2; num2++)
{
for (num3=0; num3 < 3; num3++)
{
printf("%d ", matrix[num2][num3]);
}
}
*/
/*
// ! Creating Strings
char greetings[] = "Hello World!";
printf("%c\n", greetings[0]);
// looping through a string
int length = sizeof(greetings) / sizeof(greetings[0]);
int i;
for (i = 0; i < length; i++)
{
printf("%c\n", greetings[i]);
}
// * String Functions - need to include string header file
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d\n", strlen(alphabet)); //strlen function gets string length
char str1[20] = "Hello ";
char str2[] = "World!";
strcat(str1,str2); // concatenates the two strings and saves to the first given
printf("%s\n", str1);
char string1[] = "Hello";
char string2[] = "Hello";
char string3[] = "Hi";
printf("%d\n", strcmp(str1,str2)); // strcmp compares the first string to the second, returns 0 if equal and otherwise if not equal
}
*/
int main()
{
/* //! Getting user input
int myNum; // initialize a variable to store the user number
printf("Type a number: \n"); // asking user to type a number
scanf("%d", &myNum); // use scanf method to get user input and save to variable
printf("The number given was: %d", myNum); // printing out the obtained number
// * multiple inputs
char myChar;
printf("Type a number and a character and press enter: \n");
scanf("%d %c", &myNum, &myChar);
// * Taking String Input and scanf Limitations
// scanf sees spaces as terminating characters
// Create a string
char firstName[30];
// Ask the user to input some text
printf("Enter your first name: \n");
// Get and save the text
scanf("%s", firstName);
// Output the text
printf("Hello %s", firstName);
// ---------------------------
// * Use fgets to read a line of text with arguments: sizeof(string_name),stdin
char fullName[30];
printf("Type your full name: \n");
fgets(fullName, sizeof(fullName), stdin);
printf("Hello %s", fullName);
// Type your full name: John Doe
// Hello John Doe
*/
//! Memory Address
int myAge = 18;
printf("%p\n", &myAge); // use %p (pointer value format specifier), &myAge is often called a pointer
// uses the & reference operator, the result represents where the variable is stored (in hexadecimal form)
int* ptr = &myAge; //* use * operator to make a pointer, variable "ptr", the pointer stores the address of myAge
printf("%p\n", ptr); // result of this matches with previous as we match the two
// Dereference:
printf("%d\n", *ptr);
//!
return 0;
}