ECE Application Programming

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
String Escape Sequences
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 CSC103: Introduction to Computer and Programming Lecture No 6.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 2: Basic C program structure Data in C: Data types,
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Primitive Types Four integer types: Two floating-point types:
Bill Tucker Austin Community College COSC 1315
Chapter # 2 Part 2 Programs And data
ECE Application Programming
Variables Mr. Crone.
EECE.2160 ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
Microprocessor Systems Design I
Microprocessor Systems Design I
EECE.2160 ECE Application Programming
Revision Lecture
Multiple variables can be created in one declaration
Variables have a type have an address (in memory) have a value
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to C++ Programming
Variables in C Topics Naming Variables Declaring Variables
Chapter 2: Java Fundamentals
Variables in C Topics Naming Variables Declaring Variables
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 2: Java Fundamentals
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Variables in C Topics Naming Variables Declaring Variables
EECE.2160 ECE Application Programming
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
EECE.2160 ECE Application Programming
Variables in C Topics Naming Variables Declaring Variables
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Variables in C Topics Naming Variables Declaring Variables
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

16.216 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2014 Lecture 3: Variables Operators Basic variable output with printf()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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