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
Integer Literals
Floating-Point Literals
Boolean Literals
Characters 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.
Syntax: -
2. const keyword
- #define identifier value
#include
<iostream>
using namespace std;
#define PI 3.14
int main() {
using namespace std;
#define PI 3.14
int main() {
cout << PI;
return 0;
}return 0;
3.14
Syntax: -
- const type variable value;
#include
<iostream>
using namespace std;
int main() {
using namespace std;
int main() {
const int
a =
10;
cout << a;
return 0;
}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.
Examples: -
Integer Literals | Prefix |
---|---|
Decimal | Nothing |
Octal | 0 |
Hexadecimal | 0x |
int | Nothing |
unsigned int | u |
long | l |
unsigned long | ul |
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
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.
Examples: -
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.
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−
Examples: -
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 |
---|---|
\\ | \ |
\' | ' |
\" | " |
\? | ? |
\a | Alert or Bell |
\b | Backspace |
\f | Form Feed |
\n | NewLine |
\t | Horizontal Tab |
\v | Vertical Tab |
Examples: -
#include
<iostream>
using namespace std;
int main() {
using namespace std;
int main() {
cout <<
"Character /n Literals" ;
return
0;
}
Character
Literals
Literals
2 Comments
Click here for CommentsHi sir say me scope of c++ ni marketing. And what kind of job I can get after learning c++
ReplyHi Tech2020, you can get following types of jobs after learning c++.
Reply1 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
ConversionConversion EmoticonEmoticon