Script started on Wed Sep 18 12:31:44 2024 mweeks@air:C_ch2$ vi example_switch.c mweeks@air:C_ch2$ gcc example_switch.c -o example_switch mweeks@air:C_ch2$ ./example_switch Zero One Two Expecting number to be 0, 1, or 2. mweeks@air:C_ch2$ cat example_switch.c /* Simple example showing a switch statement -MCW Sept. 2022 */ #include int main () { // Go through all possibilities for (int number=0; number<4; number++) { switch (number) { case 0: printf("Zero\n"); break; case 1: printf("One\n"); break; case 2: printf("Two\n"); break; default: printf("Expecting number to be 0, 1, or 2.\n"); } } return 0; } mweeks@air:C_ch2$ ls scanf_example.c ls: scanf_example.c: No such file or directory mweeks@air:C_ch2$ ls scanf_example1.c ls: scanf_example1.c: No such file or directory mweeks@air:C_ch2$ ls scanf_example* ls: scanf_example*: No such file or directory mweeks@air:C_ch2$ ls scanf_* scanf_ex1.c scanf_ex1_2024.c scanf_ex3.c mweeks@air:C_ch2$ cat scanf_ex1.c /* Simple example showing a scanf statement (user input) -MCW Sept. 2022 */ #include int main () { int number; // Give a prompt, so the user knows what to do. printf("Enter an integer: "); // "%d" says that we want a decimal number (integer) // The ampersand is important, and we'll talk about it in detail. scanf("%d", &number); // Just use number normally here, without the ampersand. printf("You entered %d \n", number); return 0; } mweeks@air:C_ch2$ gcc scanf_ex1.c -o scanf_ex1 mweeks@air:C_ch2$ ./scanf_ex1 Enter an integer: 8 You entered 8 mweeks@air:C_ch2$ ls rand* ls: rand*: No such file or directory mweeks@air:C_ch2$ vi random_example.c mweeks@air:C_ch2$ ls -l rand* -rw-r--r-- 1 mweeks staff 1069 Sep 18 12:54 random_example.c mweeks@air:C_ch2$ cat random_example.c /* Simple random number example -MCW Sept. 2022 */ #include // input, output #include // for random, srandom #include // for floor function #include // for time function int main () { // Call srandom to "seed" the random number generator. // See // https://stackoverflow.com/questions/16569239/how-to-use- // function-srand-with-time-h // for more detail about srandom and time. // Basically, time returns a different value each time we run it. // Use it to give us a non-predictable sequence of random numbers. unsigned int mytime; // get the current time mytime = time(NULL); // Seed the random number generator srandom(mytime); // random() returns a long integer. // Make sure to use random() instead of rand(). // Use % (modulus) to reduce it to something manageable, // in this case < 100. long r1 = random() % 100; // %li is for a long integer. %ld works too. printf("A random integer is %li.\n", r1); return 0; } mweeks@air:C_ch2$ vi random_example.c mweeks@air:C_ch2$ cat random_example.c /* Simple random number example -MCW Sept. 2022 */ #include // input, output #include // for random, srandom #include // for floor function #include // for time function int main () { // Call srandom to "seed" the random number generator. // See // https://stackoverflow.com/questions/16569239/how-to-use- // function-srand-with-time-h // for more detail about srandom and time. // Basically, time returns a different value each time we run it. // Use it to give us a non-predictable sequence of random numbers. unsigned int mytime; // get the current time mytime = time(NULL); // Seed the random number generator srandom(mytime); // random() returns a long integer. // Make sure to use random() instead of rand(). // Use % (modulus) to reduce it to something manageable, // in this case < 100. long r1 = random() % 100; // %li is for a long integer. %ld works too. printf("A random integer is %li.\n", r1); r1 = random() % 100; printf("A random integer is %li.\n", r1); r1 = random() % 100; printf("A random integer is %li.\n", r1); return 0; } mweeks@air:C_ch2$ vi random_example.c mweeks@air:C_ch2$ cat random_example.c /* Simple random number example -MCW Sept. 2022 Updated to print 3 total random values in Sept 2024 */ #include // input, output #include // for random, srandom #include // for floor function #include // for time function int main () { // Call srandom to "seed" the random number generator. // See // https://stackoverflow.com/questions/16569239/how-to-use- // function-srand-with-time-h // for more detail about srandom and time. // Basically, time returns a different value each time we run it. // Use it to give us a non-predictable sequence of random numbers. unsigned int mytime; // get the current time mytime = time(NULL); // Seed the random number generator srandom(mytime); // random() returns a long integer. // Make sure to use random() instead of rand(). // Use % (modulus) to reduce it to something manageable, // in this case < 100. long r1 = random() % 100; // %li is for a long integer. %ld works too. printf("A random integer is %li.\n", r1); r1 = random() % 100; printf("A random integer is %li.\n", r1); r1 = random() % 100; printf("A random integer is %li.\n", r1); return 0; } mweeks@air:C_ch2$ gcc random_example.c -o random_example mweeks@air:C_ch2$ ./random_example A random integer is 78. A random integer is 91. A random integer is 17. mweeks@air:C_ch2$ ./random_example A random integer is 80. A random integer is 15. A random integer is 15. mweeks@air:C_ch2$ ./random_example A random integer is 8. A random integer is 25. A random integer is 95. mweeks@air:C_ch2$ ./random_example A random integer is 59. A random integer is 42. A random integer is 59. mweeks@air:C_ch2$ ./random_example A random integer is 5. A random integer is 18. A random integer is 49. mweeks@air:C_ch2$ cp hello.c bool_ex1.c mweeks@air:C_ch2$ vi bool_ex1.c mweeks@air:C_ch2$ gcc bool_ex1.c -o bool_ex1 bool_ex1.c:13:14: warning: if statement has empty body [-Wempty-body] if (m==0) ; ^ bool_ex1.c:13:14: note: put the semicolon on a separate line to silence this warning 1 warning generated. mweeks@air:C_ch2$ vi bool_ex1.c mweeks@air:C_ch2$ gcc bool_ex1.c -o bool_ex1 mweeks@air:C_ch2$ ./bool_ex1 This is done regardless false, m=0. mweeks@air:C_ch2$ vi bool_ex1.c mweeks@air:C_ch2$ cat bool_ex1.c /* boolean example */ #include int main (int argc, char *argv[]) { char m; m = 0; if (m==1) ; printf("This is done regardless\n"); if (m) printf("true, m=%d.\n", (int)m); else printf("false, m=%d.\n", (int)m); return 0; } mweeks@air:C_ch2$ gcc bool_ex1.c -o bool_ex1 mweeks@air:C_ch2$ ./bool_ex1 This is done regardless false, m=0. mweeks@air:C_ch2$ cp bool_ex2.c usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory mweeks@air:C_ch2$ cp bool_ex1.c bool_ex2.c mweeks@air:C_ch2$ vi bool_ex2.c mweeks@air:C_ch2$ gcc bool_ex2.c -o bool_ex2 mweeks@air:C_ch2$ cat bool_ex2.c /* boolean example */ #include int main (int argc, char *argv[]) { int m; printf("Enter an int: "); scanf("%d", &m); if (m) printf("true, m=%d.\n", m); else printf("false, m=%d.\n", m); return 0; } mweeks@air:C_ch2$ ./bool_ex2 Enter an int: 0 false, m=0. mweeks@air:C_ch2$ ./bool_ex2 Enter an int: 1 true, m=1. mweeks@air:C_ch2$ ./bool_ex2 Enter an int: 3 true, m=3. mweeks@air:C_ch2$ ./bool_ex2 Enter an int: -1 true, m=-1. mweeks@air:C_ch2$ ./bool_ex2 Enter an int: 0.1 false, m=0. mweeks@air:C_ch2$ ./bool_ex2 Enter an int: -0.1 false, m=0. mweeks@air:C_ch2$ echo reads 0 reads 0 mweeks@air:C_ch2$ echo we could scanf for a float after this, which would be .1 we could scanf for a float after this, which would be .1 mweeks@air:C_ch2$ cp bool_ex2.c bool_ex3.c mweeks@air:C_ch2$ vi bool_ex3.c mweeks@air:C_ch2$ gcc bool_ex3.c -o bool_ex3 mweeks@air:C_ch2$ ./bool_ex3 false, m=0. mweeks@air:C_ch2$ cat bool_ex3.c /* boolean example */ #include int main (int argc, char *argv[]) { char m = 0; if (m) printf("true, m=%d.\n", m); else printf("false, m=%d.\n", m); return 0; } mweeks@air:C_ch2$ echo notice that the printf uses %d instead of %c notice that the printf uses %d instead of %c mweeks@air:C_ch2$ echo ASCII values ASCII values mweeks@air:C_ch2$ exit exit Script done on Wed Sep 18 13:43:00 2024