EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Summer 2018 Lecture 2: Operators Basic variable output with printf() Basic variable input with scanf()
ECE Application Programming: Lecture 2 Lecture outline Announcements/reminders Program 1 due Friday, 5/25 10 points: register for access to the course textbook 10 points: introduce yourself to your instructor 30 points: complete simple C program Sign up for the course discussion group! Review Basic program structure Data types Variables Today’s lecture Operators Basic variable output with printf() Basic variable input with scanf() 11/8/2019 ECE Application Programming: Lecture 2
Review: Basic C program structure Preprocessor directives #include: typically used to specify library files Main program Starts with: int main() or void main() Enclosed in block: specified by { } Ends with return 0; Indicates successful completion Optional if main() is void Doesn’t return value: return; Basic output Call printf(<string>); <string> can be replaced by characters enclosed in double quotes May include escape sequence, e.g. \n (new line) 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 Review: Variables Four basic data types int, float, double, char Variables Have name, type, value, memory location Variable declarations: examples int x; float a, b; double m = 2.35; Assignments: examples with variables above a = 7.5; x = a + 2; x = 9, not 9.5 m = m – 1; m = 1.35 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 1 Variables - assigning varname = expression; Declared variable single variable on left side of = expression any legal expression Expression can be constant, variable, function call, arithmetic operation, etc. Variable type (int, float, etc) and expression result type should match If not, funny things can happen ... 11/8/2019 ECE Application Programming: Lecture 1
ECE Application Programming: Lecture 1 Variables (cont.) var name memory loc main() { float hours, payrate; float grosspay; int j; hours = 40.0; hours 40.0 4278 payrate ? 427C 4280 grosspay ? j ? 4284 11/8/2019 ECE Application Programming: Lecture 1
ECE Application Programming: Lecture 1 Variables (cont.) var name memory loc main() { float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; hours 40.0 4278 payrate 20.0 427C 4280 grosspay ? j ? 4284 11/8/2019 ECE Application Programming: Lecture 1
ECE Application Programming: Lecture 1 Variables (cont.) var name memory loc main() { float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate; hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j ? 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive). 11/8/2019 ECE Application Programming: Lecture 1
ECE Application Programming: Lecture 1 Variables (cont.) var name memory loc main() { float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate; j = 5; hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j 5 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive). 11/8/2019 ECE Application Programming: Lecture 1
ECE 160 - Introduction to Computer Engineering I 02/09/2005 Variables (cont.) var name memory loc main() { float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate j = 5; j = j + 1; hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j 5 6 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive). 11/8/2019 ECE Application Programming: Lecture 1 (c) 2005, P. H. Viall
ECE Application Programming: Lecture 1 Example: Variables What values do w, x, y, and z have at the end of this program? int main() { int w = 5; float x; double y; char z = ‘a’; x = 8.579; y = -0.2; w = x; y = y + 3; z = w – 5; return 0; } 11/8/2019 ECE Application Programming: Lecture 1
ECE Application Programming: Lecture 1 Example solution int main() { int w = 5; float x; double y; char z = ‘a’; x = 8.579; y = -0.2; w = x; y = y + 3; z = w – 5; return 0; } w = 5 z = ‘a’ (ASCII value 97) x = 8.579 y = -0.2 w = 8 (value is truncated) y = (-0.2) + 3 = 2.8 z = 8 – 5 = 3 11/8/2019 ECE Application Programming: Lecture 1
Arithmetic Operations Operator Addition + Subtraction - Multiplication * Division / Modulus Division (Remainder) % 11/8/2019 ECE Application Programming: Lecture 2
Results of arithmetic operations 3+7 10 - 3.0 15.0 (using non-integer makes result double precision) 12.62 + 9.8 22.42 .08*12.3 0.984 12.0/ 2.0 6.0 10/5 2 10/3 3 (not 3.333…) 10 % 3 1 12 % 5 2 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 Operators (cont.) Previous operators are binary Deal with two values C also supports some unary operators For now, we’ll simply deal with unary negation e.g., if x = 3, the statement -x; produces the value -3 Important note: The statement above does not change the value of x 11/8/2019 ECE Application Programming: Lecture 2
Operators and variables Operators can be used either with constants or variables Examples: int main() { int w, x, y, z; w = 3 + 2; // w = 5 x = -w; // x = -5 y = x – 7; // y = -12 z = w * y; // z = -60 return 0; } 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 Operators (cont.) More complex statements are allowed e.g. x = 1 + 2 - 3; Parentheses help you prioritize parts of statement Makes difference with order of operations x = 1 + 2 * 3; is different than x = (1 + 2) * 3; 11/8/2019 ECE Application Programming: Lecture 2
Example: Arithmetic operations Evaluate each of the following expressions, including the type (int or double) in your answer 19/3 3/19 19%3 3%19 5 + 7/2 5.0 + 7/2 5 + 7.0/2 5 * 3 % 3 / 6 + 14 + 10 / 2 5 * (3 % 3) / 6 + 14.0 + 10/3 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 Example solution 19/3 = 6 (integer division) 3/19 = 0 (integer division) 19%3 = 1 3%19 = 3 5 + 7/2 = 5 + 3 = 8 5.0 + 7/2 = 5.0 + 3 = 8.0 5 + 7.0/2 = 5 + 3.5 = 8.5 11/8/2019 ECE Application Programming: Lecture 2
Example solution (cont.) For each of the following, underlined part(s) evaluated first at each step 5 * 3 % 3 / 6 + 14 + 10 / 2 = 15 % 3 / 6 + 14 + 5 = 0 / 6 + 14 + 5 = 0 + 14 + 5 = 19 5 * (3 % 3) / 6 + 14.0 + 10/3 = 5 * 0 / 6 + 14.0 + 10/3 = 0 / 6 + 14.0 + 3 = 0 + 14.0 + 3 = 17.0 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 I/O basics Need ability to Print variables (or results calculated using them) Read values from input Output: printf() Already seen basics Input: scanf() 11/8/2019 ECE Application Programming: Lecture 2
Basic printf() formatting To print variables/constants, insert %<type> (format specifier) in your format string %c: single character %d or %i: signed decimal integer %f: float; %lf: double Prints 6 digits after decimal point by default To control # digits, use precision "%.4lf" prints with 4 digits (4th digit rounds) "%.0lf" prints with 0 digits (round to nearest integer) When printed, format specifier is replaced by value of corresponding expression If x is 3, printf("x + x = %d", x + x) prints: x + x = 6 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 printf() example float a=67.49,b=9.999925; printf("hello %f there %f\n",a,b); printf("%f%f%f%f\n",a,a,b,b); printf("a=%.2f, b=%.1f",a,b); printf("Cool huh?\n"); Printed: hello 67.490000 there 9.999925 67.49000067.4900009.9999259.999925 a=67.49, b=10.0Cool huh? 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 Example: printf() Show the output from each programs(assume #include <stdio.h> for all) void main() { int i=2, j=3, k, m; k = j * i; m = i + j; printf("%d %d %d %d\n", i, j, k, m); } double f, g; f = 1.0 / 4.0; g = f * 20; printf("f = %lf,\ng = %.2lf\n", f, g); int a = 5, b = 2; printf("Output%doesn't%dmake%dsense", a, b, a + b); 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 Example solution void main() { int i=2, j=3, k, m; k = j * i; k = 2 * 3 = 6 m = i + j; m = 2 + 3 = 5 printf("%d %d %d %d\n", i, j, k, m); } Output: 2 3 6 5 11/8/2019 ECE Application Programming: Lecture 2
Example solution (cont.) void main() { double f, g; f = 1.0 / 4.0; f = 0.25 g = f * 20; g = 0.25 * 20 = 5 printf("f = %lf,\ng = %.2lf\n", f, g); } Output: f = 0.250000, g = 5.00 (remember, 6 places after decimal point printed by default with floating-point data) 11/8/2019 ECE Application Programming: Lecture 2
Example solution (cont.) void main() { int a = 5, b = 2; printf("Output%doesn't%dmake%dsense", a, b, a + b); } Output: Output5oesn't2make7sense (Every %d gets replaced with a number, which is underlined above to show what happens—in practice, the console isn’t going to underline your output!) 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 printf() details Detailed slides on printf() follow Skip these if you don’t want to go overboard with the full details of how the function works 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 02/07/2005 printf() Documentation info: int printf(const char *format [,argument] ...) Type of value returned Name of function ( ) indicate printf is a function First arg type and formal name (required, since no brackets) next argument type and name (in this case it may be any simple type) [ ] indicate optional arguments … indicates previous argument repeated zero or more times 11/8/2019 ECE Application Programming: Lecture 2 (c) 2005, P. H. Viall
ECE Application Programming: Lecture 2 printf() int printf(const char *format [,argument] ...) Type of value returned (int in this case) All functions return at most one value. The type void is used to indicate a function returns no value There is no requirement to use the value returned. The printf() function returns the number of characters printed (including spaces); returns negative value if error occurs. 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 printf() int printf(const char *format [,argument] ...) Name of function; printf( ) in this case A function name is ALWAYS followed by a set of (), even if the function takes no arguments 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 printf() int printf(const char *format [,argument] ...) Type (const char *) and name (format) of first argument For the moment, const char * can be thought of as a series of characters enclosed in double quotes The name format may be thought of as a code indicating how the arguments are to be interpreted, and how the output should look. 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 printf() int printf(const char *format [,argument] ...) zero of more optional arguments, each preceded by a comma zero because of the … optional because of the [ ] 11/8/2019 ECE Application Programming: Lecture 2
ECE 160 - Introduction to Computer Engineering I 02/09/2005 scanf() function Used to get input from user Returns number of items successfully assigned First argument is format specifiers Essentially same as printf() format string Every format specifier (%d, %lf, etc.) corresponds to an input value to be read Format string can contain other characters, which will be ignored if they are present If they’re not, you have a problem … Remaining arguments are variable addresses Use “address of” operator: & For example, given: int a; The address of a is: &a 11/8/2019 ECE Application Programming: Lecture 2 (c) 2005, P. H. Viall
ECE 160 - Introduction to Computer Engineering I 02/09/2005 scanf() function Documentation info: int scanf(const char *format [,argument] ...) format - is format specifiers similar to printf() specifiers arguments - are ADDRESSES of where to store what the user enters 11/8/2019 ECE Application Programming: Lecture 2 (c) 2005, P. H. Viall
ECE 160 - Introduction to Computer Engineering I 02/09/2005 scanf() function int hours; float rate; scanf("%d %f",&hours,&rate); If user types: 34 5.7 hours ? 1284 rate ? 1288 hours 34 1284 rate 5.7 1288 11/8/2019 ECE Application Programming: Lecture 2 (c) 2005, P. H. Viall
scanf() format strings scanf() will skip space characters for all types but %c Read input until it finds something that’s not a space, then see if it matches the desired type If type matches, value will be stored in specified variable If type doesn’t match, nothing stored; function stops Space in string only matters if using %c %c will read any character Includes spaces, newlines, etc. Example: given scanf("%d%c", &i, &c); Input: 3a i = 3, c = 'a' Input: 3 a i = 3, c = ' ' Input: 3 a i = 3, c = '\n' (assuming newline directly after 3) 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 scanf() return value scanf() returns # of successfully read items Ex.: given scanf("%d%d", &x, &y); Input: 3 7 x = 3, y = 7, return value = 2 Input: 3 7.2 x = 3, y = 7, return value = 2 Input: 3 .3 x = 3, y = ?, return value = 1 y is unchanged Input: x1 7 x = ?, y = ?, return value = 0 x, y both unchanged Can assign return value to variable Example: int numRead; // # input values read numRead = scanf("%d%d", &x, &y); 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 Example Variables: int i; double d; char c; What values are read for each of the following inputs and scanf() calls? Assume the input is as follows: 34 5.7 scanf("%d%lf", &i, &d) scanf("%d %lf", &i, &d) scanf("%lf%d", &d, &i) scanf("%d%c", &i, &c) scanf("%d %c", &i, &c) 11/8/2019 ECE Application Programming: Lecture 2
ECE Application Programming: Lecture 2 Example solution What values are read for each of the following inputs and scanf() calls? scanf("%d%lf", &i, &d) i = 34, d = 5.7 scanf("%d %lf", &i, &d) scanf("%lf%d", &d, &i) d = 34, i = 5 scanf("%d%c", &i, &c) i = 34, c = ' ' (space) scanf("%d %c", &i, &c) i = 34, c = '5' 11/8/2019 ECE Application Programming: Lecture 2
Using scanf() and printf() together ECE 160 - Introduction to Computer Engineering I 02/09/2005 Using scanf() and printf() together #include <stdio.h> int main() { int hours; float rate; float grosspay; printf("Enter hours: "); scanf("%d",&hours); printf("Enter pay rate: "); scanf("%f",&rate); grosspay = hours * rate; printf("You earned $%f\n",grosspay); } 11/8/2019 ECE Application Programming: Lecture 2 (c) 2005, P. H. Viall
scanf() function - Payroll Ver 2 ECE 160 - Introduction to Computer Engineering I 02/09/2005 scanf() function - Payroll Ver 2 #include <stdio.h> int main() { double hours; double rate; double grosspay; printf("Enter hours: "); scanf("%lf",&hours); printf("Enter pay rate: "); scanf("%lf",&rate); grosspay = hours * rate; printf("You earned $%lf\n",grosspay); } 11/8/2019 ECE Application Programming: Lecture 2 (c) 2005, P. H. Viall
ECE Application Programming: Lecture 2 Final notes Next time PE1 (Flowcharts) Conditional statements Reminders: Program 1 due Friday, 5/25 10 points: register for access to the course textbook 10 points: introduce yourself to your instructor 30 points: complete simple C program Sign up for the course discussion group! 11/8/2019 ECE Application Programming: Lecture 2