Script started on Mon Sep 23 12:30:54 2024 mweeks@air:C_ch2$ echo int constant 17 3 -54 int constant 17 3 -54 mweeks@air:C_ch2$ echo int octal number 017 03 033 int octal number 017 03 033 mweeks@air:C_ch2$ cat testfloat3.c #include int main() { int v; v = 17; printf(" 17 Value is %d \n", v); v = 017; printf(" 017 Value is %d \n", v); v = 0x17; printf(" 0x17 Value is %d \n", v); long int v2 = 17L; printf(" 17L Value is %ld \n", v2); return 0; } mweeks@air:C_ch2$ gcc testfloat3.c mweeks@air:C_ch2$ ./a.out 17 Value is 17 017 Value is 15 0x17 Value is 23 17L Value is 17 mweeks@air:C_ch2$ cat testfloat2.c #include int main() { float profit; int myarray[10]; // Am not defining ... printf("Profit is %8.2f\n", profit); for (int i=0; i<10; i++) { printf("array[%d] is %x\n", i, myarray[i]); } return 0; } mweeks@air:C_ch2$ gcc testfloat2.c mweeks@air:C_ch2$ ./a.out Profit is 0.00 array[0] is 0 array[1] is 0 array[2] is 0 array[3] is 0 array[4] is 0 array[5] is 0 array[6] is 0 array[7] is 0 array[8] is ee6f3a38 array[9] is 7ffe mweeks@air:C_ch2$ echo wi wi mweeks@air:C_ch2$ ech iw bash: ech: command not found mweeks@air:C_ch2$ echo iw iw mweeks@air:C_ch2$ echo %u unsigned %u unsigned mweeks@air:C_ch2$ echo %x hexadecimal %x hexadecimal mweeks@air:C_ch2$ echo %o for octal %o for octal mweeks@air:C_ch2$ cp testfloat3.c testfloat3b.c mweeks@air:C_ch2$ vi testfloat3b.c mweeks@air:C_ch2$ gcc testfloat3b.c mweeks@air:C_ch2$ ./a.out 017 Value is 15 decimal v in octal is 17 mweeks@air:C_ch2$ cp testfloat3b.c testchar.c mweeks@air:C_ch2$ vi testchar.c mweeks@air: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; } mweeks@air: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; } mweeks@air:C_ch2$ gcc testchar.c mweeks@air:C_ch2$ ./a.out Enter a char: A You entered 'A' which is 65 in decimal mweeks@air:C_ch2$ ./a.out Enter a char: You entered ' ' which is 32 in decimal mweeks@air:C_ch2$ grep divide *.c mweeks@air:C_ch2$ cp testchar.c testfrac.c mweeks@air:C_ch2$ vi testfrac.c mweeks@air: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); return 0; } mweeks@air:C_ch2$ gcc testfrac.c mweeks@air:C_ch2$ ./a.out frac_part = 0.500000 quotient = 2.333333 mweeks@air:C_ch2$ vi testfrac.c mweeks@air: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; } mweeks@air:C_ch2$ gcc testfrac.c mweeks@air:C_ch2$ ./a.out frac_part = 0.500000 quotient = 2.333333 quotient = 2.000000 mweeks@air:C_ch2$ grep typedef *.c typedef.c: typedef allows for user-defined types typedef.c:typedef short int Int16; mweeks@air: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; } mweeks@air:C_ch2$ gcc typedef.c mweeks@air:C_ch2$ ./a.out the number is 4 mweeks@air:C_ch2$ cp typedef.c typedef2.c mweeks@air:C_ch2$ vi typedef2.c mweeks@air:C_ch2$ gcc typedef2.c mweeks@air: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; } mweeks@air:C_ch2$ ./a.out this is a pertial example mweeks@air:C_ch2$ grep scanf *.c bool_ex2.c: scanf("%d", &m); float2hex.c: scanf("%f", &myval); io_example.c: scanf("%d", &n); io_example2.c: scanf("%d", &n); scanf_ex1.c: Simple example showing a scanf statement (user input) scanf_ex1.c: scanf("%d", &number); scanf_ex1_2024.c: scanf scanf_ex1_2024.c: count = scanf("%d", &num); scanf_ex3.c: Example using scanf scanf_ex3.c: rv = scanf("%d", &a); scanf_ex3.c: rv = scanf("%d", &a); testchar.c: scanf("%c", &ch); testfloat1.c: scanf("%u", &u); /* reads u in base 10 */ testfloat1.c: scanf("%o", &u); /* reads u in base 8 */ testfloat1.c: scanf("%x", &u); /* reads u in base 16 */ mweeks@air:C_ch2$ cp scanf_ex1_2024.c scanf_ex2_2024.c mweeks@air:C_ch2$ vi scanf_ex2_2024.c mweeks@air:C_ch2$ gcc scanf_ex2_2024.c mweeks@air:C_ch2$ ./a.out Enter a number 45 Scanf returned value 1, and number is 45 mweeks@air:C_ch2$ echo "12" | ./a.out Enter a number Scanf returned value 1, and number is 12 mweeks@air:C_ch2$ vi scanf_ex2_2024.c mweeks@air:C_ch2$ gcc scanf_ex2_2024.c mweeks@air:C_ch2$ cat scanf_ex2_2024.c /* scanf */ #include int main (int argc, char *argv[]) { int num,count = 0; printf("Enter 10 numbers\n"); // in-line comment for (int i=0; i<10; i++) { count = scanf("%d", &num); printf("Scanf returned value %d, and number is %d\n", count, num); } return 0; } mweeks@air:C_ch2$ ./a.out Enter 10 numbers 1 Scanf returned value 1, and number is 1 2 Scanf returned value 1, and number is 2 3 Scanf returned value 1, and number is 3 4 Scanf returned value 1, and number is 4 5 Scanf returned value 1, and number is 5 6 Scanf returned value 1, and number is 6 7 Scanf returned value 1, and number is 7 8 Scanf returned value 1, and number is 8 9 Scanf returned value 1, and number is 9 10 Scanf returned value 1, and number is 10 mweeks@air:C_ch2$ echo "1 2 3 4 5 6 7 8 9 10" | ./a.out Enter 10 numbers Scanf returned value 1, and number is 1 Scanf returned value 1, and number is 2 Scanf returned value 1, and number is 3 Scanf returned value 1, and number is 4 Scanf returned value 1, and number is 5 Scanf returned value 1, and number is 6 Scanf returned value 1, and number is 7 Scanf returned value 1, and number is 8 Scanf returned value 1, and number is 9 Scanf returned value 1, and number is 10 mweeks@air:C_ch2$ echo "1 2 3 4 5 6 7 8 9 10" > scanf_echo.txt mweeks@air:C_ch2$ ./a.out < scanf_echo.txt Enter 10 numbers Scanf returned value 1, and number is 1 Scanf returned value 1, and number is 2 Scanf returned value 1, and number is 3 Scanf returned value 1, and number is 4 Scanf returned value 1, and number is 5 Scanf returned value 1, and number is 6 Scanf returned value 1, and number is 7 Scanf returned value 1, and number is 8 Scanf returned value 1, and number is 9 Scanf returned value 1, and number is 10 mweeks@air:C_ch2$ exit exit Script done on Mon Sep 23 13:45:56 2024