Constants in C++ | Literals in C++

In this article, we will learn about constants and literals in C++.

What are Constants in C++?

Constants are expressions whose values remain fixed. Once defined, we cannot change the value of a constant.

What are Literals in C++?

The fixed value assigned to a constant is called literal. For example, in constant expression const int x = 10; the value 10 is called constant integer literal.

Following are the types of literals:

1. Integer Literals in C++

An integer is a numeric literal that does not contain any fractional or exponential parts. Integer literals are expressed as:

a. Prefix

It specifies the base or radix. These are of four types: no prefix for decimal (base 10), 0 for octal (base 8), 0x or 0X for hexadecimal (base 16) and 0b or 0B for binary (base 2).

For example:

  • Decimal: 97, 3, -62, etc.
  • Octal: 04, 021, 0743, etc.
  • Hexadecimal: 0x2, 0X4a, 0x56c, etc.
  • Binary: 0b10, 0B1011, 0b110, etc.
b. Suffix

It represents the type, u or U for unsigned, l or L for long and combination of both.

For example:

  • 42 //int requires no suffix
  • 42u //unsigned int
  • 42ul //unsigned long
  • 42L //long
  • 42ll //long long

2. Floating-point Literals in C++

Floating-point literals, which represent real numbers, have an integer part, a real component, a fractional component, and an exponential part. We can store it either in decimal or in exponential form.

We must remember that

  • The decimal point, exponential part, or both must be included in the decimal form; otherwise, an error will occur.
  • In the exponential form, the integer, fractional, or both parts must be included; otherwise, an error will occur.

Some examples of valid floating-point literals are 12.365, 2.67E-6, etc.

Some examples of invalid floating-point literals are 256E, 0.e34, etc.

3. Character Literals in C++

Character literals store a single character enclosed within a single quote. These have two representations:

  • Normal or narrow character literal of char type. For example, ‘i’.
  • Wide character literal of wchar_t type which begins with uppercase L. For example, L’i’.

There are various characters in C++ that have special meaning. These character literals are Escape Sequences. Following is a list of some escape sequences in C++.

Escape Sequence Meaning
\a Alert or beep
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\\ Backlash
\’ Single quote
\” Double quote
\? Question mark
\0 Null character
\ooo Octal number 
\xhh Hexadecimal number 

Example to illustrate the use of escape sequences in C++

#include <iostream>
using namespace std;

int main() {
  cout<<"TechVidvan\nGoogle\n";
  cout<<"Tech\tVidvan";
  return 0;
}

Output

TechVidvan
Google
Tech Vidvan

4. String Literals in C++

String literals store multiple characters enclosed in double quotes. They can also include special characters, escape sequences and whitespaces.

For example: “TechVidvan”, “Data Flair”, “Hello World!\n”, etc.

5. Boolean Literals in C++

These represent boolean values, which are of two types:

true: represents true value. We should not consider its value equal to int 1.

false: represents false value. We should not consider its value equal to int 0.

Defining Constants in C++

We can define a constant in one of the two ways,

1. Using const keyword

We can define a constant using const keyword as prefix.
Syntax:

const datatype name = value;

Example to define constants using const

#include <iostream>
using namespace std;

int main() {
  const int a = 10;       //integer literal = 10
  const float b = 50.74;      //floating point literal = 50.74
  const char newline = '\n';      //character literal = \n
  
  cout<<"Defined constants:"<<newline;
  cout<<a<<newline;
  cout<<b;
  return 0;
}

Output

Defined constants:
10
50.74

Let’s try to modify value of a constant

#include <iostream>
using namespace std;

int main() {
  const int a = 10;
  a = 20;
  cout<<a;
  return 0;
}

Status Compilation error
Output

In function ‘int main()’:
error: assignment of read-only variable ‘a’

We get a compilation error that variable a is read-only.

2. Using #define preprocessor directive

We can use #define preprocessor directive to declare constants.
Syntax:

#define name value

Example to define constants using #define

#include <iostream>
using namespace std;

#define length 12.5
#define breadth 8
#define unit "m sq."

int main() {
  double area;
  area = length * breadth;
  cout<<area<<" "<<unit;
  return 0;
}

Output

100 m sq.

Summary

In this article, we learnt what constants are. We talked about literals in C++. Literals can be of integer, floating-point, character, string or boolean type. There are two ways in which we can define constants. Using suitable examples, we learnt both these ways.