C++ Variables and Constants/Literals Tutorial [BCASTUDY.IN]

Ashutosh Kumar
By -
0
Variables and Constants/Literals C++ Programming language



Hi, IGNOU BCA Reader.

In this post, We will learn about C++ Variables and Constants/Literals.

VARIABLES


In any programming language, We need to work on some data. That data is stored in named reference. For this purpose, A Variable concept is used.
A Variable is used to stored data in memory with named symbolic reference. The variables is used to hold different values at different times during the execution of a program.
 
let suppose you are declaring more than one variable of same datatype, you can declare all in single statement by seprating the identifiers name with comma operator (,). 
e.g - 

int x, y, z;

We can declare same thing this way also. ( But above declaration is better )
 
int x;
int y;
int z;


CONSTANTS/LITERALS

A Constants is similar to the Variables But difference is that it doesn't change the value during execution of program. Once you declared a number as constant it is going to never change. Thing about Pie value that is 3.1215 and It is going to never change in a program. So, We can declared that as constant. 

 

A keyword const is prepended to constant/literals value. 

 

const float PI = 3.1215;

 

let talk about scope of variables or constant. 

 

Scope of Constant/Variables

A variable can be either of global or local scope accordingly needs. A Global variables are the variables that is declared in main body of C++ source code file, outside of all the function. Global variables can be called from anywhere in code, even inside functions.




 
 


#include <iostream.h>
using namespace std;

const int Pie = 3.14285;
char myFavriteLatter;
char fullName[20];

int main(){
	
    unsigned short rollNumber; 
    
    cout<<"Enter your your name: ";
    cin>>fullName;
    
    return 0; // Successful exit 
}



In Above given example, I have created a program that has used Global/Local scope variables.

Post a Comment

0Comments

✍️ Only article/post centric comments are allowed.
❌**No Spam**
❌**Advertisement**
❌**No abuse**
Rules strictly applied ✅

Post a Comment (0)