Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructors: Dr. Michael Geiger Fall 2018 Lecture 4: Operators; printf() basics
2
Announcements/reminders
Program 1 due today 10 points: register for access to the course textbook 10 points: introduce yourself to your instructor 30 points: complete simple C program DON’T FORGET BLACKBOARD “ASSIGNMENT” Program 2 to be posted; due Friday, 9/21 Won’t cover scanf() until Friday, 9/14 Suggestions Start working on program design now Test equations/output by assigning values to variables Once you understand scanf(), replace assignments with scanf() calls 5/26/2019 ECE Application Programming: Lecture 4
3
ECE Application Programming: Lecture 4
Lecture outline Review Variables Today’s lecture Operators Basic variable output with printf() 5/26/2019 ECE Application Programming: Lecture 4
4
ECE Application Programming: Lecture 4
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 5/26/2019 ECE Application Programming: Lecture 4
5
ECE Application Programming: Lecture 4
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; } 5/26/2019 ECE Application Programming: Lecture 4
6
ECE Application Programming: Lecture 4
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 (ASCII value 3 = "end of text" character) 5/26/2019 ECE Application Programming: Lecture 4
7
Arithmetic Operations
Operator Addition + Subtraction - Multiplication * Division / Modulus Division (Remainder) % 5/26/2019 ECE Application Programming: Lecture 4
8
Results of arithmetic operations
(using non-integer makes result double precision) .08* 12.0/ 10/5 2 10/3 3 (not 3.333…) 10 % 3 1 12 % 5 2 5/26/2019 ECE Application Programming: Lecture 4
9
ECE Application Programming: Lecture 4
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 5/26/2019 ECE Application Programming: Lecture 4
10
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; } 5/26/2019 ECE Application Programming: Lecture 4
11
ECE Application Programming: Lecture 4
Operators (cont.) More complex statements are allowed e.g. x = ; Parentheses help you prioritize parts of statement Makes difference with order of operations x = * 3; is different than x = (1 + 2) * 3; 5/26/2019 ECE Application Programming: Lecture 4
12
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 /2 /2 5 * 3 % 3 / / 2 5 * (3 % 3) / /3 5/26/2019 ECE Application Programming: Lecture 4
13
ECE Application Programming: Lecture 4
Example solution 19/3 = 6 (integer division) 3/19 = 0 (integer division) 19%3 = 1 3%19 = 3 5 + 7/2 = = 8 /2 = = 8.0 /2 = = 8.5 5/26/2019 ECE Application Programming: Lecture 4
14
Example solution (cont.)
For each of the following, underlined part(s) evaluated first at each step 5 * 3 % 3 / / 2 = 15 % 3 / = 0 / = = 19 5 * (3 % 3) / /3 = 5 * 0 / /3 = 0 / = = 17.0 5/26/2019 ECE Application Programming: Lecture 4
15
ECE Application Programming: Lecture 4
I/O basics Need ability to Print variables (or results calculated using them) Read values from input Output: printf() Already seen basics Input: scanf() 5/26/2019 ECE Application Programming: Lecture 4
16
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 5/26/2019 ECE Application Programming: Lecture 4
17
ECE Application Programming: Lecture 4
printf() example float a=67.49,b= ; 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 there a=67.49, b=10.0Cool huh? 5/26/2019 ECE Application Programming: Lecture 4
18
ECE Application Programming: Lecture 4
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 5/26/2019 ECE Application Programming: Lecture 4
19
ECE Application Programming: Lecture 4
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 5/26/2019 ECE Application Programming: Lecture 4 (c) 2005, P. H. Viall
20
ECE Application Programming: Lecture 4
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. 5/26/2019 ECE Application Programming: Lecture 4
21
ECE Application Programming: Lecture 4
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 5/26/2019 ECE Application Programming: Lecture 4
22
ECE Application Programming: Lecture 4
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. 5/26/2019 ECE Application Programming: Lecture 4
23
ECE Application Programming: Lecture 4
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 [ ] 5/26/2019 ECE Application Programming: Lecture 4
24
ECE Application Programming: Lecture 4
Final notes Next time scanf() Reminders: Program 1 due today 10 points: register for access to the course textbook 10 points: introduce yourself to your instructor 30 points: complete simple C program DON’T FORGET BLACKBOARD “ASSIGNMENT” Program 2 to be posted; due Friday, 9/21 Won’t cover scanf() until Friday, 9/14 Suggestions Start working on program design now Test equations/output by assigning values to variables Once you understand scanf(), replace assignments with scanf() calls 5/26/2019 ECE Application Programming: Lecture 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.