constant and their types

constant

They are also called literals. When we declare a variable in constant variables, So these variables values never ever change during execution or running of a program i.e. their value remains fixed. When you declare the constant variable, we have to initialize it at the same time.

To declare a variable constant, we need to add the “const” keyword before the variable.

SYNTEX:-

const data-type variable_name = value;

constant has two type

I. numeric constant
II.characters constant

But they also have many types. You can understand this from the diagram given below-

 

numeric constant

It contains all kinds of numeric values, there are 2 types of numeric constant-

  • integer constant
  • floating constant

integer constant are 3 types

  • decimal constant
  • octal constant
  • HexaDecimal constant

decimal constant It contains all the normal numeric values (both positive and negative). Whose base is 10 (0-9). like-

const int x = 5; 
const int y = 100;
const int z = -2;

#include <iostream.h>
#include <conio.h>
void main()
{
int x = 5;
int y = 100;
int z = -2;
cout<<x<<endl;
cout<<y<<endl;
cout<<z;
getch();
}
OUTPUT:-
5
100
-2

octal constantIt has base 8 (0-8). like-

const int x = 012; const int y=0200; const int z = 011;

#include <iostream.h>
#include <conio.h>
void main()
{
int x = 012;
int y = 0200;
int z = 011;
clrscr();
cout<<x<<endl;
cout<<y<<endl;
cout<<z;
getch();
}
OUTPUT:-
10 
128 
9

HexaDacimal constant it has Base 16 (0-9 and A-F).

const int x = 0x11; or const int y=0x2f1;

Here is example

#include <iostream.h>
#include <conio.h>
void main()
{
int x = 0x11;
int y = 0x2f1;
cout<<x<<endl;
cout<<y;
getch();
}
OUTPUT:-
17
753

floating constant It contains all the floating numeric type value. like-

const int x = 0.5;const int y= 0.10; const int 4.53;

 

characters constant

There are character values in it. Such as a name or a alphabet. It is divided into two parts-

  • character constant
  • string constant

character constant-

There is a single character in it. In character constant, single quotes are used in the character, such as a character or alphabet

const char x = '5'; 
const char y = 'x';
const char xy = ' '; // null

Example-

#include <iostream.h>
#include <conio.h>
void main()
{
char x = '5';
char y = 'x';
char xy= ' '; //space
cout<<x;
cout<<endl;
cout<<xy;
cout<<endl;
cout<<y;
getch();
}
OUTPUT:-
5

x

string constant

There is a character of sequence or sentence. Like someone’s name string constant is declared in double quotes like-

const char name[ ] = "learnprogramz";
const char y = ""; // null string
const char x[] = " " // 5 space
const char xy[20]= 'learn programz ';

Here is the Program-

#include <iostream.h>
#include <conio.h>
void main()
{
char name[] = "learnprogramz";// string without specifing size
char x[] = "";// null string
char y[] = " "
char xy[15]= "learn programz";//string with space
clrscr();
cout<<name<<endl;
cout<<x<<endl; // space
cout<<y<<endl;
cout<<xy;
getch();
}
OUTPUT:-
learnprogramz

learn programz

We can declare constant variable both locally and globally.


previous-character set | identifier | comments

next-Operator and their types