Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2018 Lecture 3: Variables

2 ECE Application Programming: Lecture 3
Lecture outline Announcements/reminders Chapter 1 exercises due Monday, 9/10 Chapter 2 exercises due 9/13 & 9/15 Textbook exercises always due 3 days after related lecture—check ”Assignments” tab regularly!!! Program 1 due Wednesday, 9/12 10 points: register for access to the course textbook 10 points: introduce yourself to your instructor 30 points: complete simple C program Programs will also require “submission” on Blackboard To be added by end of today Review Basic C program structure Comments Today’s lecture Data types Variables 6/2/2019 ECE Application Programming: Lecture 3

3 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 Not used if main() is void Basic output Call printf(<string>); <string> can be replaced by characters enclosed in double quotes May include escape sequence, e.g. \n (new line) 6/2/2019 ECE Application Programming: Lecture 3

4 ECE Application Programming: Lecture 3
Review (cont.) Comments Single-line: // This is a comment Multi-line: /* This is also a comment */ Typical uses Multi-line comment at start of program with Author’s name (& other info if appropriate) Date started/modified File name Description of overall file functionality For individual code sections Comment for major section of code performing single function Comment for single line of code if that line alone is important 6/2/2019 ECE Application Programming: Lecture 3

5 ECE Application Programming: Lecture 2
Comment example /* ECE Application Programming Instructor: M. Geiger 6/2/2019 hello.c: Intro program to demonstrate basic C program structure and output */ #include <stdio.h> // Main program: prints basic string and exits int main() { printf("Hello World!\n"); // Comment return 0; } 6/2/2019 ECE Application Programming: Lecture 2

6 ECE Application Programming: Lecture 2
Representing data in C Two major questions (for now) What kind of data are we trying to represent? Data types Can the program change the data? Constants vs. variables 6/2/2019 ECE Application Programming: Lecture 2

7 Four Types of Basic Data
Integer int Floating point (single precision) float Double Precision double Character char 6/2/2019 ECE Application Programming: Lecture 2

8 ECE Application Programming: Lecture 2
Integer Constants Any positive or negative number without a decimal point (or other illegal symbol). Legal values: Illegal values: 2,523 (comma) 6.5 (decimal point) $59 (dollar sign) 5. (decimal point) 6/2/2019 ECE Application Programming: Lecture 2

9 Range of Integers (Machine Dependent)
unsigned signed char 0   +127 (8 bits) short int 0   short (16 bits) int 0 to  long long int (32 bits) 6/2/2019 ECE Application Programming: Lecture 2

10 float/double Constants
Any signed or unsigned number with a decimal point Legal values: Legal (exponential notation): 1.624e e e23 1.0e e e e e+7 Illegal: $ , E5 6/2/2019 ECE Application Programming: Lecture 2

11 float/double Constants
Range of float (32 bits) ± E – 38 ± E + 38 Range of double (64 bits) ± E – 308 ± E + 308 6/2/2019 ECE Application Programming: Lecture 2

12 ECE Application Programming: Lecture 3
Character Constants Stored in ASCII or UNICODE Signified by single quotes (’ ’) Valid character constants ’A’ ’B’ ’d’ ’z’ ’1’ ’2’ ’!’ ’+’ ’>’ ’?’ ’ ’ ’#’ Invalid character constants ’GEIGER’ ’\’ ’CR’ ’LF’ ’’’ ’’’’ ’”’ ”Q” 6/2/2019 ECE Application Programming: Lecture 3

13 Character Escape Sequences
Meaning ’\b’ Backspace ’\’’ Single quote ’\n’ Newline ’\”’ Double quote ’\t’ Tab ’\nnn’ Char with octal value nnn ’\\’ Backslash ’\xnn’ Char with hex value nn 6/2/2019 ECE Application Programming: Lecture 3

14 ECE Application Programming: Lecture 3
Variables All variables have four characteristics: A type An address (in memory) A value A name 6/2/2019 ECE Application Programming: Lecture 3

15 ECE Application Programming: Lecture 3
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 6/2/2019 ECE Application Programming: Lecture 3

16 Variables - legal names
grossPay carpet_Price cArPeT_price a_very_long_variable_name i ______strange___one_____ _ (not recommended) 6/2/2019 ECE Application Programming: Lecture 3

17 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/ _ ) 6/2/2019 ECE Application Programming: Lecture 3

18 ECE Application Programming: Lecture 3
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 6/2/2019 ECE Application Programming: Lecture 3

19 ECE Application Programming: Lecture 3
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 ... 6/2/2019 ECE Application Programming: Lecture 3

20 ECE Application Programming: Lecture 3
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 6/2/2019 ECE Application Programming: Lecture 3

21 ECE Application Programming: Lecture 3
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 6/2/2019 ECE Application Programming: Lecture 3

22 ECE Application Programming: Lecture 3
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). 6/2/2019 ECE Application Programming: Lecture 3

23 ECE Application Programming: Lecture 3
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). 6/2/2019 ECE Application Programming: Lecture 3

24 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). 6/2/2019 ECE Application Programming: Lecture 3 (c) 2005, P. H. Viall

25 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; } 6/2/2019 ECE Application Programming: Lecture 3

26 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 (ASCII value 3 = "end of text" character) 6/2/2019 ECE Application Programming: Lecture 3

27 ECE Application Programming: Lecture 3
Final notes Next time Operators Output using printf() Reminders: Chapter 1 exercises due Monday, 9/10 Chapter 2 exercises due 9/13 & 9/15 Textbook exercises always due 3 days after related lecture—check ”Assignments” tab regularly!!! Program 1 due Wednesday, 9/12 10 points: register for access to the course textbook 10 points: introduce yourself to your instructor 30 points: complete simple C program Programs will also require “submission” on Blackboard To be added by end of today 6/2/2019 ECE Application Programming: Lecture 3


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google