Wednesday, October 20, 2010

Data types and variable in C++


Now that you have had an introduction to the origin and history of C++, we'll start our introduction to C++ variables and data types supported by C++ language. Many IDEs are used for C++ language like TurboC, Developer C++, VSC++ etc. You will have to download one of these in order to start practicing C++.
C++ supports many data types including
Character data type = char, takes 1 byte
Boolean data type = bool, takes 1 byte
Short data type = short int or short, takes 2 bytes
Integer Data type = int, takes 4 bytes
Long Data type = long int or long, takes 4 bytes
Floating data type= float, takes 4 bytes
Double data type = double, takes 8 bytes
Long double data type = long double, takes 8 bytes
Declaration of Variables
Declaring a variable means that you are telling your compiler to reserve a place for your variable in memory, so whenever you declare a variable, you compiler reserves number of bytes required by the data type of that variable in memory.
For example
float myvariable;
is a statement that will allocate a memory of 4 bytes to myvariable.
C++ compiler works statement by statement and a statement in C++ always ends with a semi-colon. If you forget to put semi colon you will not be able to go past compiler and an error will be issued.
Definition of a variable
After the declaration you will need to define a value for the declared variable.
For example
int myvar = 3;
This statement also called the assignment statement, basically defines a value for the variable myvar.
Multiple Definitions and Declarations
Multiple variables can be defined and declared at the same time. For example
int a,b,c;
float y=1.9, t=0.9;
So this is just the beginning, there is a lot more to come. So stay tunedJ




 

No comments:

Post a Comment

Please comment, if you like the post, you find any mistake or if you have any query.