EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 2 Data Types, Declarations, and Displays
String Escape Sequences
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
C Programming Lecture 4 : Variables , Data Types
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 2: Basic C program structure Data in C: Data types,
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Chapter 2 Variables.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Numbers in ‘C’ Two general categories: Integers Floats
CSCE 206 Structured Programming in C
Chapter 2 Variables.
ECE Application Programming
Chapter 2: Introduction to C++
EECE.2160 ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
Microprocessor Systems Design I
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Microprocessor Systems Design I
EECE.2160 ECE Application Programming
Variables have a type have an address (in memory) have a value
ECE Application Programming
By: Syed Shahrukh Haider
Chapter 2: Introduction to C++
IDENTIFIERS CSC 111.
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 CS Your First C Programs
Chapter 2 Variables.
UMBC CMSC 104 – Section 01, Fall 2016
EECE.2160 ECE Application Programming
WEEK-2.
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
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
Unit 3: Variables in Java
Chapter 2 Variables.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Introduction to C Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Variables in C Topics Naming Variables Declaring Variables
EECE.2160 ECE Application Programming
The Fundamentals of C++
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

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

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

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

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

ECE Application Programming: Lecture 2 Comment example /* 16.216 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

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

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

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

Range of Integers (Machine Dependent) unsigned signed char 0  255 -128  +127 (8 bits) short int 0  65535 -32768  + 32767 short (16 bits) int 0 to 4294967295 -2147483648  2147483647 long long int (32 bits) 6/2/2019 ECE Application Programming: Lecture 2

float/double Constants Any signed or unsigned number with a decimal point Legal values: 5. .6 +2.7 0.0 -6.5 +8. 43.4 Legal (exponential notation): 1.624e3 7.32e-2 6.02e23 1.0e2 -4.23e2 +4.0e2 1.23e-4 +11.2e+7 Illegal: $54.23 6,349.70 1.0E5 6/2/2019 ECE Application Programming: Lecture 2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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