Types of Constant in C++ | Types of Literals in C++ 

constants in c++

Types of Constant in C++ | Types of Literals in C++ 



What is a constant/literal in C++?

It is a quantity whose value we can’t change during the execution of program. They are of two types.

  • Numeric Constant
  • Character Constant

Numeric Constant/Literal

Numeric Constants contain digits only. They are of two types

  • Integer Constant
  • Floating point Constant

Integer Constant in C++

Integer Constants can contain numeric values without decimal point in them.

They are of three types.

    1. Decimal Integer Constant
    2. Octal Integer Constant
    3. Hexadecimal Integer Constant

Decimal Integer Constant

It contains digits from 0 to 9. So its base is 10

Examples:  6, 45, 5467 etc.

Examples of Invalid Decimal Integer constant are

16,62    //Comma not allowed.

3.6      //Decimal point not allowed

042      //Can’t start with zero

3   33  // Can\t contain space.

$24       //Can’t contain special characters

Octal Integer Constant

It contains digits from 0 to 7. Its base is 8.

Octal Numbers start with 0.

Example:  0123, 06663

Examples of invalid Octal Integer Constants are 

49       //Must start with 0.

058      //Can’t contain  8.

Hexadecimal Integer Constant

It contains digits from 0 to 9 and alphabets from A to F. where

A = 10

B = 11

C = 12

D = 13

E = 14

F = 15

Its base is 16. Hexadecimal Number starts with 0X.

Example: 0X23, 0X6f9A

Examples of invalid Hexadecimal Integer Constants are 

625        //must start with 0X.

11.5      //Can’t contain decimal point]

Floating point Literal/Constant in C++

Floating point constants contain numbers with decimal point.  They can be used to represent very small as well as very large quantities.

Real constants can be used in two ways

  • Standard Form
  • Exponent Form

Standard Form

In this form, floating point number contains digits followed by a dot (.) further followed by digits called fractional part.

Examples:  12.33 ,     3.5,      -1.5 ,     +5.0,     .55,

Exponent Form

This type of floating point number  is represented in terms of power of 10.

Power of 10 is represented by alphabets E or e .

There are two parts of number in exponent form.

  • Mantissa: Digits before the alphabet E.
  • Exponent: Digits after the alphabet E. It can only contain integers.

Examples

1.45 х 105  can be represented as 1.45E5

3.5590  х 10-2  can be represented as 3.5590e-2

Examples of invalid floating point  constants in Exponent form

1.45E2.5   //Exponent Part contains floating point Number

4.55e  4       //Exponent Part contains space between 3 and 4.

Character Literal/Constant in C++

Character Constant in C++ can store alphabets, digits, special symbols as well as blank spaces.

They are of three types

  • Single Character
  • String
  • Escape Sequence

i. Single Character Constant in C++

It contains a single alphabet, digit, special character or space within single quotes.

Examples:  ‘A’ , ‘8’ , ‘$’,  ‘@’

ii. String Constant in C++

It  contains multiple characters within double quotes (Inverted commas “).

String constant always ends with a special character known as null represented as ‘\0’

Examples:  “Aman” , “2022” , “$600”,  “lovejot@lovejot.com” etc.

“Aman” is a string constant that contains 5 characters  as there are four characters  in string “Aman” and one character is for null character \0.

iii. Escape Sequence in C++



Spread the love
Lesson tags: Character Constant in C++, escape sequence in c, Floating point Constant in C++, Integer Constant in C++, String Constant in C++
Back to: C++ Programming Notes
Spread the love