Dec 10, 2011

DataTypes & Variables

Main DATA TYPES in C++:-


When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way.



The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255). In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers.

Next you have a summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one:


NameDescriptionSize*Range*
char Character or small integer. 1byte signed: -128 to 127
unsigned: 0 to 255
short int (short) Short Integer. 2bytes signed: -32768 to 32767
unsigned: 0 to 65535
int Integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool Boolean value. It can take one of two values: true or false. 1byte true or false
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
long double Long double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t Wide character. 2 or 4 bytes 1 wide character

* The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems. But for other systems, the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size. The same applies to the floating point types float, double and long double, where each one must provide at least as much precision as the preceding one.

Declaration of Variables

To use this Variable we need to declare Variable With Data Type. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float...) followed by a valid variable identifier. For example: 

To define Integer  :  int az1;

To Define Float : float az2;

To Define Character : char az3;

Above az1,az2,az3 is a variable name, we cannot use same name again and int,float & char is a data types. 

You  will know more about data types with time.


SCOPE of Variables

Look out Example Program for better understand

#include<iostream.h>

int a = 20;  // Its Global Variable and have scope within a file and can be used in whole program.

int main()

{

 int a = 30; /* Its Local Variable & have scope within this main function can be used in this function only*/

cout << a; // it will print 30

cout << ::a; //it will print 20   :: (double colon) used to access global variable

 //  There is more scope that i will tell you when classes and functions are going to start...

return 0;

}


Initialization of variables

When declaring a regular local variable, its value is by default undetermined. But you may want a variable to store a concrete value at the same moment that it is declared. In order to do that, you can initialize the variable

To initialize variable -

datatype variable_name = initial_value ;


For example, if we want to declare an int variable called az initialized with a value of 0 at the moment in which it is declared, we could write:

int az = 0; 

Declared constants (const)

 When constants variable is declare then we cannot change its value if we try to change compiler give error to this statement. To Declare Constant :-

const datatype variable_name = initial_value;

const int az = 10;



0 comments:

Post a Comment