C++ - SPLessons

C++ Constant

Chapter 8

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Constant

C++ Constant

Constants

shape Description

C++ Constants are same as the identifiers which are fixed values and cannot be modified once they are defined. CPP Constant is represented by a name in the program. This name can be used same as the variable name.

CPP Constant of type integer can be declared in the following two ways.

1) Defining CPP Constant with const keyword : const keyword is a modifier that can be applied to any variable declaration. A variable declared to be const cant be modified during program execution. It is only initialized only at the time of declaration.

Const int a=1;
Int const a=1;

2) Defining CPP Constant with #define directive : Directive #define is also used to declare the constants.

#define a 1 (pre-processor directive)

Differences between #define and const

  1. When #define is used,all constants gets replaced with their actual values before compilation by the pre-processor and const is used by the compiler.
  2. #define is always global, const can be local(global also).
  3. #define does not have type checking where as type checking is part of const.

shape Types

Constant Type of Value Stored
Integer Constant Stores integer value
Floating Constant Stores float value
Character Constant Stores character value
String Constant Stores string value

C++-Integer constant

shape Description

An Integer constant may be “Decimal (base 10), Octal (base 8), or Hexadecimal (base 16) Number” that represents an integral value.

Types of Integer Constants

shape Description

Integer constants are of four types. Along with these four, there are other two constants called “Backslash Character and Wide Character Constant”.

Decimal Integer Constant

It takes values from “0 to 9” and sign is optional.

Floating Point Integer Constant

It represents a real integer value i.e it takes “Fractional, Decimal and Integer Values”.

Character constants

A character constant can be an “Alphabet, a Digit or a Single Special Symbol”.

String Constants

String constant is same as character constant, and can be enclosed in double quotes " ". There is no special datatype for string.

For example, char area= ” A”; Here “char” is only used as datatype.

Backlash Constant is another constant that is used in C-language.

Backslash character constants

This should be started with backslash.

Symbol Backslash Symbol
\b Backspace
\v Vertical tab
\” Double Quote
\r Carriage Return
\t Horizontal Tab
\f Form feed
\\ Backslash
\? Question Mark
\a Alert or Bell
\’ Single Quote
\n New line

Wide character Constants

Wide Characters in C++ are declared using Wide Character Constants. The values up to 16 bits are allowed here. To make a constant wide character place” L before it.

Syntax: wchar_t character_name=L'character';

For example,

wchar_t  c=L’A’;

shape Example

#include<iostream>
using namespace std;
int main()
{
	const double pi=3.14159265;
	double circumference;
	int radius=3;
	circumference=2*pi*radius;
	cout << "circumference is " << circumference << endl;
	return 0;
}

Output:

circumference is 18.8496

Summary

shape Key Points

CPP Constant chapter draws out following important points.

  • CPP Constant cannot be modified
  • “Integer, Float, Character, Backslash and String” are constant types
  • Wide characters can be used by placing L before the value.

shape Programming Tips

Always enclose string constants in double quotes only, otherwise it is considered as character value.