Constants in C++ Programming Language

Constant in C++ Programming Language
Constant in C++ Programming Language

What is Constants in C++: A constant can be used program that value cannot be changed at the time of execution of program that is called constant. Constants are fixed values that a program cannot change during its execution. Unlike variables, the value of constants is set once when they are declared. It is also called literals. It may be integer, float and character data types constant. Constant in C++ are fixed value that is can't change value of the run time program.

If the code contains a change to a constant, the compiler will generate an error. Constants can be of the basic data types, such as int, float, char, etc.

Const Keyword: A constant keyword makes a variable read only and is handling by the compiler provided security and program scope.

How To Use Constant in C++

#include<iostream>
using namespace std;
#include<string.h>
int main(){
const int x=10;
const float y=65.8;
const char c='J';
const char n[20]="jiocoding"; 
cout<<"Value of x:"<<x<<endl;
cout<<"Value of y:"<<y<<endl;
cout<<"Value of c:"<<c<<endl;
cout<<"Value of n:"<<n<<endl;
}
============OUTPUT============
Value of x:10
Value of y:65.8
Value of c:J
Value of n: jiocoding

#define Preprocessing Constant: This is a Constant defines a symbolic constant. There is no need considerer any data type.

Example of Preprocessing Constant in C++

#include<iostream>
using namespace std;
#define PI 3.14
int main(){
cout<<"Value of PI:"<<PI;
}
============OUTPUT============
Value of PI:3.14

What is Integer Constant in C++

Integer Constants: Integer Constant is a whole numbers positive, negative, or zero without decimal points. The range of integer constant is between -32768 To +32768. No comma or blank space are not allowed in integer constant.

Example of Integer Constant in C++

#include<iostream>
using namespace std;
int main(){
const int x=20;
cout<<"Value of x:"<<x;
}
============OUTPUT============
Value of x:20


Floating Constant in C++ with Example

Floating-Point Constants: Floating point number is a at least one digit. It is decimal point number. It is a positive or negative number. No comma or blank space are not allowed in floating number.

Example of Floating Constant in C++

#include<iostream.h>
using namespace std;
int main(){
clrscr();
const float x=75.8;
cout<<"Value of x:"<<x;
}
============OUTPUT============
Value of x:75.8


Character Constant in C++ Language

Character Constants: A single character, digit, or special symbol. The length of character constant is 1 character. Enclosed within single quotes( 'J', '9', '*'). Each character has an associated ASCII value.

Example of Character Constant in C++

#include<iostream>
using namespace std;
int main(){
clrscr();
const char j='A';
cout<<"Value of J:"<<j;
}
============OUTPUT============
Value of J:A

String Constant in C++ Programming

String Constants: A sequence of characters including letters, digits, and special symbols enclosed in double quotes ("Jiocoding", "Jiocoding@").It is called by string constant.

Example of String Constant in C++

#include<iostream>
using namespace std;
#include<string.h>
int main(){
const char name[10]="Jiocoding";
cout<<"Value of Name:"<<name;
}
============OUTPUT============
Value of Name: Jiocoding


Constant Conclusion in C++ Language

Conclusion: A constant is a important part of C++ language. Constant value is not changeable. I hove your doubt constant related are clear. If any query you can comment or contact me.



Post a Comment

0 Comments