-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
222 lines (178 loc) · 5.14 KB
/
main.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
#include <complex.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define __STDC_WANT_LIB_EXT1__ 1
#define TEST int
unsigned TEST t;
typedef int a_test;
a_test a;
extern int get_number();
extern int test;
typedef int *i_pointer;
int increment() {
static int counter = 0;
int localVar = 0;
printf("Increment static: %d auto: %d\n", counter, localVar);
counter++;
localVar++;
return counter;
}
// Example of variable length array
void alphabet_array(int size) {
char alphabet_array[size];
int x = 0;
while (x < size) {
alphabet_array[x] = 'A' + x;
printf("alphabet_array[%d] = %c\n", x, alphabet_array[x]);
x++;
}
}
// Example of variable length array
int sum2d(int row, int col, int arr[row][col]) {
int r;
int c;
int tot = 0;
for (r = 0; r < row; r++) {
for (c = 0; c < col; c++) {
tot += arr[r][c];
}
}
return tot;
}
// flexible array members
struct s {
int arraySize;
int array[];
};
//designated initializers
struct point {
int x;
int y;
};
// packed data with bits
struct packed_struct {
unsigned int : 3;
unsigned int f1: 1;
unsigned int f2: 1;
unsigned int f3: 1;
unsigned int type: 8;
unsigned int index: 18;
};
int main(void) {
printf("Hello, World!\n");
int number = 0;
printf("%d\n", get_number());
printf("%d\n", test);
// printf("Enter a number: ");
// scanf("%d", &number);
// printf("The number is: %d\n", number);
for (int i = 0; i < 10; i++) {
increment();
}
i_pointer p; // same as int* p
i_pointer a, *b; // same as int* a , **b
i_pointer array[10]; // same as int *array[10]
size_t size = 0;
// printf("Enter array size: ");
// scanf("%d", &size);
// float values[size];
alphabet_array(5);
alphabet_array(10);
// flexible array members
int desireArraySize = 10;
struct s *ptr;
ptr = malloc(sizeof(struct s) + desireArraySize * sizeof(int));
// end flexible array member
// complex number
#ifdef __STDC_NO_COMPLEX__
#else
printf("complex are supported.\n");
double complex cx = 1.0 + 3.0 * I;
double complex cy = 1.0 - 4.0 * I;
printf("cx_r = %f,cx_i = %f, cy_r = %f, cy_i = %f\n", creal(cx), cimag(cx), creal(cy), cimag(cy));
double complex cz = cpow(cx, cy);
printf("cz_r = %f, cz_i = %f\n", creal(cz), cimag(cz));
double complex z1 = I * I; // imaginary unit squared
printf("I * I = %.1f%+.1fi\n", creal(z1), cimag(z1));
double complex z2 = pow(I, 2); // imaginary unit squared
printf("pow(I, 2) = %.1f%+.1fi\n", creal(z2), cimag(z2));
double PI = acos(-1);
double complex z3 = exp(I * PI); // Euler's formula
printf("exp(I*PI) = %.1f%+.1fi\n", creal(z3), cimag(z3));
double complex z4 = 1 + 2 * I, z5 = 1 - 2 * I; // conjugates
printf("(1+2i)*(1-2i) = %.1f%+.1fi\n", creal(z4 * z5), cimag(z4 * z5));
#endif
// designated initializers
int numbers_array[10] = {[2] = 12, [6] = 7};
for (int i = 0; i < 10; i++) {
printf("numbers_array[%d] = %d\n", i, numbers_array[i]);
}
struct point p1 = {10, 20};
struct point p2 = {.y = 10, .x = 20};
printf("p1 = %i, p2 = %i\n", p1.x, p1.y);
struct point pts[5] = {p1, [2].y = 4, [4].x = 7};
for (int i = 0; i < 5; i++) {
printf("pts[%d] = %i %i\n", i, pts[i].x, pts[i].y);
}
// constants
const float *pf; // pf points to a constant float value same as -> float const *pf;
float *const pd; // pd is the constant pointer which means you cant change the address
// restrict
int arr[10];
int *restrict rest_arr = (int *) malloc(sizeof(int) * 10);
int *par = arr;
int n;
for (n = 0; n < 10; n++) {
par[n] += 5;
rest_arr[n] += 5;
arr[n] *= 2;
par[n] += 3;
rest_arr[n] += 3; // optimized by the compiler like -> rest_arr[n] += 8;
}
// bit manipulation
short int w1 = 25;
short int w2 = 77;
short int w3 = 0;
short int w4 = 2;
w3 = w1 & w2;
printf("w1 = %d, w2 = %d, w3 = %d\n", w1, w2, w3);
w3 = w1 | w2;
printf("w1 = %d, w2 = %d, w3 = %d\n", w1, w2, w3);
w3 ^= w1;
printf("w1 = %d, w2 = %d, w3 = %d\n", w1, w2, w3);
w3 = ~w4;
printf("w1 = %d, w2 = %d, w3 = %d\n", w1, w2, w3);
int result = 0;
result = w1 << 1;
printf("result = %d\n", result);
int flags = 15; // 0000 1111
int mask = 182; // 1011 0110
flags = flags | mask; // 1011 1111
printf("flags = %d\n", flags);
flags = 15;
flags = flags & (~mask); // 0000 1001
printf("flags = %d\n", flags);
flags = 15;
flags = flags ^ mask; // 1011 1001
printf("flags = %d\n", flags);
// pact of data
struct packed_struct packed_data;
packed_data.type = 7;
unsigned int bit = packed_data.type;
if (packed_data.f1 == 0) {
printf("packed_data.f1 = %d\n", packed_data.type);
}
return 0;
}
void display(const arr[], size_t size) {
}
char *strcat(char *s1, const char *s2) {
}
// restrict
void f1(int n, float *restrict a1, const float *restrict a2) {
int i;
for (i = 0; i < n; i++) {
a1[i] += a2[i];
}
}