Skip to main content

Constants and Literals

Introduction

In C++, Constants are fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types and they can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

Integer Literals

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

Here is an example:

212         // Legal
215u // Legal
0xFeeL // Legal
078 // Illegal: 8 is not an octal digit
032UU // Illegal: cannot repeat a suffix

Floating-point Literals

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.

Here is an example:

3.14159       // Legal
314159E-5L // Legal
510E // Illegal: incomplete exponent
210f // Illegal: no decimal or exponent
.e55 // Illegal: missing integer or fraction

Boolean Literals

There are two Boolean literals and they are part of standard C++ keywords:

  • true
  • false

Character Literals

Character literals are enclosed in single quotes, e.g., 'x' and can be stored in a simple variable of char type.

String Literals

String literals are enclosed in double quotes. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.

"hello, dear"
"hello, \

dear"
"hello, " "d" "ear"

Defining Constants

There are two simple ways in C++ to define constants:

  1. Using #define preprocessor.
  2. Using const keyword.

#define preprocessor

Below is the form to use #define to define a constant:

#define identifier value

Here is an example:

#include <iostream>
using namespace std;

#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'

int main() {
int area;

area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
}

const keyword

You can use const prefix to declare constants with a specific type as follows:

const type variable = value;

Here is an example:

#include <iostream>
using namespace std;

int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;

area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;

return 0;
}

Both of the above examples produce the same result. The only difference between them is the first one uses preprocessor directives, and the second one uses C++ keyword const.

And that's it! With this, we have covered the basics of Constants and Literals in C++. Remember, practice is key when learning a new programming language. So, don't forget to try out these examples and experiment with them on your own. Happy coding!