This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples.c
196 lines (160 loc) · 3.59 KB
/
examples.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
/* commonly used imports */
// Basic I/O (printing to screen, reading input, etc)
#include <stdio.h>
// Basic conversion functions and memory management
#include <stdlib.h>
// String manipulation functions
#include <string.h>
// Defines boolean types
#include <stdbool.h>
// Math functions
#include <math.h>
/* function prototypes */
void struct_demo(void);
void do_swap();
void stack_vs_heap(void);
/* our entry point */
int main(void)
{
printf("%s\n", "Hello World!");
}
/* ========== SECTION 1: BASIC CONTROL FLOW ========== */
void if_statement_example(int i)
{
if (i == 1)
{
// do something
}
else if (i == 2)
{
// do something else
}
else
{
// default thing to do
}
}
void switch_case_example(int i)
{
switch (i)
{
case 1:
// do something
break;
case 2:
// do something else
break;
default:
// default thing to do
break;
}
}
void loop_example(void)
{
int integers[] = {1, 2, 3, 4, 5};
// example of a for loop
// iterating over an array
for (int i = 0; i < 5; i++)
{
int current = integers[i];
// do something with current
}
// non-example of a for-each loop
//* for (int i : some_list)
//* {
//! THIS DOES NOT WORK!
//* }
int i = 69;
// terminating while loop
while (i > 0)
{
// do something
i--;
}
// infinite loop
while (1)
{
// do something forever
}
}
void type_casting_example(void)
{
// Casting a character to an integer (its ASCII value)
char ex = 'A';
int ex_as_int = (int) ex;
// Casting ex from an integer to a float
float ex_as_float = (float) ex_as_int;
// Casting an integer to a pointer (points to memory address 0)
void *null = (void *) 0;
}
/* ========== SECTION 2: POINTERS AND MEMORY MANAGEMENT ========== */
void swap_two_numbers_wrong(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
void swap_two_numbers_correctly(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void do_swap(void)
{
int x = 69;
int y = 420;
printf("x is %i, y is %i\n", x, y);
swap_two_numbers_correctly(&x, &y);
printf("after swap, x is %i, y is %i\n", x, y);
}
void stack_vs_heap(void)
{
char stack_str[32];
//gets(stack_str);
scanf("%s", stack_str);
printf("%s\n", stack_str);
char *heap_str = malloc(32);
free(heap_str);
}
void aliasing_demo(void)
{
int *x = malloc(sizeof(int));
*x = 16;
int *y = x;
free(x);
x = NULL;
}
/* ========== SECTION 3: STRUCTS AND TYPEDEFS ========== */
// declaring a struct
struct thisisastruct
{
int field1;
char *field2;
};
// typedef-ing a struct
typedef struct thisisastruct newstruct;
// typedef-ing an existing type
typedef char *string;
void struct_demo(void)
{
printf("The size of our struct is %lu bytes\n", sizeof(struct thisisastruct));
string str = "according to all known laws of aviation";
// declaring a struct on the stack
struct thisisastruct a_struct;
a_struct.field1 = 420;
a_struct.field2 = "nani!?";
printf("%s\n", a_struct.field2);
// allocating a new struct on the heap
newstruct *a_struct2 = malloc(sizeof(newstruct));
if (a_struct2 == NULL)
{
// do some error handling
}
// dereferencing a struct to access its fields
(*a_struct2).field1 = 69;
// syntactic sugar for the above operation
a_struct2->field2 = str;
// malloc and free should ALWAYS come in pairs
free(a_struct2);
}