Download presentation
Presentation is loading. Please wait.
1
16.216 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2014 Lecture 3: Variables Operators Basic variable output with printf()
2
ECE Application Programming: Lecture 3
Lecture outline Announcements/reminders Program 1 due today Program 2 posted; due 9/17 Sign up for the course discussion group Review Comments Data types Today’s lecture Constants Variables Operators Basic variable output with printf() 8/8/2018 ECE Application Programming: Lecture 3
3
ECE Application Programming: Lecture 3
Review Comments Single-line: // This is a comment Multi-line: /* This is also a comment */ Data types Define size & format of data Four basic types: int, float, double, char 8/8/2018 ECE Application Programming: Lecture 3
4
Using #define with constants
Often makes sense to give constant value a symbolic name for readability Don’t want constant wasting memory space Use #define to define a macro Macro: named code fragment; when name is used, the preprocessor replaces name with code Syntax: #define <name> <code> Common use: defining constants By convention, start constant values with capital letters e.g. #define NumberOne 1 At compile time, all uses of “NumberOne” in your program are replaced with “1” and then compiled 8/8/2018 ECE Application Programming: Lecture 2
5
ECE Application Programming: Lecture 2
Variables All variables have four characteristics: A type An address (in memory) A value A name 8/8/2018 ECE Application Programming: Lecture 2
6
ECE Application Programming: Lecture 2
Variables - name must start with a-z, A-Z ( _ allowed, but not recommended) other characters may be a-z, A-Z, 0-9, _ upper case/lower case are not equal (i.e. ECE, ece, Ece, EcE, eCe would be five different variables) max length system dependent (usually at least 32) By convention Start with lowercase letter Descriptive names improve code readability 8/8/2018 ECE Application Programming: Lecture 2
7
Variables - legal names
grossPay carpet_Price cArPeT_price a_very_long_variable_name i ______strange___one_____ _ (not recommended) 8/8/2018 ECE Application Programming: Lecture 2
8
Variables - legal names (but not recommended)
l (that's lower case L) O (that's capital O) l1 (that's lower case L, and digit one) O0Oll11 (oh,zero,oh,el,el,one,one) _var (many system variables begin w/ _ ) 8/8/2018 ECE Application Programming: Lecture 2
9
ECE Application Programming: Lecture 2
Variables - declaring var name memory loc main() { float hours, payrate; float grosspay; int j; hours ? 4278 payrate ? 427C 4280 grosspay ? j ? 4284 All variable declarations should be grouped together at the start of the function 8/8/2018 ECE Application Programming: Lecture 2
10
ECE Application Programming: Lecture 2
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 ... 8/8/2018 ECE Application Programming: Lecture 2
11
ECE Application Programming: Lecture 2
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 8/8/2018 ECE Application Programming: Lecture 2
12
ECE Application Programming: Lecture 2
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 8/8/2018 ECE Application Programming: Lecture 2
13
ECE Application Programming: Lecture 2
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). 8/8/2018 ECE Application Programming: Lecture 2
14
ECE Application Programming: Lecture 2
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). 8/8/2018 ECE Application Programming: Lecture 2
15
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). 8/8/2018 ECE Application Programming: Lecture 2 (c) 2005, P. H. Viall
16
ECE Application Programming: Lecture 3
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; } 8/8/2018 ECE Application Programming: Lecture 3
17
ECE Application Programming: Lecture 3
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 8/8/2018 ECE Application Programming: Lecture 3
18
ECE Application Programming: Lecture 3
Final notes Next time Operators Output using printf() Reminders: Sign up for the course discussion group on Piazza! Program 1 due today Program 2 posted; due 9/17 8/8/2018 ECE Application Programming: Lecture 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.