Constant In C++

constant in c plus plus

A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants never change in value. You must initialize a constant when it is created. C++ has two types of constants: literal and symbolic.

Literal Constant

Literals are data used for representing fixed values. They can be used directly in the code. For example: 12.5'c' etc.

Here, 12.5 and 'c' are literals. Why? You cannot assign different values to these terms.

Here’s a list of different literals in C++ programming.

  1. Integer Literal
  2. Floating-point Literals
  3. Characters
  4. Escape Sequences
  5. String Literals

1. Integer Literal

An integer is a numeric literal(associated with numbers) without any fractional or exponential part. There are three types of integer literals in C programming:

  • decimal (base 10)
  • octal (base 8)
  • hexadecimal (base 16)

For example:

Decimal: 0, -9, 22 etc
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 etc

In C++ programming, octal starts with a 0, and hexadecimal starts with a 0x.

2. Floating Point Literal

A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For example:

-2.0

0.0000234

-0.22E-5

3. Character Literal

A character literal is created by enclosing a single character inside single quotation marks. For example: 'a''m''F''2''}' etc.

4. Escape Sequences

Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C++ programming. For example, newline (enter), tab, question mark, etc.

In order to use these characters, escape sequences are used.

Escape SequencesCharacters
\bBackspace
\fForm feed
\nNewline
\rReturn
\tHorizontal tab
\vVertical tab
\\Backslash
\'Single quotation mark
\"Double quotation mark
\?Question mark
\0Null Character

5. String Literals

A string literal is a sequence of characters enclosed in double-quote marks. For example:

"good"string constant
""null string constant
" "string constant of six white space
"x"string constant having a single character
"Earth is round\n"prints string with a newline

Symbolic Constant

The symbolic constant is a way of defining a variable constant whose value cannot be changed. It is done by using the keyword const.

For example

const int c=5;

In C symbolic constant can be achieved by the use of #define.

For example

#define PI=3.142;

When this statement is compiled, the pre-processor(#) will replace all the occurrence of PI with 3.142, which is then compiled into executable format.

A problem with this was the we cannot use any normal variable with same name PI throughout the program otherwise it will be replaced by its constant value. So this problem is solved in C++, by the use of const qualifier. The scoping of const values differs. A const in C++ is default to the internal linkage and therefore, it is local to the file where is is declared. While in C, constants values are global in nature. They are visible outside the file which they are declared. They can be made local by declaring them as static. To access the const value from another file we must explicitly define it as extern in C++.

For example 

extern const total=100;

Symbolic constant can also be defined using enumeration.

For example

enum{n1,n2};

The enumerator n1 and n2 can be assigned constant value in following way:

const n1=1;
const n2=70;
5 1 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x