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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

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 …