Script started on Wed Sep 25 12:30:59 2024 mweeks@air:C_ch3$ echo ">> disp(sprintf('%tx \n', 5.6))" >> disp(sprintf('%tx \n', 5.6)) mweeks@air:C_ch3$ echo this is matlab this is matlab mweeks@air:C_ch3$ echo 40b33333 40b33333 mweeks@air:C_ch3$ echo binary: 0100 0000 1011 0011 0011 0011 0011 0011 binary: 0100 0000 1011 0011 0011 0011 0011 0011 mweeks@air:C_ch3$ echo IEEE 754 standard for float IEEE 754 standard for float mweeks@air:C_ch3$ echo sign:0 exponent:100 0000 1 significand: 011 0011 0011 0011 0011 0011 sign:0 exponent:100 0000 1 significand: 011 0011 0011 0011 0011 0011 mweeks@air:C_ch3$ echo sign: positive, exponent: 2, hidden bit 1.01100110011001100110011 sign: positive, exponent: 2, hidden bit 1.01100110011001100110011 mweeks@air:C_ch3$ echo result: 101.100110011001100110011 result: 101.100110011001100110011 mweeks@air:C_ch3$ echo "result: 2^2*1 + 2^1*0 + 2^0*1 + 2^(-1)*1 + 2^(-2)*0 + 2^(-3)*0 + 2^(-4)*1 ..." result: 2^2*1 + 2^1*0 + 2^0*1 + 2^(-1)*1 + 2^(-2)*0 + 2^(-3)*0 + 2^(-4)*1 ... mweeks@air:C_ch3$ echo 5 + 1/2 + 1/16 5 + 1/2 + 1/16 mweeks@air:C_ch3$ echo 5.5625 5.5625 mweeks@air:C_ch3$ ls *.c addfrac.c rand_ex1.c switch_example.c bitwise.c scanf_ex1.c expr.c scanf_multiple.c mweeks@air:C_ch3$ cat scanf_multiple.c /* Simple example showing a scanf statement (user input) this gets multiple values. -MCW Sept. 2024 */ #include int main () { int number, count; // Give a prompt, so the user knows what to do. printf("Enter one or more integers: "); count = 1; // To make sure we try reading at least once. while (count >= 1) { count = scanf("%d", &number); if (count >= 1) printf("You entered %d \n", number); } return 0; } mweeks@air:C_ch3$ gcc scanf_multiple.c mweeks@air:C_ch3$ ./a.out Enter one or more integers: 34 You entered 34 45 You entered 45 56 You entered 56 -2 You entered -2 mweeks@air:C_ch3$ echo "98 76 54 43" | ./a.out Enter one or more integers: You entered 98 You entered 76 You entered 54 You entered 43 mweeks@air:C_ch3$ cp scanf_multiple.c bad_printfs.c mweeks@air:C_ch3$ vi bad_printfs.c mweeks@air:C_ch3$ cp bad_printfs.c temp.c mweeks@air:C_ch3$ vi temp.c mweeks@air:C_ch3$ vi temp.c mweeks@air:C_ch3$ vi temp2.c mweeks@air:C_ch3$ cd mweeks@air:~$ ls -al | grep vi drwxr-xr-x 3 mweeks staff 96 Sep 27 2019 .IdentityService drwx------ 3 mweeks staff 96 Apr 16 2020 .ServiceHub drwxr-xr-x 4 mweeks staff 128 Feb 23 2024 .vim -rw------- 1 mweeks staff 34092 Sep 25 13:22 .viminfo drwxr-xr-x@ 8 mweeks staff 256 Mar 5 2020 2D-Entities-Starter-0.22.0-preview.7 drwxr-xr-x 5 mweeks staff 160 Mar 15 2024 Movies mweeks@air:~$ cd Desktop/csc3320/programs/C_ch3 mweeks@air:C_ch3$ ls *c addfrac.c expr.c scanf_multiple.c bad_printfs.c rand_ex1.c switch_example.c bitwise.c scanf_ex1.c temp.c mweeks@air:C_ch3$ cat temp.c /* Simple example showing a scanf statement (user input) this gets multiple values. -MCW Sept. 2024 */ #include int main () { int number=2, count=3; return 0; } mweeks@air:C_ch3$ cat bad_printfs.c /* Simple example showing a scanf statement (user input) this gets multiple values. -MCW Sept. 2024 */ #include int main () { int number=2, count=3; printf("You entered %d \n", number); // OK printf("You entered %d \n", number); return 0; } mweeks@air:C_ch3$ vi bad_printfs.c temp.c 2 files to edit mweeks@air:C_ch3$ vi bad_printfs.c mweeks@air:C_ch3$ gcc bad_printfs.c bad_printfs.c:14:39: warning: data argument not used by format string [-Wformat-extra-args] printf("You entered %d \n", number, count); // bad ~~~~~~~~~~~~~~~~~~~ ^ bad_printfs.c:15:27: warning: more '%' conversions than data arguments [-Wformat] printf("You entered %d %d \n", count); // bad ~^ 2 warnings generated. mweeks@air:C_ch3$ ./a.out You entered 2 You entered 2 You entered 3 515 mweeks@air:C_ch3$ ls Sept25_2024.log bitwise.c scanf_ex1.s a.out expr.c scanf_multiple.c addfrac.c rand_ex1.c switch_example.c bad_printfs.c scanf_ex1.c temp.c mweeks@air:C_ch3$ cp bad_printfs.c bad_printfs2.c mweeks@air:C_ch3$ vi bad_printfs2.c mweeks@air:C_ch3$ gcc bad_printfs2.c bad_printfs2.c:12:34: warning: format specifies type 'int' but the argument has type 'float' [-Wformat] printf("You entered %d %f \n", num2, count); // bad ~~ ^~~~ %f bad_printfs2.c:12:40: warning: format specifies type 'double' but the argument has type 'int' [-Wformat] printf("You entered %d %f \n", num2, count); // bad ~~ ^~~~~ %d 2 warnings generated. mweeks@air:C_ch3$ ./a.out You entered 3 5.600000 mweeks@air:C_ch3$ cat bad_printfs2.c /* -MCW Sept. 2024 */ #include int main () { int number=2, count=3; float num2 = 5.6; printf("You entered %d %f \n", num2, count); // bad return 0; } mweeks@air:C_ch3$ ls Sept25_2024.log bitwise.c scanf_multiple.c a.out expr.c switch_example.c addfrac.c rand_ex1.c temp.c bad_printfs.c scanf_ex1.c bad_printfs2.c scanf_ex1.s mweeks@air:C_ch3$ cp scanf_ex1.c bad_scanf_ex1.c mweeks@air:C_ch3$ vi bad_scanf_ex1.c mweeks@air:C_ch3$ cat bad_scanf_ex1.c /* */ #include int main () { int number; printf("Enter an integer: "); scanf("%d", number); % I removed the & printf("You entered %d \n", number); return 0; } mweeks@air:C_ch3$ gcc bad_scanf_ex1.c bad_scanf_ex1.c:11:15: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat] scanf("%d", number); % I removed the & ~~ ^~~~~~ bad_scanf_ex1.c:11:26: error: expected expression scanf("%d", number); % I removed the & ^ bad_scanf_ex1.c:11:28: error: use of undeclared identifier 'I' scanf("%d", number); % I removed the & ^ 1 warning and 2 errors generated. mweeks@air:C_ch3$ vi bad_scanf_ex1.c mweeks@air:C_ch3$ cat bad_scanf_ex1.c /* */ #include int main () { int number; printf("Enter an integer: "); scanf("%d", number); // I removed the & printf("You entered %d \n", number); return 0; } mweeks@air:C_ch3$ gcc bad_scanf_ex1.c bad_scanf_ex1.c:11:15: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat] scanf("%d", number); // I removed the & ~~ ^~~~~~ 1 warning generated. mweeks@air:C_ch3$ ./a.out Enter an integer: 33 Segmentation fault: 11 mweeks@air:C_ch3$ vi bad_scanf_ex1.c mweeks@air:C_ch3$ cat bad_scanf_ex1.c /* */ #include int main () { int number; printf("Enter an integer: "); // scanf("%d", number); // I removed the & // printf("You entered %d \n", number); return 0; } mweeks@air:C_ch3$ gcc bad_scanf_ex1.c mweeks@air:C_ch3$ ./a.out Enter an integer: mweeks@air:C_ch3$ mweeks@air:C_ch3$ cat addfrac.c /* Adds two fractions */ #include int main(void) { int num1, denom1, num2, denom2; int result_num, result_denom; printf("Enter first fraction: "); scanf("%d/%d", &num1, &denom1); printf("Enter second fraction: "); scanf("%d/%d", &num2, &denom2); result_num = num1 * denom2 + num2 *denom1; result_denom = denom1 * denom2; printf("The sum is %d/%d\n", result_num, result_denom); return 0; } mweeks@air:C_ch3$ gcc addfrac.c mweeks@air:C_ch3$ ./a.out Enter first fraction: 2/3 Enter second fraction: 3/4 The sum is 17/12 mweeks@air:C_ch3$ ls Sept25_2024.log bad_scanf_ex1.c scanf_ex1.s a.out bitwise.c scanf_multiple.c addfrac.c expr.c switch_example.c bad_printfs.c rand_ex1.c temp.c bad_printfs2.c scanf_ex1.c mweeks@air:C_ch3$ cp scanf_ex1.c scanf_ex2.c mweeks@air:C_ch3$ vi scanf_ex2.c mweeks@air:C_ch3$ vi scanf_ex2.c mweeks@air:C_ch3$ gcc scanf_ex2.c mweeks@air:C_ch3$ cat scanf_ex2.c /* Simple example showing a scanf statement (user input) -MCW Sept. 2022 */ #include int main () { int i, j; float x, y; printf("Enter a 2 integers and 2 floats: "); scanf("%d%d%f%f", &i, &j, &x, &y); printf("You entered %d %d \n", i, j); printf("You entered %f %f \n", x, y); return 0; } mweeks@air:C_ch3$ ./a.out Enter a 2 integers and 2 floats: 2 3 4.1 5.2 You entered 2 3 You entered 4.100000 5.200000 mweeks@air:C_ch3$ echo "2 3 4.1 5.2" | ./a.out Enter a 2 integers and 2 floats: You entered 2 3 You entered 4.100000 5.200000 mweeks@air:C_ch3$ echo "1-20.3-4.0e3" | ./a.out Enter a 2 integers and 2 floats: You entered 1 -20 You entered 0.300000 -4000.000000 mweeks@air:C_ch3$ exit exit Script done on Wed Sep 25 13:49:29 2024