knowledgediarybd.com

Your Virtual Knowledge Campus







C++ Variable & It’s Declaration into the code

C++ Variable & It’s Declaration into the code

 

c++ variables Variables which  are essential components in C++ programming. A variable that can hold a data when we want to execute a  c++ program. without variable we can not handle a lot of data in any programming.

Dear reader, here i will discuss details about  c++ programming variable  that will be helpful for your learning c++  programming.

Variable:

To store data, different types of variable are used in C++. A variable can store character, integer,

float or string type data. A variable must be declared before using it.

Syntax of declaring a variable in c++:

<data type of variable> <variable name 1,variable name 2,….  ,variable name n>;

     Example:

                     int first_number, second_number;

                     char a;

                     float number;

Also a value can be initialized at the time of declaration of a variable.

          Example:

                     int payment=1000;

Rules of giving name of a variable

1. Variable name consists of letters (a-z and A-Z), digits (0-9) and underscore (_)

                Example: number, digit1, first_name

2. The first character of a variable must be a letter or underscore.

                Example: legal: payment, _details

                            illegal: 3due, 7fine

3. No other characters, spaces or special symbols are not allowed in C++

                 Example: #name, first  name, +.temp are illegal.

4.  Any C++ keyword cannot be a variable.

                 Example:  int, float, main are illegal variable.
5.  As C++ is a case sensitive language so variable name is also case sensitive

                Example: x & X are not same, name and Name are not same variable.

6. Variables must be declared before use it.

Wrong variable declaration Example:

#include <iostream>//for cout,cin etc.
using namespace std;
int main() 
{
cout<< "Value of x is :"<<a<<endl;            //display prompt 

int a=5;                                             //variable name and type integer 

return 0; 
}

Correct variable declaration Example:

#include <iostream>//for cout,cin etc.
using namespace std;
int main()

{

int x=5;                                              //variable name and type integer
cout<<"Value of x is:"<<x<<endl;            //display value of int value x.

return 0;

}

if you want to run the above Two Program then you have to save as Like : “example.cpp” (cpp means c plus plusthen compile & run.

 

Related Post Or  You may Like:

 

knowledgediarybd.com © 2017
shares