• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Oops » Constants – Type of Constants in C++
Next →
← Prev

Constants – Type of Constants in C++

By Dinesh Thakur

Constants refer to fixed values that the program may not alter. Constants can be of any of the basic data types. The way each constant is represented depends upon its type. Constants are also called literals.

Definition: “A constant value is the one which does not change during the execution of a program.”

 

Constant uses the secondary storage area. Constants are those quantities whose value does not vary during the execution of the program i.e. constant has fixed value. Constants in every language are the same. For example, in the C++ language some valid constant are: 53.7 , -44.4, +83.65, “Dinesh”, ‘M’, “2013” , “\n” etc.

In C++ language constants are of two types:

 

1. Numeric Constant

2. Non-Numeric Constant (Character Constant)

 

These are further sub-divided into more categories.

We’ll be covering the following topics in this tutorial:

  • Numeric Constant
  • Character Constant
  • Escape Sequences

Numeric Constant

These have numeric value having combination of sequence of digits i.e. from 0-9 as alone digit or combination of 0-9 with or without decimal point (precision value) having positive or negative sign. These are further sub-divided into two categories as:

(i) Integer Numeric Constant

(ii) Float or Real Numeric Constant

(i) Integer Numeric Constant

An Integer Numeric Constant is a sequence of digits (combination of 0-9 digits without any decimal point or without precision), optionally preceded by a plus or minus sign.

There are 3 types of integer numeric constant namely decimal integer, octal integers and hexadecimal integer.

(a) Decimal Integer Numeric Constant: These have no decimal point in it and are either be alone or be the combination of 0-9 digits. These have either +ve or -ve sign. For example: 1214, -1321, 10,254, -78, +99 etc.

(b) Octal Integer Numeric Constant: These consist of combination of digits from 0-7 with positive or negative sign. It has leading with 0 or 0 (upper or lower case) means Octal or octal. For example: 0317,003, -045 etc.

(c) Hexadecimal Integer Numeric Constant: These have hexadecimal data. It has leading ox, OX, Ox or x, X. These have combination of 0-9 and A-F (a-f) or alone. The letters a-f or A-F represents the numbers 10-15. For example: 0x232, 0x92, 0xACD, 0xAEF etc.

(ii) Float or Real Numeric Constant

Float Numeric Constants consists of a fractional part in their representation; Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containing fractional parts like 26.082.

Examples of real constants are: 0.0026, -0.97, 435.29, +487.0, 3.4E-2, 4.5E5

A floating-point constant consists of a sequence of decimal digits, a decimal point, and another sequence of decimal digits. A minus sign can precede the value to denote a negative value. Either the sequence of digits before the decimal point or after the decimal point can be omitted, but not both.

Float Numeric constants are further divided into two parts. One is Mantissa Part and the other is Exponent Part.

(a) Mantissa part: The part without E and having a decimal point is called Mantissa Real part e.g. 45.5, -22.43, 0.5 etc. it is also called without exponent part.

(b) Exponent part: The exponent part has an E within it. It is also called a scientific notation. Here E has base value 10. it computes the power. For example: 4.2xl02 can be written as 4.2E2, 4.2xlO-5 can be written as 4.2E- 5.-Similarly some more valid real numeric constant are as: 54.73 E -4,51.9 E +11 etc.

Character Constant

Character constants have either a single character or group of characters or a character with backslash used for special purpose. These are further subdivided into three types:

(i) Single Character Constant

(ii) String Character Constant

(iii) Backslash Character Constant

(i) Single Character Constant

Single Character constants are enclosed between single quotes(‘). For example, ‘a’ and ‘%’ are both character constants. So these are also called single quote character constant.

For example: ‘a’, ‘M’, ‘5’, ‘+’, ‘1’ etc. are some valid single character constant.

(ii) String Character Constant

C++ supports another type of constant: the string. A string is a set of characters enclosed in double quotes (“). For example: “Punar Deep” is a string.

You must not confuse strings with characters. A single character constant is enclosed in single quotes, as in ‘a’. However, “a” is a string containing only one letter.

For example: “Dinesh”, “Hello”, “2013”, “2013-2020”, “5+3”, “?+!” etc. are some valid string character constant. These are used for printing purpose or display purpose in the C++ program’s output statements. These can also be used for assigning the string data to the character (string) type variables.

(iii) Backslash Character Constants

Enclosing character constants in single quotes works for most printing characters. A few, however, such as the carriage return, can’t be. For this reason, C++ includes the special backslash character constants, shown below, so that you may easily enter these special characters as constants.

These are also referred to as escape sequences. You should use the backslash codes instead of their ASCII equivalents to help ensure portability.

These are used for special purpose in the C++ language. These are used in output statements like cout etc.

Escape Sequences

A list of backslash character constant or escape sequence is as in the below table:

                       List of Backslash Character Constant

You’ll also like:

  1. What do you means by C++ Tokens? Explain Variable,Data Type,Constants, Identifiers and Keyword
  2. What is Constants? Type of constant
  3. What are Constants
  4. PHP Constants Types
  5. Explicit Type Conversion (Type Casting)
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.