Constants & Literals in C++

In this section of the C++ tutorial, we will have a look at the constants & literals in C++.

Constants in C++

Constant in C++ are similar to variables with exceptional properties. The value of the constant variable can’t be modified throughout the program. Its value is initialized at the time of declaration only.

Ways to define constants in C++:

  • Using #define preprocessor.

Example:

#define constexp 500;

  • Using the const keyword.

Example:

const int constexp = 500;

Literals in C++

In the C++ programming language, Literal is an object that depicts a fixed value in your C++ program.

Example:

double amount= 500.0;

Types of literals in C++:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping cart