In this c++ programming tutorial we take a look at variables and data types.
Variables
If you declare a variable in c ++ ( later on we will talk about how to do this),you ask the operating system for a piece of memory . you (can) give this pieces of memory a name and you can store something in that piece of memory (for later use ).
The name of a variable is called an identifier .you can give a variable any name you want ,as long as it is a valid identifier .
Valid identifier
a valid identifier is a sequence of one or more letters,digits or underscore and the identifier must begin with a letter.(it is not possible to start an identifier with a number.) It is also not possible to use punctuation marks, symbols and spaces in an identifier .compiler specific keywords or externals identifiers usually begin with an underscore.(it possible to use an underscore at the beginning of your identifier, but it is not recommended.)
The c++ language is a "case sensitive " language .This means that an identifier with capital letters is not the same as with normal letters.
for example: mtrb,Mtrb,mtrB, mTRB are four different variable identifiers.
Basic types of variables
Each variable while declaration must be given a datatype,on which the memory assigned to the variable depends. following are the basic types of variables ,
- bool for variable to store boolean values(true or false)
- char for variables to store character types.
- int for variable with integral values
- float and double are also types for variables with large and floating point values .
Variable must be declared before they are used .usually it is preferred to declare them at the starting of the program,but in c++ the can be declared in the middle of program too ,but must be done before using them .
OR
Declare a variable is very easy .first you have to declare the data type .after the data type you place the name of the variable .but remember choose the name wisely .It is easier if a variable name reflects the use of that variable.
EXAMPLE:
int i; //declared but not initialized
char c;
int i,j,k ; // multiple declaration
initialization means assigning value to an already declared variable,it is possible to declare more than one variable at the same time .
int i; // declaration
i=13; // initialization
Signed and Unsigned variables
The difference between signed and unsigned variable is that signed variables can be either negative or positive but unsigned variables can only be positive .By using an unsigned variable you can increase the maximum positive range .When you declare a variable in the normal way it is automatically a signed variable .
To declare an unsigned variable you just put the word unsigned before your variable declaration or signed for a signed variable although there is no reason to declare a variable as signed since they already are .
why should i use an unsigned integer?
EXAMPLE:
int i; //declared but not initialized
char c;
int i,j,k ; // multiple declaration
initialization means assigning value to an already declared variable,it is possible to declare more than one variable at the same time .
int i; // declaration
i=13; // initialization
Signed and Unsigned variables
The difference between signed and unsigned variable is that signed variables can be either negative or positive but unsigned variables can only be positive .By using an unsigned variable you can increase the maximum positive range .When you declare a variable in the normal way it is automatically a signed variable .
To declare an unsigned variable you just put the word unsigned before your variable declaration or signed for a signed variable although there is no reason to declare a variable as signed since they already are .
why should i use an unsigned integer?
- They are more efficient.
- The signed integer can be negative .
- You get a larger dynamic range.
- You can safely perform shift operations.
- modulus arithmetic is completely defined .
- overflowing an unsigned data type is defined,whereas overflowing a signed integer type could result in world war 4 starting .
When declaring a regular local variable , its value is by default undetermined .It is possible to store a concrete value in a variable at the same moment that the variable is declared .If you do this you are initializing a variable.
This can be done in two ways:
- The first method is the c-like method :int a=0;
- The second method is known as the constructor initialization:int a(0);
EXAMPLE:
#include<iostream>
using namespace std;
int main ()
{
int a=10;
int b(86);
int result;
a=a+4;
result=a-b;
cout<<result;
return 0;
}
Scope of variables
All the variables have their area of functioning , and out of that boundary they don't hold their value, this boundary is called scope of the variable . For most of the cases its between the curly braces, in which variable is declared that a variable exists,not outside it. We will study the storage classes later, but as of now ,we can broadly divide variables into main types ,
- GLOBAL VARIABLES
- LOCAL VARIABLES
Global variables are those , which are once declared and can be used throughout the lifetime of the program by any class or any function . They must be declared outside the main() function . If only declared , they can be assigned different values at different time in program lifetime . But even if they are declared and initialized at the same time outside the main() function , then also they can be assigned any value at any point in the program.
EXAMPLE: only declared , not initialized
#include <iostream>
using namespace std ;
int x; // GLOBAL VARIABLE DECLARED
int main()
{
x=11; // INITIALIZED ONCE
cout<<"first value of x="<<x;
x=21; // INITIALIZED AGAIN
cout<<"initialized again with value ="<<x;
}
LOCAL VARIABLES
Local variables are the variables which exist only between the curly braces , in which its declared, outside that they are unavailable and leads to compile time error .
EXAMPLE:
#include<iostream>
using namespace std ;
int main()
{
int i=12;
if(i<22) //IF CONDITION SCOPE STARTS
{
int n=233; // LOCAL VARIABLE DECLARED AND INITIALIZED
} // IF CONDITION SCOPE ENDS
cout<<n; //COMPILE TIME ERROR, N NOT AVAILABLE HERE
}
SOME SPECIAL TYPES OF VARIABLES
There are also some special keywords , to impart unique characteristics to the variables in the program . following two are mostly used , we will discuss them in detail later.
- FINAL- once initialized, its value cant be changed.
- STATIC- These variables holds their value between function calls.
#include<iostream.h>
using namespace std
int main ()
{
final i=256;
satic y=351;
}
No comments:
Post a Comment