Copyright © 2005 Curt Hill Constants in C++ Why and How.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Chapter 11 Introduction to Programming in C
Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Structure of a C program
Programming with Collections Collections in Java Using Arrays Week 9.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
C Programming Constants and Control Structures. Constants Can be defined in two ways. –Traditional Method –#define PI 3.14 #define is a preprocessor command.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Input & Output: Console
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
Numeric Types, Expressions, and Output ROBERT REAVES.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Java Data Types Data types, variable declaration, and initialization.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
CPS120: Introduction to Computer Science
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes –
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Data Types Declarations Expressions Data storage C++ Basics.
Copyright © – Curt Hill Types What they do.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Copyright © 2015 Curt Hill Make An Indispensable Developer’s Tool.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Copyright Curt Hill Arrays in C/C++ What? Why? How?
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Copyright © Curt Hill Formatting Reals Outputs other than normal.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Recap……Last Time [Variables, Data Types and Constants]
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
1.2 Primitive Data Types and Variables
CPS120: Introduction to Computer Science Variables and Constants.
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
The Second C++ Program Variables, Types, I/O Animation!
Chapter 3 Assignment and Interactive Input.
Programming Fundamentals
More important details More fun Part 3
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.
Variables and Primative Types
C Basics.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Constants in Java Why and How Copyright © Curt Hill.
C Preprocessor(CPP).
C++ Data Types Data Type
EECE.2160 ECE Application Programming
Module 2 Variables, Data Types and Arithmetic
EECE.2160 ECE Application Programming
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Presentation transcript:

Copyright © 2005 Curt Hill Constants in C++ Why and How

Copyright © 2005 Curt Hill Introduction There are three kinds of constants in C++ –Literals –Define named constants –Variables with const prefix We will see the whys and wherefores now

Copyright © 2005 Curt Hill Literal constants We have seen the following fours kinds of constants: –-45 (int) –2.3 (float) –1E4 (float) –‘A’ (char) –“Hi” (string) Normally, merely inspecting the constant will tell you its type, as well as its value

Copyright © 2005 Curt Hill Literals Continued What do we do to set the constant to something other than a standard int or float? We can use a suffix to tell it the type The L or l indicates that it is long The U or u indicates unsigned Floating point constants are by default double Suffix of F or f makes it a float, L a long double You may also use binary, octal or hexadecimal constants but we do not need to do that here

Copyright © 2005 Curt Hill Literal Problems However, literal constants have the following problems that sometimes need to be dealt with We have to retype them each time –This is a problem with high precision constants like PI –Opportunities for typing errors –Just remembering what the 14 digit value is We have to remember what they represent –Small integer constants tend to get confused with one another

Copyright © 2005 Curt Hill Example: Suppose that I am a manager and I have 5 people who work 5 days a week and 8 hours a day Thus the number of manhours is: workhours = 5 * 5 * 8; Now we will try something new, four ten hour days per week I use my text editor and change all 5s to 4s and all 8s to 10s We now get workhours = 4 * 4 * 10; The problem with the above expression is that the 5 and 8 have no inherent meaning

Copyright © 2005 Curt Hill Alternative It would be better to say: int people = 5, dayperweek = 5, hoursperday = 8;... workhours = people * dayperweek * hoursperday Unfortunately that leads to a problem: –An inadvertent assignment to one of the variables The solution is that we would like to be able to name constants, just like we name variables but without possibility of assignment

Copyright © 2005 Curt Hill The define preprocessor directive This is the only C way had to do this Constants are set up using the define directive Format: #define name value Blanks separate the items The value is usually a constant of some sort The name has the format of a C variable

Copyright © 2005 Curt Hill Example Some constants #define DAYSPERWEEK 5 #define HOURSPERDAY 8 #define EMPLOYEES 5 hours = DAYSPERWEEK * HOURSPERDAY * EMPLOYEES;

Copyright © 2005 Curt Hill Discussion The preprocessor searches the rest of the text and replaces every occurrence of NAME with VALUE The name is not a variable, though it may look like one Examples: #define PI x = PI * r * r; Simply does macro text substitution so that statement becomes: x = * r * r;

Copyright © 2005 Curt Hill More Discussion Preprocessor does not know C/C++ It has its own syntax No = or ; is needed So #define PI = 19/6 does not work, it becomes x = = 19/6 * r * r; Even without = the 19/6 would be evaluated as an integer

Copyright © 2005 Curt Hill More Discussion String constants are not changed such as: “ The value of PI is ” –This would remain the same #define must occur before use, but customarily comes at front of program Used to parameterize a program There is a convention that constants should be in all caps so a constant is obvious in the text

Copyright © 2005 Curt Hill #define problems A problem with the #define statement is that no type is associated with the value All that occurs is macro processing, where one string is found and replaced with another C++ accepts #defined constants but adds a new way to define a constant

Copyright © 2005 Curt Hill Const Keyword A feature that is new to C++ is the const qualifier: –const float pi = ; Any attempt to assign to this will result in an error The float type determines the precision used

Copyright © 2005 Curt Hill Discussion The const is a keyword that prefixes the declaration This may be used anywhere a variable may be declared The declaration must have an initialization