Constants Literals
Constants Literals
PRG1002 - Programming I
Constants/Literals
in C++
● What is a Literal?
● Literal is a fixed value that is written directly into the source code. Literals are compiled into
constants with the values in the source code.
int x = 5;
cout << "Hello world!" << endl;
● Both literals and variables have a value, and they both have a type.
○ The type of a variable is given in its declaration.
○ The type of a literal is assumed from the value and format of the literal itself.
Types of Literals
● We can also specify an integer literal type long and unsigned. We can change the type of an integer
literal by adding a suffix as shown below:
● There are some special characters that are used for different purpose in character literals.
● We can use the same special characters in string literals as we can in character literals.
● So far we’ve only used these to print output to the console, but we will learn more about strings in
another lesson.
Boolean Literal
● Boolean literals are used with the bool data type. For a boolean literal there are two possible values:
true and false.
cout << "True will be displayed as " << isTrue << endl;
cout << "True will be displayed as " << isFalse << endl;
● Use the boolalpha output modifier to print ‘true’ and ‘false’ instead
bool isTrue = true;
bool isFalse = false;
cout << boolalpha;
cout << "True will be displayed as " << isTrue << endl;
cout << "True will be displayed as " << isFalse << endl;
Constants
Defining constants
● Constants are treated just like regular variables except that their values cannot be modified after their
definition.
● Use a constant variable to avoid accidentally changing a variable that should not change.
● We can use const prefix to declare constants with a specific data type.
● The constant variables should be initialized with value when the variable is created.
● If we try to change the value of the constant variable after it’s initialization, we will get an error
// can't do this
PI = 231.424; // this line will result in compile error
Using #define preprocessor
● Another mechanism to name constant values is to use the #define preprocessor directive.
● This is a blind replacement as it is performed by the preprocessor, and happens before the program is
compiled. The validity of the types or syntax involved will not be checked in any way.
Using #define preprocessor
#include <iostream>
using namespace std;
● Note that #define lines are preprocessor
#define PI 3.141592653589793238
directives, and they do not require semicolons
at the end (;) int main()
{
double r = 5.0;
● If a semicolon is included in the line, it is part double areaOfCircle;
of replacement sequence and is also included // Area of Circle: PI
areaOfCircle = r * r * PI;
in all replaced occurrences.
cout << areaOfCircle << endl;
return 0;
}
Summary
● An integer literal can be decimal, octal, binary, or hexadecimal constant. It can also have a suffix that is
a combination of U and L, for unsigned and long respectively.
● Character literal is enclosed in single quotes and string literal is enclosed in double quotes.
● Boolean literal can be true or false, the value of true will be 1 and false will be 0.
● Constants can be of any of the basic data types and can be declared as const variables, or with the
#define preprocessor directive.
References