-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex12.c
61 lines (53 loc) · 1.85 KB
/
ex12.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
#include <stdio.h>
int main(int argc, char *argv[])
{
int i = 0;
if(argc == 1) {
// As per Extra Credit item #4
printf("You didn't enter any command-line arguments. You suck.\n");
} else if(argc > 1 && argc < 4) {
printf("Here are your arguments:\n");
for(i = 1; i < argc; i++) {
printf("%s ", argv[i]);
}
printf("\n");
} else if(argc > 4 && argc < 10) {
// Extra credit #2.
printf("That's a lot of arguments. I'll print them anyway.\n");
while(i < argc) {
printf("%s ", argv[i]);
i++;
}
printf("\n");
} else {
puts("You have too many arguments. You suck.\n");
}
// Extra credit #2
if(argc > 1) {
if(*argv[1] == 'Z') {
printf("You entered the secret character. Congratuations!\n");
}
}
//////////////////////////////////////////////////////////////////////////
printf("** EXTRA CREDIT\n");
// 1) Research the "boolean operators"
// Technically, they are &&, || and !, but since since C uses
// numeric values for conditional tests, technically any operator
// that returns a numeric value can be used in this manner.
// I.e. &, | and ^.
if(1 & 0) { printf("1 & 0 = \"true\"\n"); } else { printf("1 & 0 = \"false\"\n"); }
if(1 | 0) { printf("1 | 0 = \"true\"\n"); } else { printf("1 | 0 = \"false\"\n"); }
if(1 ^ 1) { printf("1 ^ 1 = \"true\"\n"); } else { printf("1 ^ 1 = \"false\"\n"); }
// Let's test if these operators are "short circuit" like in Java...
if(1 > 0 && printf("If you see this, then C does not \"short circuit\" conditionals.\n")) {
printf("short circuit test: true\n");
} else {
printf("short circuit test: false\n");
}
// 2) Add some more test cases (see above).
// 3) Go back to ex10 and ex11. Add if-statements to make loops exit early
// using break.
// 4) Fix the first test so the message accurately explains to the user
// that they didn't enter any arguments.
return 0;
}