.

Constants/Literals in C++

In programming Literal is an object which represents a fixed value in the code. When the value is fixed, the value should not be altered throughout the program. Constants are defined with static values. Constants are divided into different data types.

They are

  • Integer literals
  • Floating Point Literals
  • Boolean literals
  • Character literals
  • String literals

Integer Literals

The integer literals are numerical which identify integer values that are not enclosed in quotes or special character. The integer literals are simple succession of digits representing a whole number in decimal digits.

Example

    75         // int
    75u        // unsigned int
    75l        // long
    75ul       // unsigned long 
    75lu       // unsigned long
    

Floating Point Literals

They expressed as real values, with decimals and/or exponents. These literals includes either a decimal point or an e character or both a decimal point and an e character.

Example

    3.14159L	// long double
    6.0875 f	// float
    

Boolean Literals

The keyword used in this literals exist in C++ are  true, false and nullptr. true and false are the two possible values of  variables of type bool. nullptr represent the null pointer value

Example

    boolpoo = true;
    bool bar = false;
    int* S = nullptr
    

Character and String Literals

The first two expressions mentioned above represent single-character literals and the next literal represent string literals composed of several characters. For a single character, Single quotes (') is used, and to express a string. String literals and single-characters require quotation marks which distinguish them from reserved keywords. Note down the difference between these two expressions.

Defining Constants

The value cannot be changed at the execution time of program is defined as identifier. In general constant is used to represent as fixed values in programming. Constants are of different types. The single character constant is defined as the single character which is enclosed between single cote '.

constants literals img1

The string character constant is defined as the set of characters which are enclosed between double cotes " ".

There are two different types of defining constants. They are

# define preprocessor - # defines the constant.

Example

#define length 15

Const keyword – declare with specific type

Example

Const int area = 10;.

.