Upcoming
hi I'Welcome To Blog Mag Site . Your Link Here

Friday, May 10, 2013

Variable, Constants and Datatypes in C++

So lets understand what are variables, constants and data-types in c++. To understand variables and constants we can relate them with a box and a object. A box can be considered as a variable while a object can be considered as constant. So basically we store the objects(constants) inside the boxes(variables). This is the perfect analogy i can come up with. So lets understand this practically. You used to have a subject called algebra in your school right? In algebra you used to perform certain operations like :
Here we are storing value of 1o inside variable x, value of 5 inside y and sum of x and y is passed as a value to variable z. So here X, Y, Z are the variables. While 5, 10, 15 are constants.
Constants
In C++, the types of C++ constants are divided into Primary Constants and Secondary constants. Primary constants consist of Integer Constants, Real Constants and Character Constants. While secondary constants consist of Array, Pointer, Structure, Union, etc.
Data-Types
Since there are different types of constants, there is a need for difference types of variables to handle different types of constants. These variable data-types are called as Data-types in C++. The following table shows the same :

As you see in the above picture, Data-types in c++ are classified into three categories, namely user-defined types, built-in types and derived types. Where user defined types are the one which are defined by us (like structure, union, class, enumeration). And derived types are the ones which are derived from existing data types (like array, pointer, reference).
The built in data-type is again classified into three categories, which are integer (int and char), void and floating type(float and double). These are the pre defined data types inside c++ programming language.

 int can only accept integer type of value
char store only one character
float can store real constant which contain decimal
double is same as float just the range is wider
void means <null> or nothing or as good as empty;   

Declaring and Creating Variables
Now you have a idea of what are variables, constants and data-types. So now we are ready to learn how to declare and create variables in c++ programming language.
The syntax to declare variable is given below :

example :

We can even write these two lines as a comma separated list as follows :

This is the way to declare variables. Now to create variables we have to assign values to variables which are already declared before. The syntax for creating variable is :

example :

1 x = 10;
2 y = 5;
Up till now we declared x and y variables. Then we created these variables in memory by assigning values to the created variables.  Instead of taking many lines, we can write these lines in short by using short-hand property. The syntax to declare and create variables in one line is given below :

 1. datatype variable-name = value ;
example :

int x = 10;
int ny = 5;
Now if we want to write these two in one single line, we can use comma separated list as shown below :

 int x = 10, y = 5;
Ok so now we know how to declare and create variables but there are certain rules to construct variable names. Lets see what are these rules :
  • The first character in a variable name must be an alphabet or an underscore.
  • No commas or blank spaces are allowed within a variable name.
  • No special symbol other than an underscore ( _ ) can be used in a variable name.
Variables can be even dynamically initialized . That means we can do some operation on the right hand side and assign to left hand side. Lets see what it actually means in the examples below :


1 int z = x + y ; // addition is operated on right hand side and stored to z
2 int a = x / y ; // similarly, division takes place here
3int b = x * y ;  // and multiplication takes place here..
Lets learn how to display value of variables in the output. The concept is that if we pass variable to console out ( cout ) by using insertion operator ( << ) then the value contained within that variable will be displayed as the output. But if we pass a variable in quotation marks ( ” ” )  to console out ( cout ) by using insertion operator ( << ) then that variable itself is displayed on the screen. Lets understand the following example :
Code :


int x = 10;
cout << x ;
cout << "x"Output :

 1
2
10


Putting it all together
Now grouping together all the points we learned, lets write a simple program as an exercise for this tutorial. So the program below is to demonstrate creation of variables and displaying variables as output.
Code :


1 #include <iostream.h>
2 #include <conio.h>
3 void main ()
4 {
5 clrscr();
6 int x = 10, y = 5;
7 int z = x + y ;
8 cout << " value of x = " << x ;
9 cout << " value of y = " << y ;
10 cout << " value of z = x+ y = " << z ;
11 getch();
12 }

Output:


1. value of x = 10
2value of y = 5
3. . value of z = x + y = 15
thats all dont be lost of hope you ganna do this more practice we good you wen |

No comments:

Post a Comment

Sponsor Ad

Related Posts Plugin for WordPress, Blogger...
Thanks To: Best Themes | New WP Themes | Best Blogger Themes
Copyright © 2013. Elvin jhon Campilan - All Rights Reserved
Template Design by Campilan | Published by New Blog Themes
Powered by Blogger
Blogger Widgets