C++ Constants and Literals

C++ Literals

In programming Literal is an object that represent a fixed value in your code. We already have used literals in the previous programs. For example, when you assign a value to a variable:

double price = 2.0;

Here 2.0 is a double literal. In C++ the following types of literals are used:

Types of Literals in C++

  1. Integer literals
  2. Floating-point literals
  3. Boolean literals
  4. Character literals
  5. String literals

Now we will discuss each kind of literals in details. But before we start – you should create a new project, called “Literals Demo” and add a file called “LiteralsDemo.cpp”. Initially we will start with the empty main function in this file:

//include a header file from Standard Library
#include <iostream>
using namespace std;
//the work of the program starts from function called  main
int main()
{
	return 0;
}

Integer Literal

Integer literals represent different integer values. There are several ways to use integer literals in your code. You can specify a literal with decimal, octal, or hexadecimal base. To specify an integer in decimal base you can simply use it as shown below

int decimalBase = 123;

If you use the literal in the way, shown above – the value of decimalBase variable will be 123 in decimal base.

Octal Base

To represent a literal with octal base you have to add “0” (zero) before the value

int octalBase = 0123;

Hexadecimal Base

For representing an integer in hexadecimal base add (0x or 0X) before the value

int hexadecimalBase = 0x123;

Now we can try to print above variables:

cout << "Decimal base " << decimalBase <<  endl;
cout << "Octal base " << octalBase << endl;
cout << "Hexadecimal base " << hexadecimalBase <<  endl;
cin.ignore();

The output of this program will be:

constants and literals

All the values by default are displayed in decimal base. To set base for cout output you can use setbase function. But before it you need to add the following include line:

#include <iomanip>  // use setbase

Now to specify the base of the input you have to use setbase(int base) with cout object before output of the variable:

cout << "Use decimal base" << endl;
cout << "Decimal base " << decimalBase << endl;
cout << "Octal base " << octalBase << endl;
cout << "Hexadecimal base " << hexadecimalBase << endl;

cout << "Use different bases" << endl;
cout << "Decimal base " << decimalBase << endl;
cout << setbase(8) << "Octal base " << octalBase << endl;
cout << setbase(16) << "Hexadecimal base " << hexadecimalBase << endl;

Now run your program and check the output. You will get an output as shown below

constants and literals

We can also specify a literal of type long and unsigned. For this we have to add “L” at the end of the literal for long type and “U” for unsigned type as shown below

unsigned unsignedVar = 10U;
long longVar = 1000L;

Floating point Literal

A floating point literal consist of decimal values (fractional value) that can be followed by the exponent part if you want. Floating point literals are shown below:

float simple = 13.2;

Here, 13.2 is a floating point literal.

We can use the floating point integers with the exponent part. For example:

float value1 = 0.001;
float value2 = 1.0E-3;

Value2 consist of decimal value 1.0) with exponent part E-3. We can print these values to see the output of value2:

cout << "Value1 is " << value1 << endl;
cout << "Value2 is " << value2 << endl;

The output of the program will be as follows:

constants and literals

As you can see, the value1 is same as value2 because 1-3 = 0.001


Boolean Literal

Boolean literals are used to work with bool data type. For a Boolean value there are two possible values: true and false. It is used in the following way:

bool thisIsTrue = true;
bool thisIsFalse = false;
cout << "True is " << thisIsTrue << "But false  is "  << thisIsFalse << endl;

Below is the output:

constants and literals

cout displays true as “1” and false as “0”


Character Literal

Character literals are the sequence of the characters that are enclosed by single quotes. These literals are used to represent some messages and characters:

‘Hello World’ ‘X’

If the sequence of characters is followed by L like L'Text' it means that this literal has to be stored in variable wchar_t type. For example:

wchar_t ch = L'TutorialCup';

An important note that char variable can store only one character.

char c = 'H';

If you want to store multiple characters then you have to use character array. We will discuss the arrays later in this tutorial.

There are some special characters that are used for different purposes in character literals. These special characters are presented in the following table:

 Character Escape Sequence
 Newline \n
 Horizontal tab \t
 Vertical tab \v
 Backspace \b
 Carriage return \r
 Formfeed \f
 Alert \a
 Backslash \\
 Question mark \?
 Single quotation mark \’
 Double quotation mark \”
 Octal number \ooo
 Hexadecimal number \xhhh
 Null character \0

For example, if you want to use a new line in your character literal, you can do it in the following way:

'This is the first line\nAnd this is the second one'

String Literal

String literals are same as the character literals. The main difference between string and character literals is that string literals are enclosed by the double quote " "

"This is string literal"

You can use the same special characters in your string literals as in the characters literals.


C++ Constants

Constant variables are similar to normal variables except one important property that the value of a constant variable can not be changed after it is defined. Constant variable will be initialized at the time of variable definition as shown below

const int constInt = 100;

If you will try to change the value of a constant variable after its initialization, you will get an error:

//can't do this
//constInt = 5;

Why to use constant variable

Sometime you will encounter a situation when you want that programmer should not be able to change the value of a variable by mistake. In those cases we must declare a variable using constant.

References

https://en.wikipedia.org/wiki/Constant_(computer_programming)

https://en.wikipedia.org/wiki/Literal_(computer_programming)

Translate »