-
Notifications
You must be signed in to change notification settings - Fork 1
/
C.c
510 lines (270 loc) · 9.02 KB
/
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* -------------------------------------------------- */
Hello C
/* -------------------------------------------------- */
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
/* -------------------------------------------------- */
Cygwin
/* -------------------------------------------------- */
// compile
gcc file.c // a.exe
gcc file.c -o file // file.exe
// run
./a
./file.exe
/* -------------------------------------------------- */
While
/* -------------------------------------------------- */
while(1)
{
printf("hello, world\n");
}
/* -------------------------------------------------- */
For
/* -------------------------------------------------- */
for (int i = 0; i < 10; i++)
{
printf("hello, world\n");
}
/* -------------------------------------------------- */
if & else
/* -------------------------------------------------- */
if (1)
{
printf("hello\n");
}
else if (1)
{
printf("bonjour\n");
}
else
{
printf("goodbye\n");
}
/* -------------------------------------------------- */
printf
/* -------------------------------------------------- */
// print string
#include <stdio.h>
int main(void)
{
char name[128] = "danny";
printf("hello, %s\n", name);
}
// print some numbers
#include <stdio.h>
int main(void)
{
int a = 2; int b = 3; // int
float c = 2; float d = 3; // float
printf("%i + %i = %i\n", a, b, a + b); // int
printf("%i * %i = %i\n", a, b, a * b); // int
printf("%f / %f = %.10f\n", c, d, c / d); // float (10 decimal)
}
// sizeof()
#include <stdio.h>
int main(void)
{
printf("1 is %lu\n", sizeof(1));
printf("char is %lu\n", sizeof(char));
printf("double is %lu\n", sizeof(double));
printf("float is %lu\n", sizeof(float));
printf("int is %lu\n", sizeof(int));
printf("long long is %lu\n", sizeof(long long));
}
c Character
d or i Signed decimal integer
e Scientific notation (mantissa/exponent) using e character
E Scientific notation (mantissa/exponent) using E character
f Decimal floating point
g Uses the shorter of %e or %f
G Uses the shorter of %E or %f
o Signed octal
s String of characters
u Unsigned decimal integer
x Unsigned hexadecimal integer
X Unsigned hexadecimal integer (capital letters)
p Pointer address
n Nothing printed
% Character
/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */
Idioms
/* -------------------------------------------------- */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#define CHECK(PRED) printf(“%s…%s\n”, (PRED) ? “passed!” : “FAILED”, #PRED)
#define LINESIZE 1024
/*********** STANDARD IDIOMS ***********/
/**** And other fun examples ****/
/************************TABLE OF CONTENTS *************************/
/**** Copy a heading below and ctrl-f! ****/
/**** STANDARD IDIOM TO PROCESS AN ARRAY ***************************/
/**** STANDARD IDIOM TO PROCESS A STRING ***************************/
/**** FIND THE LENGTH OF A STRING **********************************/
/**** STANDARD IDIOM TO OPEN A FILE ********************************/
/**** PERFORM I/O OPERATIONS ON FILE *******************************/
/**** STANDARD IDIOM TO CLOSE A FILE *******************************/
/**** STANDARD IDIOM TO PROCESS A FILE CHAR BY CHAR ****************/
/**** STANDARD IDIOM TO PROCESS STDIN LINE BY LINE *****************/
/**** CLEARING THE ERROR FROM STDIN ********************************/
/**** USING TERNARY OPERATOR IN PRINTF *****************************/
/**** STANDARD IDIOM TO PROCESS COMMAND LINE ARGUMENTS**************/
/**** SWITCH STATEMENTS ********************************************/
/**** STANDARD IDIOM TO SEEK IN A FILE *****************************/
/**** STANDARD IDIOM TO PROCESS ARRAY BACKWARDS ********************/
/**** STANDARD IDIOM TO FIND n BITS THAT ARE p BITS FROM THE END ***/
/**** STANDARD IDIOM TO PROCESS A STRING, POINTER-STYLE ************/
int main(int argc, char *argv[]) {
int i, count, c, n = 1, j, m = 2, p = 3, total;
char choice = 'y';
int a[LINESIZE];
char s[LINESIZE];
FILE *fp;
char line[LINESIZE];
/**** STANDARD IDIOM TO PROCESS AN ARRAY ***************************/
for (i = 0; i < LINESIZE; i++) {
}
/******************************************************************/
/**** STANDARD IDIOM TO PROCESS A STRING ***************************/
for (i = 0; a[i] != '\0'; i++) {
/*Does NOT process the null character. */
}
/**** FIND THE LENGTH OF A STRING **********************************/
for (i = 0; a[i] != '\0'; i++) {
/* Do nothing */
}
printf("%d", i);
/******************************************************************/
/**** STANDARD IDIOM TO OPEN A FILE ********************************/
FILE *fp;
if ((fp = fopen("data.txt", "w+")) == 0) { /* if fails; 0 is null pointer;
fopen returns the null pointer on faiure */
perror("fopen");
/* perror prints a system error message to stderror.
it can only be used if a function in the standard library fails and
it sets an error number in the global variable "errno". perror looks
at and prints a corresponding error message.*/
/* additional error handling if necessary */
}
/**** PERFORM I/O OPERATIONS ON FILE *******************************/
FILE *fp;
while (fscanf(fp, "%d", &n) == 1) { /*scanning from a file; fp is type FILE* */
total += n;
}
printf("total:%d", total);
/******************************************************************/
/**** STANDARD IDIOM TO CLOSE A FILE *******************************/
FILE *fp;
if (fclose(fp) != 0) {
perror("fclose");
/*additional error handling if needed */
}
/******************************************************************/
/**** STANDARD IDIOM TO PROCESS A FILE CHAR BY CHAR ****************/
char c;
while ((c = fgetc(fp)) != EOF) {
fputc(c, stdout);
putchar(c); /* same thing as fputc above */
/* Copy into another file:
fputc(c, ofp);
*/
}
/******************************************************************/
/**** STANDARD IDIOM TO PROCESS STDIN LINE BY LINE *****************/
char line[LINESIZE];
while (fgets(line, LINESIZE, stdin)) {
count++;
}
printf("%d", count);
if (fgets(line, LINESIZE, stdin)) { /* if we can read a line */
/*if (fgets(line, LINESIZE, stdin) != 0) {} --not as good; '!=0' is redundant */
printf("%s", line);
}
/**** CLEARING THE ERROR FROM STDIN ********************************/
if (!fgets(line, LINESIZE, stdin)) { /* if (fgets() == 0), or if we can't read a line */
clearerr(stdin); /*clears error; puts stream back into good state*/
/*break; if in a loop, break out of loop. */
}
/******************************************************************/
/**** USING TERNARY OPERATOR IN PRINTF *****************************/
for(i = 1; i < argc; i++) {
if (i < argc - 1) {
printf("%s ", argv[i]);
} else {
printf("%s\n", argv[i]);
}
}
/* or use ternary operator: */
for(i = 1; i < argc; i++) {
printf((i < argc - 1) ? "%s " : "%s\n", argv[i]);
}
/* or */
for(i = 1; i < argc; i++) {
printf("%s%c", argv[i], (i < argc - 1) ? ' ' : '\n');
}
printf("\n");
/******************************************************************/
/**** STANDARD IDIOM TO PROCESS COMMAND LINE ARGUMENTS**************/
for(i = 1; i < argc; i++) {
/* process argv[i] */
}
/******************************************************************/
/**** SWITCH STATEMENTS ********************************************/
int choice; /* or char choice */
switch (choice) {
case 'y': /*fall through to 'Y' */
case 'Y':
break;
case 'n':
case 'N':
break;
default:
break;
}
/******************************************************************/
/**** STANDARD IDIOM TO SEEK IN A FILE *****************************/
FILE *fp;
/*
SEEK_SET = from beginning of file
SEEK_CUR = from current position
SEEK_END = from end of file
*/
if (fseek(fp, pos, SEEK_SET) != 0) { /* if fseek fails */
perror("fseek");
}
/******************************************************************/
/**** STANDARD IDIOM TO PROCESS ARRAY BACKWARDS ********************/
for (i = 100; i > 0; i--) {
/* process a[i - 1] */
}
/* check if something is a palindrome */
for (i = 0, j = strlen(s); i < j; i++, j--) {
if (s[i] != s[j - 1]) {
printf("NOT a palindrome");
}
printf("IS a palindrome");
}
/******************************************************************/
/**** STANDARD IDIOM TO FIND n BITS THAT ARE p BITS FROM THE END ***/
i = m & ~(~0 << n) << p;
/******************************************************************/
/**** STANDARD IDIOM TO PROCESS A STRING, POINTER-STYLE ************/
char s[] = "world";
(const) char *p;
for (p = s; *p != '\0'; p++) {
/* Process *p */
/* NOTE: does NOT process null character! */
}
*p = '\0';
/******************************************************************/
return 0;
}