CS 202 f2017 quiz 3. cs202xy login: _____________________________________________ Note: answers put in // line afterwards, and sometimes some notes in the problems about why/how Note: for each, you should copy/paste the original 1. For each, give the value printed. 1a) char c = 97; printf("%c", c); // a 1b) char c = 97; printf("%i", c); // 97 1c) char c = 127; printf("%i", c << 1); // 127*2 = 254 1d) char c = 127; printf("%i", c & 15); // 1111 1111b & 1111b = 1111b = 15 1e) int x = (1 < 2) && (2 < 1); 1 && 0 0 printf("%i", ++x); // 1 1f) int x = (1 < 2) | (2 < 1); 1 | 0 1 printf("%i", x++); // 1 is printed, final value of x is 2 1g) int x = 1; switch (x) { case 1: x++; case 2: x++; case 3: x++; default: x++; } printf("%i", x); // case 1 happens, then the rest do as well because there is no break // final value of x is 5 1h) for(int i=0; i < 5; i++) ; printf("%i", i); // final value of i is 5 - the value causing the "i < 5" condition to be false 1i) int x=0; while((x=1) > 2) // x is set to 1 and condition is false x++; printf("%i", x); // 1 1j) char c = 127; c++; // -127 because of overflow printf("%i", c); 2) Give the min, max, and # bytes for each of the following data types on CS with gcc *** NOTE *** in these problems I am using ^ to mean exponent. 2a) unsigned char 0 to 255, 1 byte 2b) char -128 to 127, 1 byte 2c) int -2^31 to 2^31-1, 4 bytes roughly -2 billion to 2 billion 2d) unsigned int 0 to 2^32-1, 4 bytes roughly 0 to 4 billion 2e) short -2^15 to 2^15-1, 2 bytes roughly -32000 to 32000 2f) unsigned short 0 to 2^16-1, 2 bytes roughly 0 to 65000 2g) long int -2^63 to 2^63-1, 8 bytes roughly 8*10^18 to 8*10^18 2h) unsigned long int 0 to 2^64-1, 8 bytes roughly 0 to 16*10^18 2i) float - max/min, precision (# digits) roughly -10^127 to 10^127, roughly 7 digits of precision, exponent goes between -126 and 127. 4 bytes 2j) double roughly -10^1023 to 10^1023, roughly 15 digits of precision, exponent goes between -1022 and 1023 8 bytes 2k) long double roughly -10^16000 to 10^16000, roughly 34 digits of precision, exponent goes between roughly -16000 and 16000 16 bytes 3) Give an extremely short phrase (at most 10 words) describing what each keyword does 3a) static, for local variable - global storage, local scope 3b) static, for global variable - cannot be used in separate .c files 3c) const - variable value cannot be changed after initialized 3d) while - loop while condition is true 3e) #define - set a pre-processor "variable" (called a "macro") e.g., #define TRUE 1 3f) sizeof - # of bytes in a variable or data type 3g) extern, for global variable - variable is declared in another .c file 3h) enum - integer type with named values e.g., typedef enum FRUIT_T {apple, banana, orange} fruit_t; 3i) typedef - make a shorcut for a data type (often used with struct, enum) 3j) union - type that allows one variable that could take multiple types e.g., typedef struct GRADE_T { char type; // 'i' for int, 'f' for float, 'l' for letter union grade { int i; float f; char l; }; } grade_t; grade_t g; g.type = 'f'; g.grade.f = 97.23; 3k) struct - data type for grouping variables together into a single variable 3l) for - loop while condition is true, includes initializer / condition / body / increment. 3m) switch - run different statements depending on value of integer expression 3n) break - stop innermost loop the break is in, skip to end of loop 3o) continue - go to top of innermost loop continue is in, and check the loop condition 3p) default - for switch statement, statements that run if expression doesn't match any case statements