C++ Constants

Constants

C++ Constants are actually like fixed variables. Their value is declared in the beginning and cannot be altered afterward. In the C++ programming language, Constants are basically fixed values. These values are unchangeable throughout the program. These are also called Literals.

C++ Constants Data types

C++ constants have the same data types as C++ Variables. So, the basic data types for defining a constant are 5, given as:

  • Integer Numerals
  • Floating-point Numerals
  • Boolean
  • Characters
  • Strings

Integer Constants

C++ Constants can have integer values. The integers include decimal, octal, and hexadecimal values. Decimal type constants have no suffix or prefix. Whereas we use prefix 0 for Octal type constants. And for Hexadecimal type constants we use 0x or 0X prefix. Integers can also have suffixes like u and l. Literal u is used for unsigned integers. While literal l is for long integers. These suffixes can be used both alone and in combination. Both uppercase and lowercase letters are accepted syntaxes. Examples of Integer Constants are shown below:

398          // decimal 
0723         // octal
0x64         // hexadecimal
0X312        // hexadecimal
50u          // unsigned int 
50uL         // unsigned long 
50l          // long

Floating-point Constants

A floating-point number consists of an integer part and a decimal point. Other than decimals, floating-point numbers are fractions and exponents. The decimal number consists of a decimal point between the integer and the fractional parts. The exponential number contains the literal E. The integer part is written before the literal E. And the exponential part is written after E.  Both uppercase E and lowercase e are acceptable syntaxes. Here are some examples to get a better understanding:

788.299      // fraction 
9.000        // fraction 
20E5         // exponential 
429e-4       // exponential 
2.63e9       // exponential fraction

Boolean Constants

Another C++ constants type is Boolean constants. There are two Booleans True and False in C++ programming language. Booleans lie in the keyword category.

  • The True keyword is for true expressions.
  • The False keyword is for false expressions.

Characters

Character literals consist of sequences of characters or escape sequences. The characters enclosed in single quotation marks are either character literals or universal constants. Character literals are of two types, given below:

  • Wide character literal: The data type of wide character literal is wchar_t type. It starts with the uppercase letter L.
  • Narrow character literal: The data type of narrow character literal is char type.

The characters those start with a backslash have specific meaning and purpose. Following are the examples of different types of character literals:

'x'          // x character 
L'y'         // y wide character
\n           // new line 
\t           // tab
\b           // backslash 
\?           // ? character

Strings

The characters enclosed in double quotation marks are string literals. We can write sentences with white spaces as strings. Note that strings can also contain escape sequences. Here are some examples:

"Hello World!" 
"Introduction to C++" 
"Hello \n World"

Here is a code that displays simple sentences in two lines:

#include <iostream>
using namespace std;
int main() {
  cout << "Hello World! \nThis my first code.";
  return 0;
}

The output of above code is:

Hello World! 
This my first code.

Defining C++ Constants

There are two methods of defining C++ constants. The first method is to define by a keyword. And the second method is to define by a preprocessor.

Defining by Keyword

We use the keyword const to define a constant. The syntax of defining a constant is:

const type CONSTANT = value;

The best practice is to write constants in uppercase letters. Here Type is the data type that we need to select for our constant. Constant is the name of the constant. Whereas, Value is the value of the constant. An example will help explain better the syntax and use of constants.

#include <iostream>
using namespace std;

int main() {
const int HEIGHT = 20;
const float BREADTH = 10.5;
const char NEWLINE = '\n';
float area;

area = HEIGHT * BREADTH;
cout << area;
cout << NEWLINE;
return 0;
}

The above code generates the following result after compilation:

210

Defining by Preprocessor

Another method of defining constants is by using the preprocessor #define. By using the #define preprocessor there is no need to declare the constant’s type separately. The syntax of writing preprocessor is:

#define identifier value

Here identifier is the name we wish to give to the constant. Whereas, value is the desired constant’s value. For instance, the code below will show the use of preprocessor #define for defining constants of various data types.

#include <iostream>
using namespace std;

#define HEIGHT 4
#define BREADTH 3.5
#define NEWLINE '\n'

int main() {
float area;

area = HEIGHT * BREADTH;
cout << area;
cout << NEWLINE;
return 0;
}



The above code generates the following result after compilation:

14