├── .gitignore
├── .idea
├── .name
├── codeStyles
│ └── Project.xml
├── dictionaries
│ └── vivnp.xml
├── learn-c.iml
├── misc.xml
├── modules.xml
├── vcs.xml
└── workspace.xml
├── CMakeLists.txt
├── README.md
├── a.out
├── arrays.c
├── functions.c
├── hello_world.c
├── if_statements.c
├── loops.c
├── pointers.c
├── pointers_extra.c
├── strings.c
└── variables.c
/.gitignore:
--------------------------------------------------------------------------------
1 | cmake-build-debug/
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | Learn-C
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.idea/dictionaries/vivnp.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | printf
5 | strlen
6 | strncat
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/learn-c.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 |
187 |
188 |
189 |
190 |
191 | true
192 | DEFINITION_ORDER
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 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 | 1535944329497
292 |
293 |
294 | 1535944329497
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required (VERSION 2.6)
2 | project (Learn-C)
3 | add_executable(Hello_World hello_world.c)
4 | add_executable(Variables variables.c)
5 | add_executable(Arrays arrays.c)
6 | add_executable(Strings strings.c)
7 | add_executable(If_Statements if_statements.c)
8 | add_executable(Loops loops.c)
9 | add_executable(Functions functions.c)
10 | add_executable(Pointers pointers.c)
11 | add_executable(Pointers_extra pointers_extra.c)
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Learn C
2 | This repository contains a tutorial for all the major C concepts like variables,
3 | loops, pointers, etc.
4 |
5 | ## Index
6 | * [Hello World](https://github.com/vivek3141/learn-c/blob/master/hello_world.c)
7 | * [Variables](https://github.com/vivek3141/learn-c/blob/master/variables.c)
8 | * [Arrays](https://github.com/vivek3141/learn-c/blob/master/arrays.c)
9 | * [If Statements](https://github.com/vivek3141/learn-c/blob/master/if_statements.c)
10 | * [Strings](https://github.com/vivek3141/learn-c/blob/master/strings.c)
11 | * [Loops](https://github.com/vivek3141/learn-c/blob/master/loops.c)
12 | * [Functions](https://github.com/vivek3141/learn-c/blob/master/functions.c)
13 | * [Pointers](https://github.com/vivek3141/learn-c/blob/master/pointers.c)
14 | * [Pointers Extra](https://github.com/vivek3141/learn-c/blob/master/pointers_extra.c)
15 |
--------------------------------------------------------------------------------
/a.out:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/vivek3141/learn-c/f79b3b078b58c40a6a47cb9c74677b6b32cf828c/a.out
--------------------------------------------------------------------------------
/arrays.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(){
4 | // Array stores many variables together
5 | int array[3]; // Array of 3 elements, indexed 0 to 2
6 | array[0] = 0;
7 | array[1] = 1;
8 | array[2] = 2;
9 |
10 | // Can also define like this
11 | int arr[3] = {0, 1, 2};
12 |
13 | // Arrays can be of any type
14 | char character[2][2];
15 |
16 | // 2 Dimensional Arrays - like a grid
17 | int two_d_arr[1][2]; // 1x2 array
18 | two_d_arr[0][1] = 3;
19 |
20 | // Can also do this
21 | int darr[2][2] = {{1,2}, {3,4}}; // 2x2 Array
22 |
23 | // Or this if no specification is needed
24 | int array_multi[2][2] = {1,2,3,4};
25 |
26 | // Can have as many dimensions
27 | int multi_d_array[2][2][3];
28 |
29 | return 0;
30 | }
31 |
32 |
--------------------------------------------------------------------------------
/functions.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | // Function prototype, define later
4 | int add(int, int); // int is the return type
5 | void printHello(); // void means no return
6 | int main() {
7 | int sum = add(1, 2);
8 | static int a = 0; // Add later.
9 | printf("%d\n", sum);
10 | printHello();
11 | return 0;
12 | }
13 |
14 | void printHello() {
15 | printf("Hello!");
16 | }
17 |
18 | int add(int num1, int num2) { // must declare types of input variables
19 | return num1 + num2;
20 | }
21 |
22 |
23 |
--------------------------------------------------------------------------------
/hello_world.c:
--------------------------------------------------------------------------------
1 | #include // Include this file which contains the printf command
2 |
3 | int main(){
4 | printf("Hello World\n"); // \n is new line
5 | return 0; // Return 0 to say that the program is complete
6 | }
7 |
8 |
--------------------------------------------------------------------------------
/if_statements.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(){
4 | int a = 0;
5 | int c = 1;
6 | if(a == 0){ // == is equality operator, checks if 2 things are equal.
7 | printf("a is equal to 0\n");
8 | }
9 | else{ // If the above is not true
10 | printf("a is not equal to 0\n");
11 | }
12 |
13 | if(a != 0){ // != is not equal to
14 | printf("a is not equal to 0\n");
15 | }
16 | else{
17 | printf("a is equal to 0\n");
18 | }
19 |
20 | if(a == c){
21 | printf("a is not equal to c\n");
22 | }
23 | else if(a == 0){ // Not checked if the above is true
24 | printf("a is equal to 0\n");
25 | }
26 | // Supported comparisons: ==, >, <, >=, <=, !=
27 |
28 | // Combining Statements,
29 | if(a != 0 || a == c){ // Or
30 | printf("a is not equal to zero or a is equal to c");
31 | }
32 | if(a == 0 && a != c){ // And
33 | printf("a is equal to 0 and not equal to c");
34 | }
35 | return 0;
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/loops.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(){
4 | int i = 0;
5 | for(i = 0; i < 10; i++){ // Initial value 0, until i is not less than 10, increment by 1
6 | printf("%d", i);
7 | printf(" ");
8 | }
9 | printf("\n");
10 |
11 | int k = 0;
12 | while(k < 10){ // Do while the condition is satisfied. If the condition is not, break. Check the condition then do the loop.
13 | printf("%d", k);
14 | printf(" ");
15 | k++;
16 | }
17 | printf("\n");
18 |
19 | int z = 10;
20 | do { // First do the loop, then check the condition. In this case the loop is executed once, even though the condition is false.
21 | printf("%d", z);
22 | z++;
23 | }
24 | while(z > 100); // If this was a while loop, printf(z) would never have run.
25 | // Can also have a break; statement to exit the loop or a continue; statement to go to the top of the loop.
26 | return 0;
27 | }
28 |
29 |
--------------------------------------------------------------------------------
/pointers.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | void modify(char *string);
6 |
7 | int main() {
8 | //first, let's take a look at the behavior of a simple variable with the operators * and &
9 | int x; //declaration
10 | x = 4; //initialization, aka, putting 4 inside x, or assigning x to 4, or setting x's content as 4
11 |
12 | printf("Content in x: %d\n", x);
13 | printf("Address of x: %ld\n", (long) (&x));
14 | printf("Content in x: %d\n", *(&x));
15 |
16 | printf("(Address of x) + (1 * 4) bytes (size of an int in memory): %ld\n", (long) ((&x) + 1));
17 | //^^^ when you set up the variable as type int adding 1 to its address will add
18 | // actually 4 bytes behind the scenes, because that's the size of an int in computer memory.
19 | printf("Content in ((address of x) + 4 bytes): %d //garbage value\n", *(&x + 1));
20 |
21 | int *pointer_to_x;
22 | pointer_to_x = &x;
23 |
24 | printf("Content in x / content of the variable pointed by pointer_to_x: %d \n", *(pointer_to_x));
25 | printf("Address of pointer_to_x: %ld\n", (long) (pointer_to_x));
26 |
27 | printf("\n\n");
28 |
29 |
30 | char **array = malloc(sizeof(char *) * 10);
31 | int index = 0;
32 | int index_string;
33 |
34 | while (index < 10) {
35 | index_string = 0;
36 | array[index] = malloc(sizeof(char) * 10);
37 | while (index_string < 10) {
38 | array[index][index_string] = (char) (index_string + 48);
39 | index_string++;
40 | }
41 | modify(array[index]);
42 | printf("%s\n", array[index]);
43 | index++;
44 | }
45 |
46 | return 0;
47 | }
48 |
49 | void modify(char *string) {
50 | int index = 0;
51 | char c = 'a';
52 | while (index < 10) {
53 | string[index] = (char) (c + index);
54 | index++;
55 | }
56 | }
--------------------------------------------------------------------------------
/pointers_extra.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 | int main() {
8 |
9 | //uint32_t * nums = (uint32_t *) 0x0001; // 0x0001 | 01 00 00 00, 02 00 00 00, 03 00 00 00
10 | // nums = an integer of 0x0001 - not a legal address
11 | // or
12 | //uint32_t nums[] = { 1, 2, 3 };
13 | // nums = an integer pointer generated by the compiler linker; points to somwhere in the data section of the executable
14 | // pointer is legal
15 | //0x0001 | 01 00 00 00, 02 00 00 00, 03 00 00 00
16 | uint32_t *nums = (uint32_t *) 0x0001;
17 |
18 | printf("%i \n", *nums); // = 1
19 | printf("%i \n", nums[0]); // = 1
20 | printf("%i \n", nums[1]); // = 2
21 | printf("%i \n", nums[2]); // = 2
22 |
23 |
24 | printf("%p \n", nums); // 0x0001
25 | printf("%p \n", &nums[0]); // 0x0001
26 | printf("%p \n", &nums[1]); // 0x0001 + sizeof(uint32_t)
27 | printf("%p \n", &nums[2]); // 0x0001 + sizeof(uint32_t) + sizeof(uint32_t)
28 | int a = 2;
29 | int *p = &a;
30 | printf("%d\n", p[10]);
31 | //printf("%d", sizeof(int));
32 |
33 | int *array_int;
34 | array_int = malloc(sizeof(int) * 10);
35 | char *string = "first string";
36 | char **str;
37 | str = malloc(sizeof {})
38 | int index = 0;
39 | while (string[index]) {
40 | printf("%c\n", string[index]);
41 | index++;
42 | }
43 | index = 0;
44 | while (index < 10) {
45 | array_int[index] = index;
46 | index++;
47 | }
48 |
49 | int a = 0;
50 | int *p = &a;
51 |
52 | printf("%p\n", p);
53 | printf("%p\n", &p[10]);
54 |
55 | printf("%p\n", p + 10);
56 | }
57 |
--------------------------------------------------------------------------------
/strings.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include // Contains required functions
3 |
4 | int main(){
5 | // Strings are arrays of characters
6 | char * str = "Hello!"; // * explained later on. Can't manipulate this. Eg. can't do str[0];
7 | char string[6] = "Hello!"; // Creates a array of characters.
8 | char string_no_num[] = "Hello!"; // Same as the top
9 | // Printing out strings
10 | printf("The variable str contains %s\n", str);
11 | printf("Length of str is: %li\n", strlen(str)); // strlen() gives length of string.
12 | printf("str + string: %s\n", strncat(str, string, 12));
13 | /* strncat() joins 2 strings. Number at the end is for max number of appended characters. */
14 | if (strncmp(string, string_no_num, 6) == 0){ // Number at end is for how many characters to compare.
15 | printf("They are the same"); // Outputs 0 if they are the same, 1 if not.
16 | }
17 | else{
18 | printf("They are not the same");
19 | }
20 |
21 | return 0;
22 | }
23 |
24 |
--------------------------------------------------------------------------------
/variables.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(){
4 | int number; // No value set
5 | int num = 0; // 2 or 4 bytes
6 | printf("Size of int: %ld\n", sizeof(int)); // sizeof() outputs storage size for the object in bytes. Outputs 4
7 | char h = 'h'; // 1 byte '' is for character
8 | short n = 0; // 2 bytes
9 | long l = 0; // 8 bytes
10 |
11 | float f = 0.4f; // 4 bytes
12 | double d = 0.1f; // 8 bytes
13 | long double ld = 0.22f; //10 bytes
14 |
15 | // No boolean, can use 1 or 0 instead
16 |
17 | return 0;
18 | }
19 |
--------------------------------------------------------------------------------