C++ Constants/Literals

literal are used in programming language to define fixed and constant value of variables and object. Or we can say that constant values like integers, floating point numbers, boolean value are related to literal. let as view an example to understand this points.

//Basic example of literal
#include<iostream>
using namespace std;
int main(){
  //90u is an Integer literal
  int data=90u;
  //0b0101 valid literal 
  int binary=0b0101;//0b indicates binary
  //true is an literals
  bool status=true;
  
  cout<<" data :"<<data<<endl;
  cout<<" binary :"<<binary<<endl;
  cout<<" status :"<<status<<endl;
}
Output
 data :90
 binary :5
 status :1

Note that in given program Literals are used to initialize the initial value of variables. And possible to modify of variable value in during of program execution. but literals is an constant regular expression.

Type of Literals

In C++ programming they are supported various type of literals.

Boolean Literal

In C++ programming boolean data type are allowed two of value. true and false. true are internally represented of 1 (positive integer 1). And false are relate to 0 (zero). This (true and false) are constants literals.

//boolean literal example
#include<iostream>
using namespace std;
int main(){
  int status=false; //false is Literal
  cout<<" initial status "<<status<<endl;
  
  if(status==0){
    status=true; //true is Literal
    cout<<" After status "<<status<<endl;
  }
}
Output
 initial status 0
 After status 1

Character Literal

character literal are used to (' ') single quotes for characters. here given of few examples.

//Character Literals Example
#include<iostream>
using namespace std;
int main(){
  //Character Literals
  char status='T';
  char escape='\?'; //Escape sequence
  //Hexa Escape sequence
  char hexa_escaps='\x42';//B
  cout<<"status :"<<status<<endl;
  cout<<"escape :"<<escape<<endl;
  cout<<"hexa_escaps :"<<hexa_escaps<<endl;
  return 0; 
}
Output
status :T
escape :?
hexa_escaps :B

character literal are defined in three ways. first one is very simple ('T') with single quote single character. second one using escape sequence, and third one is hex escape sequence.

Integer Literal

Integer literal are combination of various type.

//Integer literals
#include<iostream>
using namespace std;
int main(){
  //0b00111 is binary literal
  //0b represent of binary
  int binary=0b00111; 

  //0xA is Hexadecimal literal
  //0x represent of Hexadecimal
  int hexa=0xA;

  //012 is Octal literal
  //start with 0 represent of Octal
  int octal=012;

  int decimal=100; //decimal literal

  //end with u is represent of unsigned int
  unsigned int un_int=123u;

  //end with ul is represent of unsigned long
  unsigned long un_long_int=123432ul;

  //end with l is represent of long int
  long int long_int=122222l;
  
  cout<<" binary :"<<binary<<endl;
  cout<<" hexa :"<<hexa<<endl;
  cout<<" octal :"<<octal<<endl;
  cout<<" decimal :"<<decimal<<endl;
  cout<<" un_int :"<<un_int<<endl;
  cout<<" un_long_int : "<<un_long_int<<endl;
  cout<<" long_int : "<<long_int<<endl;
  return 0; 
}
Output
 binary :7
 hexa :10
 octal :10
 decimal :100
 un_int :123
 un_long_int : 123432
 long_int : 122222

Floating Point Literal

There is an decimal number and fraction part of decimal value.

//valid floating point literal
  34.56;  //double value
  34.65f; //float value
  34.24e20;//scientific notation 
For Example
#include<iostream>
using namespace std;
int main(){
  //562.45f is an floating point literal
  //f is represent of floating data
  float normal=562.45f;

  //34.23e7 is literal of scientific notation 
  //34.23e7=34.23X10^7
  double spcial=34.23e7;
  //Display result
  cout<<normal<<endl;
  cout<<spcial<<endl;
  return 0; //0 is an literal 
}
Output
562.45
3.423e+008

Object Literal

We know about that the literals, this are some constant value those are assign of variables. There are possible to assign literal value to object? that question is arise in our mind. so Answer is yes. there are possible. look at this example.

#include<iostream>
//for string class
#include<string>
using namespace std;
int main(){
  //"programming" is literal (String Literal)
  //string is an class
  //obj is object of string class
  string obj="Programming"; 
  cout<<obj; //display values
  return 0; 
}
Output
Programming

In this program string is an class which are defined by string header file. and obj is an object of class string.

© 2019, regularcodes.com, All rights reserved