diff --git a/Makefile b/Makefile index a6c2725..9495c33 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CFLAGS=-Wall -g CC=gcc -NAME=ex11 +NAME=ex12 RUNCMD=./out/$(NAME) all: clean compile diff --git a/ex10.c b/ex10.c index 8dba196..4c27d77 100644 --- a/ex10.c +++ b/ex10.c @@ -22,18 +22,24 @@ int main(int argc, char *argv[]) int num_states = 4; puts("Printing our own array of strings:"); for(i = 0; i < num_states; i++) { - printf("state %d: %s\n",i, states[i]); + char * state = states[i]; + if('W' == *state) { + printf("Detected Washington. Exiting early (ex12 Extra Credit #3)\n"); + break; + } else { + printf("state %d: %s\n",i, state); + } } //////////////////////////////////////////////////////////////////// - // EXTRA CREDIT + printf("** EXTRA CREDIT\n"); // 1) Figure out what kind of code you can put in the parts of a for-loop // 2) Look up how to use the comma character to separate multiple // statements in parts of the for-loop. // http://stackoverflow.com/q/276512/7507 - puts("multiple initializers"); + puts("Testing for statement with multiple initializers"); int j = 0; for(i = 0, j = 100; i < num_states; i++, j += 5) { printf("state %d: (%d) %s\n", i, j, states[i]); diff --git a/ex11.c b/ex11.c index 43474e3..cbbc969 100644 --- a/ex11.c +++ b/ex11.c @@ -6,6 +6,13 @@ int main(int argc, char *argv[]) int i = 0; while(i < argc) { + char *arg = argv[i]; + // Ex12 Extra Credit #3 + if(*arg == 'Z') { + printf("Detected argument starting with 'Z'! "); + printf("Not printing any more command line arguments...\n"); + break; + } printf("arg %d: %s\n", i, argv[i]); i++; } diff --git a/ex12.c b/ex12.c new file mode 100644 index 0000000..ea12f3a --- /dev/null +++ b/ex12.c @@ -0,0 +1,61 @@ +#include + +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; +}