One of the
important think of C++ is variables. Variables are simply placeholders. It
represents storage location in your computer memory. The variable concept of
mathematics and C++ are almost the same. They just hold some terms or figures
in your computer memory and use that terms or figures afterwards.
In C++, in
order to make variables you need to write the type of that variable (data
types). The summery of fundamental data types are as follows:
Name
|
Description
|
Size*
|
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
|
Source: http://www.cplusplus.com/doc/tutorial/variables/
Syntax of
variable:
#include
<iostream>
using namespace std;
int main()
{
int x = 15;
cout << x << endl;
return 0;
}
Here, int is the data type and x is
the variable. Write
cout << x << endl; for printing on the screen.
Then press Ctrl + F5 for debugging.
Summation program:
#include
<iostream>
using namespace std;
int main()
{
int x = 15;
int y = 26;
int z ;
z = x + y;
cout <<x << " +
"<<y <<" = "<< z;
return 0 ;
}
Output:
N.B: int data type is only for non-decimal numbers. You can also use
other data types for example, you can use double instead of int for decimal figures.
User input:
A user can also input something with his keyboard, which will be
processed by the compiler. To take inputs from the user, simply type
cin>> between the
{------} curly brackets.
For example, in summation program,
to take inputs from the user just write this syntax.
#include
<iostream>
using namespace std;
int main()
{
int x;
int y;
int z;
cin >> x;
cin >> y;
z = x + y;
cout <<x << " +
"<<y <<" = "<< z;
return 0 ;
}
And run that program and the output will be…….
If you have Microsoft Visual C++ or any other compiler installed in your
computer, you can just check the above programs.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.