C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.

Slides:



Advertisements
Similar presentations
Types and Variables. Computer Programming 2 C++ in one page!
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
String Escape Sequences
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Variable & Constants. A variable is a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Input & Output: Console
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
C Programming Lecture 4 : Variables , Data Types
C-Language Keywords(C99)
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 2 Variables.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
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.
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.
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.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Spring 2012.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Electrical Engineering Department 1st Year Programming Languages
Chapter 1.2 Introduction to C++ Programming
Wel come.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
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.
Basic Elements of C++.
Multiple variables can be created in one declaration
Basic Elements of C++ Chapter 2.
Chapter 2.
Escape Sequences What if we wanted to print the quote character?
C++ Basics.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Basics of ‘C’.
An overview of Java, Data types and variables
Chapter 2 Variables.
Introduction to Primitive Data types
Introduction to Programming - 3
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Unit 3: Variables in Java
Chapter 2 Variables.
Variables and Constants
Introduction to Primitive Data types
Presentation transcript:

C++ Basics Tutorial 5 Constants

Topics Covered Literal Constants Defined Constants Declared Constants

Literal constants Most easy to see and most obvious constants

Types of literal constants Integer Numerals Floating-Point Numerals Boolean literals Character literals String literals

Integer Numerals A number without decimal points (duh!) Examples: 23, 56, 8… In C++, expressing numerical constants does not require any special character like “”. Integer numerals can be defined in other numeral forms like octal or hexadecimal. 255 Decimal Value 0377 Octal value 0xff Hexadecimal Value All represent same number

Decimal, Octal, hexadecimal, signed and unsigned representation To denote an octal number(Base 8 number) start the number with 0 (zero) To denote a hexadecimal value(base 16 number) start with 0 x (zero “x”) Write normally for decimal numerical(base 10) system value. Force int to be unsigned by adding u(or U) at the end of number. (Ex: 19U) or l(or L) to make it long( 19L or 19UL)

Floating Point literals To represent number with decimal or exponents A decimal point “.” or a “e” can be added to represent the number, where e means to the “power by 10” to number after e. Also can have both “e” and “.” Example: 3.14 Value of Pi 5.97e24(Mass of earth) = 5.97 x 10 ^ 24 or 1.67e-27(Mass of proton) = 1.67 x 10 ^ -27 Force number to be long double add L or l, to force number to be float add f or F in the end of the number E or e both are same. C++ is not case sensitive in this case.

Boolean literals Only two boolean literals in C++: true or false. Can be represented by bool data type.

Character literals One character. Example: ‘a’, ‘b’, ‘A’ etc… To represent character literals we put them inside single quotes. This is done to differentiate them from possible variable identifiers that we might define in the program. If you just write 1 it is numerical literal. But ‘1’ makes it character literal. ‘a’ is a character literal where as just a is a variable identifier named a.

String literals Combination of characters. Inside double quotes “”. Example “Dean”

Escape Characters Characters and string both can have a special character called escape character. Impossible or at least difficult to express otherwise Precede by a backslash(\) and a character. Example: \n  New Line, \t  Tab ‘\n’ or ‘\t’ or “LineOne\nLineTwo\nLineThree” “\”DoubleQuote\”” = “DoubleQuote” when you print.

Few list of escape characters: \n  Newline \t  tab \r  carriage return \v  vertical tab \b  backspace \f  form feed \a  alert beep \’  single quote(‘) \”  Double quote(“) \?  question mark(?) \\  backslash(\)

Defined constants Using something called preprocessor directive Use #define preprocessor directive Define constant that will be used frequently. Example: #define PI #define NEWLINE ‘\n’ This now have defined two constants called PI and NEWLINE. Now you can use PI and NEWLINE as other constants we learned earlier. Due to #define the C++ compiler literally replaces all the occurrences of PI and NEWLINE with assigned value( or ‘\n’)

Directive A directive is not a C++ statement. It is interpreted by the preprocess that happens before even looking at the code of the program. So these directive does not need to have a semicolon in the end. Directives start with #. Applies to #include as well.

Declared Constants (const) Using const prefix with specific data type you make a variable unchangeable throughout the program. Example: const int a = 23; const char newline = ‘\n’ The are completely regular variables except that you cannot modify these after you initialize.

Please Rate Comment and subscribe Next Chapter: We will talk about operators Then: Basic input/ output. End of Basics. Then to Control structures / Functions