CPP-Day 2
CPP-Day 2
C++ Literals
Literals are data used for representing fixed values
Literals can be used directly in code e.g. 1 , 3.1 , ‘m’ etc.
Here 1, 3.1 and ‘m’ are literals because you cannot assign
different values to these
C++ has different literals which are explained in next few
slides
C++ Introduction – Day 2
Integers
An Integer is numeric literal (related to numbers) without
fractional or exponential part
There are three types of integer literals in C++
Programming
Decimal (Base 10) (This is the number system we all use in
day to day life)
Octal (Base 8)
Hexadecimal (Base 16)
C++ Introduction – Day 2
Examples of C++ Integer Literals
Decimal :
0 , 1, 11, -22 , 75, 100001 etc.
Octal :
011 , 033, 088 etc.
Octal Literals in C++ start with 0
Hexadecimal :
0x7f , 0x3b, 0x521 etc
Hexadecimal Literals in C++ start with 0x
For more info on Number Systems please read this article
C++ Introduction – Day 2
Floating-point Literals
Floating- point literals are numeric literals which have
wither fractional form or exponent form
E.g.
1.5 , 3.1 , 10.1 , -4.5, 0.00001, -0.00278 etc. are
fractional literals
3.8E5, 9.1E2 etc. are exponent literals where
E5 = 105
So 3.8E5 is equivalent to 3.8 X 105
C++ Introduction – Day 2
Character Literals
A character literal is created by enclosing a single
character inside single quotation mark.
For example
‘m’ , ‘e’, ‘E’ , ‘T’ , ’2’ , ‘1’, ‘(‘, ‘{‘ etc.
C++ Introduction – Day 2
Escape Sequences
Sometimes it is necessary to use characters that cannot
be types or has special meaning in C++ program.
For example
new line , backspace , tab , quotation mark etc.
In order to use these characters in C++ program, escape
sequences are used
C++ Introduction – Day 2
Escape Sequences
Characters Escape Sequences
Backspace \b
Form feed \f
Newline \n
Return \r
Horizontal tab \t
Vertical tab \v
C++ Introduction – Day 2
Escape Sequences
Characters Escape
Sequences
Backslash \\
Single quotation mark \'
Double quotation mark \"
Question mark \?
Null Character \0
C++ Introduction – Day 2
String Literals
A string literal is sequence of characters enclosed in double-
quotation marks
For example
“Hello!!” - string constant
“” - null(without any value) string constant
“ “ - string constant with two white spaces
“m” -string constant with single character
“Hello!! Meet \n” -string constant with new line character (Refer previous
slide of
escape sequence)
C++ Introduction – Day 2
C++ Constant
Constant is a variable whose value cannot be changed
once program is compiled.
To define constant we use “const” keyword
For example
const PI = 3.14;
If you try to write PI = 22/7 this will give error
C++ Introduction – Day 2
Exercise Two
Write a program using online IDE. Define variables for following
Variable to store First Name of person
Variable to store Last Name of person
Variable to store Age of person
Variable to store Address of person
Variable to store Pin Code
Program should print First Name and Last Name on one line
Age on another line
Address and Pin Code on next line
Refer Example program and make use of cout and cin functions