TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Friday, 22 May 2020

Variables In C++ | Variables In C++ Programming | Variables In C++ Programming Language | Variables In C++ In Hindi

Variables in C++


A variable is a name which is associated with a value that can be changed.


Syntax of declaring a variable in C++

Data_type   vriable_name=value;


For example:

int num=20;


Types of variables

Variables can be categorised based on their data type.

int: These type of of variables holds integer value.

char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc.

bool: holds boolean value true or false.

double: double-precision floating point value.

float: Single-precision floating point value.

 

Types of variables based on their scope

we have understood what is scope. Lets move on to the types of variables based on the scope.

1. Global variable
2. Local variable


image of variable of c++
Fig: variable in c++


Global Variable

A variable declared outside of any function (including main as well) is called global variable. Global variables have their scope throughout the program, they can be accessed anywhere in the program, in the main, in the user defined function, anywhere.

Global variable example

#include<iostream.h>

using namespace std;

//this is a global variable

char myvar=’A’;

int main()

{

cout<<”value of myVar:”<<myVar<<endl;

myVar=’Z’;

count<<”value of myVar:”<<myVar;

return 0;

}


Output:

Value of myvar: A

Value of myVar: Z


Local variable

Local variables are declared inside the braces of any user defined function, main function, loops or any control statements(if, if-else etc) and have their scope limited inside those braces.

Local variable example

#include<iostream.h>

using namespace std;

char myFun()

{

//this is a local variable

char myvar=’A’;

}

int main()

{

cout<<”value of myVar:”<<myVar<<endl;

myVar=’Z’;

count<<”value of myVar:”<<myVar;

return 0;

}

Output:


Compile time error, 

 

 

 





No comments:

Post a Comment