Variables in C++ and its use in DEV C++

What are Variables in C++

Variables:
It may be letters or digits but must starts with either a letter or underscore.As there is a case sensitive issue in c++, so upper and lower case letters are case sensitive.
Different variables have different memory sizes depending upon the type of variable and because of this different operations applied on different variables.
Basic types of variables are



Type
Description
bool
Stores true or false value
int
Integer
char
character
float
Single precision floating point value

other types are double , void etc.

Initialization:

type variable_name;

type: any valid c++ data type
variable_name: any valid c++ identifier
semicolon:    followed by semicolon ( ; )


Examples:

int x ;
char y ;
float z ;
double t ;

Assigning values to Variables:
Values can also be assigned during initialization.Just after variable name use equal sign (=) and then assigns the value what you want like integer value or char value or something else valid in C++.

Examples:

int x = 2 ;
char y = ' z ' ;


Lets do example in broader way. I am going to define variables  of different data types then accordingly i will perform operations on it. It is easy.
Program code:

#include<iostream>
#include<conio.h>
using namespace std;


int main()
{
int  a ,b ,x;       //  it defines three integer variables a,b,x
       char y =  'Z'  ;     // character variable y stored character Z
    
       float z;                // floating variable z
    
    
    a = 100 ;
    b = 200 ;
    x = a + b;
    
    z = 100.0 / 3.0 ;
     
    cout<< "integer value   =  ";
    cout<< x <<endl<<endl;
        
    cout<< "Character value =  ";
    cout<< y <<endl<<endl;
    
cout<< "Float value     =  ";
    cout<< z <<endl<<endl;
    
  
    getch();

}

Output:

Click to enlarge it.


Keep practicing on it.



No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.