C++ Constants/Literals3 min read

C++ Constant

Constant in C++ refers to the values in the program whose value during the entire execution of the program. And these fixed values are called Literals.

These constants may be any of the data types present in C++ such as Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

They are just like the regular variables, the only difference is that their value cannot be changed once they are defined in the program. The constant is defined by the keyword const in a program.

Syntax:

The constant value cannot be modified later in the program.

Although, there are two ways in which you can define a constant. They are:

  1. By the use #define preprocessor.
  2. Another with keyword const itself.

1. #define: Syntax

Example: C++ example for constant.

2. constant: Syntax

Example: C++ example for constant.


C++ Literals

As mentioned above, literals are the fix value. They are the notation used in a program for fix value.
For example: 1, 2.5'c' etc. are the literals because you cannot assign different values to these terms.

The following are the various literals in C++ programming.

1. Integers Literals

An integer literal is a numeric value without any fractional or exponential part. The following are the three integer literal in C++:

  • octal (base 8): prefix – 0, starts with 0. (0, 1, 2, 3, 4, 5, 6, 7)
  • hexadecimal (base 16): prefix – 0x or 0X (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F)
  • decimal (base 10): No prefix. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9. )

The Integer Literal may have U or L as a suffix for unsigned and long integers


2. Floating-point Literals

Floating-point literals are the numeric value with fractional part or exponential part. The signed exponent is introduced by e or E.

Example:

Note: e-14 means 10^-14


3. Boolean Literals

There are two Boolean literals and they are the keyword true and false. They are part of standard C++ keywords.


4. character-literal

A character literal refers to the single character enclosed within a single quote.
Example'b''A''5'')''}' etc.

A character Literal may be an escape sequence that is '\t' or it can be universal character that is ‘\u02C0’.

Escape Sequence: There are some characters with the special meaning when used with backslash (\)that cannot be typed but only when used such as the new line ( '\n'), tab ( '\t'), etc.

The following are the list of some escape sequence.

Escape sequenceMeaning
\\\ character
\’‘ character
\”” character
\?? character
\aAlert or bell
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tHorizontal tab
\vVertical tab

5. String Literals

A string literal refers to the sequence of characters enclosed in double quote. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.

Example:

Later on this C++ tutorial, you will learn more on String.


MORE

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …