ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Names and Bindings.
Chapter 7: User-Defined Functions II
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Structure of a C program
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
JavaScript, Third Edition
Basic Elements of C++ Chapter 2.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CPS120: Introduction to Computer Science Variables and Constants.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
A variable is a name for a value stored in memory.
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
CMSC 104, Section 4 Richard Chang
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
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
Variables in C Declaring , Naming, and Using Variables.
Expressions and Assignment
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Chap 2. Identifiers, Keywords, and Types
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
Variables in C Topics Naming Variables Declaring Variables
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

Syllabus What’s This Blue Code? Naming Variables Declaring Variables Assigning Values to Variables Read-only Variables

2 What’s This Blue Code? void main() { // main #ifdef Apollo printf( " C program on Apollo.\n" ); #else # undef Apollo // o.k. to undef what isn't defined # ifdef IBMPC printf( " C program on IBMPC.\n" ); # else # ifdef Sun printf( " C program on Sun.\n" ); # else printf( " Don't know what this runs on.\n" ); # endif } //end main

3 Naming Variables Variables store data for use by a program Fashionable to call “variables” instead: “objects” Any C program variable has a name, type, scope – and other attributes, e.g. const-- that uniquely identifies it The datum stored in a variable is called its value A variable’s value may be modified during execution Some variables persist during the whole program life; others come and go automatically as functions come to life; yet others appear dynamically –on heap

4 Identifiers –e.g. names of variables-- can contain:  Letters (uppercase A-Z and lowercase a-z)  Digits (0-9)  Underscore _ Must start with letter; underscore counts as letter Note:A leading underscore is typically reserved for system variables. Use a letter instead At least 31 characters are significant C variable names are case-sensitive

5 C keywords cannot be used as variable names Reserved keywords: † Is only reserved in C99 autoenumrestrict † unsigned breakexternreturnvoid casefloatshortvolatile charforsignedwhile constgotosizeof_Bool † continueifstatic_Complex † defaultinline † struct_Imaginary † dointswitch doublelongtypedef elseregisterunion C99

6 Hints for naming variables:  Accurately describe what the variable represents  Choose a name just long enough to be clear  Avoid names that are too short unless necessary  Be consistent in your naming convention Common styles for multi-word identifiers: federal_tax_rate ← underscores Federal_Tax_Rate federalTaxRate ← camel case FederalTaxRate

7 Example: meanvalue2, mEaNvAlUe2, and MeanValue2 are all different names. Sample Variable NameValidComment x Yes Generally not best naming convention meanvalue2 Yes mEaNvAlUe2 MeanValue2 Mean_Value_2 8ball Wait4It$ double ABCDEFGHIJKLMNOPQRSTUVWXYZ12345abc ABCDEFGHIJKLMNOPQRSTUVWXYZ12345def Yes No Spaces are not allowed Underscores are fineYes NoFirst character must be a letter No $ is illegal character No Cannot use C keyword for variable name Yes At least 31 characters significant Maybe Depends on number of significant chars

8 Declaring Variables Variables must be declared in C, else cannot be used A declaration specifies the name, type, scope, and storage attribute of variable  Is the selected type appropriate for representing the data?  Do you need to consider the possibility of overflow?  For floating point, does the type have enough precision/range?  Is memory available? Once declared, a non-static variable has an unknown value until explicitly initialized Static objects are initialized to 0 by default If a variable must have a particular starting value, then initialize it explicitly before using it!

9 Syntax: type varname1, varname2, …; Example: int lower, upper, step; float x; double y, z[500]; Variables of the same type can also be declared on separate lines. Example: int lower;/* Lower bound of range */ int upper;/* Upper bound of range */ int step;/* Step size */

10 Assigning Values to Variables Assignment operator is a single equal sign = varname = expression; Example: speed_of_light = e8; Do not confuse = (assignment) with == (comparison). Example: x = 5; x == 5; Stores the number 5 in variable x.Compares value of variable x to the number 5 for equality.

11 Variables can be initialized in or after declaration Example: float delta; /* unknown initial value */ delta = 1.5e-6f; /* now equals 1.5e-6 */ Example: int count;/* All are uninitialized */ int w; float factor; count = 5;/* Now initialized */ w = 3; factor = 2.0f * (count + w);

12 Variables can be initialized during declaration: type varname = initial_expression; Example: char InChar = 'A'; float delta = 1.5e-6f;

13 In C90, variable declarations must appear at the start of a block and before other code statements Cannot arbitrarily mix variable declarations and code Example: double pi = ; double w, f = 100; w = 2*pi*f; double x; x = w + pi / 6.0; This declaration will cause a compiler error. In C90, double x; must go before w = 2*pi*f;

14 Order of Variable Declarations and Code C99 allows variable declarations to be mixed with code. This allows a variable to be declared closer to where it is first used. /* C90 version */ #include int main (void) { int x = 1, y = 3, z, A[2]; z = x + 7; A[0] = 36; A[1] = z; printf("%d\n", y); return 0; } // C99 version #include int main (void) { int x = 1, z; z = x + 7; int A[2]; A[0] = 36; A[1] = z; int y = 3; printf("%d\n", y); return 0; } C99

15 Read-Only Variables With a standard declaration, the value stored in a variable can be changed at will To declare a variable whose contents can be read but not changed, use the const modifier: const type varname = value; A variable that is const must be assigned its value when it is declared

16 Example: const int answer_to_the_ultimate_question = 42; Example: double g = 9.81; /* standard declaration */ g = 1.6; /* Can change it */ Example: const double g = 9.81; /* Make it read-only */ g = 1.6; /* Causes compiler error */ Example: /* This is a constant string */ const char * msg = "PSU"; printf("Message: %s\n", msg);

17 Arrays, Initial Coverage An array in C is a collection of 1 or more elements of all the same type Known by one name, the array name Usable like any scalar variable But selected by an operation called “indexing” With the low index, AKA the name of the first element, being an integer value of 0 And the last element being of index “number_of_elements – 1”

18 Arrays, Initial Coverage Sample #define MAX 4 int count1;// declaration w/o initialization int count2 = 9;// scalar object with initial value 9 int mat1[ 4 ];// 4 elements hard coded bounds 0..3 int mat2[ MAX ];// 4 elements properly expressed int mat3[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23 }; int mat4[ 9 ] = { 1, 1, 1, 2, 2, 2, 3, 3, 3 };... mat3[ 2 ] = mat4[ I ] + mat3[ 0 ];