Constants/Literals in C++

Constant is refer to fixed values that the program may not alter and they are called literals.

Constants can be of any of the fundamental data types and can be isolated into Integer Number, Floating-Point Numerals, Characters, Strings and Boolean Values.

constants are dealt with simply like ordinary variables aside from that their values can't be altered after their definition.

Defining Constants

There are two straightforward ways in C++ to characterize constants −

  • Using #define preprocessor
  • Using const keyword.
1. #define preprocessor

Syntax: -
  • #define identifier value
Example: -

#include <iostream>
using namespace std;
#define PI 3.14

int main() {
cout << PI;
return 0;
}

3.14
2. const keyword

Syntax: -
  • const type variable value;
Example: -

#include <iostream>
using namespace std;

int main() {
const int a = 10;
cout << a;
return 0;
}

10

Integer Literals

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix.

Integer Literals Prefix
DecimalNothing
Octal 0
Hexadecimal 0x
intNothing
unsigned int u
long l
unsigned longul

Examples: -
85 //It is Decimal Literals because there is no prefix in number
0 213 //It is Octal Literals because there is 0 prefix in number
0x 1a //It is Hexadecimal Literals because there is 0x prefix in number
30 //It is int Literals because there is no prefix in number
30 u //It is unsigned int Literals because there is u prefix in number
40 l //It is long Literals because there is l prefix in number
50 ul //It is unsigned long Literals because there is ul prefix in number


Floating-Point Literals

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.


Floating-Point Literals Sign
Decimal Form. (point)
Exponential Form E or e

Examples: -
3 . 14 //It is decimal form Literals because there is .(point)
314 E -2L //It is exponential form Literals because there is E.


Boolean Literals

There are two Boolean literals and they are part of standard C++ keywords −

  • A value of true representing true.
  • A value of false representing false.


Characters Literals

Character literals are enclosed in single quotes. If the literal begins with L (uppercase only), it is a wide character literal (e.g., L'x') and should be stored in wchar_t type of variable . Otherwise, it is a narrow character literal (e.g., 'x') and can be stored in a simple variable of char type.

There are certain characters in C++ when they are preceded by a backslash they will have special meaning

you have a list of some of such escape sequence codes here−

Escape Sequences Meaning
\\ \
\''
\""
\??
\aAlert or Bell
\bBackspace
\fForm Feed
\nNewLine
\tHorizontal Tab
\vVertical Tab

Examples: -
#include <iostream>
using namespace std;

int main() {
cout << "Character /n Literals" ; return 0;
}

Character
Literals


2 Comments

Click here for Comments
Tech2020
admin
May 26, 2020 at 5:42 PM ×

Hi sir say me scope of c++ ni marketing. And what kind of job I can get after learning c++

Reply
avatar
Raj Kamariya
admin
May 26, 2020 at 6:53 PM ×

Hi Tech2020, you can get following types of jobs after learning c++.
1 Junior Programmer
2 Senior Programmer
3 Software Developer
4 Quality Analyst
5 Game Programmer
6 Software Developer Engineer
7 C/C++ Analyst
and many more i will write article on this topic.so, don't worry

Reply
avatar