Script started on Mon Sep 15 17:27:35 2025 macbook:logs> cd ../programs/C_ch2 macbook:C_ch2> echo code for today is 461 code for today is 461 macbook:C_ch2> cat testfloat3b.c #include int main() { int v; v = 017; printf(" 017 Value is %d decimal \n", v); printf("v in octal is %o\n", v); return 0; } macbook:C_ch2> gcc testfloat3b.c macbook:C_ch2> ./a.out 017 Value is 15 decimal v in octal is 17 macbook:C_ch2> vi simpleIO.c macbook:C_ch2> gcc simpleIO.c macbook:C_ch2> cat simpleIO.c #include int main() { unsigned int u; printf("enter an unsigned int\n"); scanf("%u", &u); printf("Read this value: %u\n", u); return 0; } macbook:C_ch2> ./a.out enter an unsigned int 65 Read this value: 65 macbook:C_ch2> cat testchar.c #include int main() { char ch; printf("Enter a char: "); scanf("%c", &ch); printf("You entered '%c' which is %d in decimal \n", ch, ch); return 0; } macbook:C_ch2> gcc testchar.c macbook:C_ch2> ./a.out Enter a char: 5 You entered '5' which is 53 in decimal macbook:C_ch2> ./a.out Enter a char: x You entered 'x' which is 120 in decimal macbook:C_ch2> ./a.out Enter a char: a You entered 'a' which is 97 in decimal macbook:C_ch2> ./a.out Enter a char: A You entered 'A' which is 65 in decimal macbook:C_ch2> ./a.out Enter a char: You entered ' ' which is 10 in decimal macbook:C_ch2> vi type_conv.c macbook:C_ch2> gcc type_conv.c macbook:C_ch2> ./a.out c is 3.550000 d is 3 % macbook:C_ch2> cat type_conv.c #include int main() { int a=1, d=0; double b=2.55, c=0; c = a + b; // a is converted to double printf("c is %f\n ", c); d = a + b; //is illegal printf("d is %d\n ", d); return 0; } macbook:C_ch2> vi type_conv.c macbook:C_ch2> cat type_conv.c #include int main() { int a=1, d=0; double b=2.55, c=0; c = a + b; // a is converted to double printf("c is %f\n ", c); d = a + b; //is illegal printf("d is %d\n", d); printf("\n"); return 0; } macbook:C_ch2> gcc type_conv.c macbook:C_ch2> ./a.out c is 3.550000 d is 3 macbook:C_ch2> cat testfrac.c #include int main() { char ch; float f, frac_part; f = 3.5; frac_part = f - (int)f; printf("frac_part = %f\n", frac_part); float quotient; int dividend = 7, divisor = 3; quotient = (float) dividend/ divisor; printf("quotient = %f\n", quotient); quotient = (float) (dividend/ divisor); printf("quotient = %f\n", quotient); return 0; } macbook:C_ch2> gcc testfrac.c macbook:C_ch2> ./a.out frac_part = 0.500000 quotient = 2.333333 quotient = 2.000000 macbook:C_ch2> cat typedef.c /* typedef allows for user-defined types */ #include typedef short int Int16; int main (void) { Int16 a = 4; printf("the number is %d\n", a); return 0; } macbook:C_ch2> gcc typedef.c macbook:C_ch2> ./a.out the number is 4 macbook:C_ch2> cat typedef2.c /* typedef allows for user-defined types */ #include typedef struct { int age; char *name; } person; person people; int main (void) { printf("this is a pertial example\n"); return 0; } macbook:C_ch2> gcc typedef2.c macbook:C_ch2> ./a.out this is a pertial example macbook:C_ch2> vi typedef2.c macbook:C_ch2> cat typedef2.c /* typedef allows for user-defined types */ #include typedef struct { int age; char *name; } person; person people; int main (void) { printf("this is a partial example\n"); return 0; } macbook:C_ch2> vi for_example.c macbook:C_ch2> cat for_example.c #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"); } */ printf(" %d\n", number); } return 0; } macbook:C_ch2> gcc for_example.c macbook:C_ch2> ./a.out 0 1 2 3 macbook:C_ch2> vi switch_example.c macbook:C_ch2> gcc switch_example.c macbook:C_ch2> ./a.out Zero One Two Expecting number to be 0, 1, or 2. macbook:C_ch2> cat switch_example.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; } macbook:C_ch2> echo edit the program edit the program macbook:C_ch2> vi switch_example.c macbook:C_ch2> gcc switch_example.c macbook:C_ch2> cat switch_example.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; } macbook:C_ch2> ./a.out Zero One Two Two Expecting number to be 0, 1, or 2. macbook:C_ch2> ls rand* random_example.c macbook: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; } macbook:C_ch2> gcc random_example.c macbook:C_ch2> ./a.out A random integer is 44. A random integer is 21. A random integer is 45. macbook:C_ch2> ./a.out A random integer is 9. A random integer is 18. A random integer is 79. macbook:C_ch2> ./a.out A random integer is 86. A random integer is 41. A random integer is 24. macbook:C_ch2> exit Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. Script done on Mon Sep 15 18:44:06 2025