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 :
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 :
example :
int ny = 5;
Now if we want to write these two in one single line, we can use comma separated list as shown below :
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.
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..
Code :
int x = 10;
cout << x ;
cout << "x"Output :2
10
x
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
No comments:
Post a Comment