- During programming we need to store data.
- We store this data into place holders called variables.
- Variables are location in memory for storing data.
- In memory, there is a numerical address for each location of memory (block).
- We give names to these locations and call them variable.
- They are called variables because they contain different values at different times.
- The variables in C may be started with a character or an underscore (_).Variables have to be declare and defined.Every variable has:
- —Name
- —Type
- —Size
- —Value
Declaration of variables:
Values in variables:
Examples:
- In order to use a variable in C++, we must first declare it specifying which data type we want it to be.
- The syntax to declare a new variable is to write the specifier of the desired data type.
datatype <variable name>
e.g int x; double y; int x,y,z;
- The variable having a name will need some value in it.
- To put some value in these boxes is known as assigning value to variables.
- In C++ language, we use assignment operator for this purpose
Values in variables:
—Assignment operator is a binary operator that has two operands.
—It must have a variable on left hand side and expression (that evaluates a single value) on right hand side.
—This operator takes the value on right hand side and stores it to the location labeled as the variable on left hand side.
X=0;
X=0;
Z = x +4
x +4 = Z Wrong
Caution !!!!!!!
Don’t confuse (=) with the equal sign in Algebra …….NO
Examples:
2) int 4x = 16;
3) bool @myBool;
4) bool myBool;
5) char char = 'B';
6) char _char = 'B';
7) double my-num = 4;
8) double my_num = 4;
9) int iAmNot4giving = 1231;
Now
(1) valid
(2) not valid (doesn't start with a letter or an underscore
(3) not valid for the same as (2)
(4) valid
(5) not valid because char is a reserved word
(6) valid because _char is not the same as char
(7) not valid because there is a dash within the variable name
(8) valid
(9) valid
If you wish to declare a variable as constant (unchangable) put the const keyword in front of your variable declaration. This is a good idea when you want to ensure that you will not change something, even accidently. Also, you can declare global constants by using the #define keyword at the top of your program beneath the #include lines. When defining these constants you must initialize them with a value but the = sign is not needed. Also, these lines do not have semi-colons after them because they are in fact pre-processor directives, which means when your program is compiled, it finds all occurences of the constant in your code and replaces them with the number. It is an accepted rule to keep all your constants as upper case and your normal variables as mostly lower case.
#define PI 3.141592564
// A simple demonstration of variable usage
#include <iostream.h>
#include <math.h>
#define PI 3.14159
#define NAME "bob"
int main(void) {
const int hey = 3;
int x = 0;
char myChar = 'A';
char myChar2 = 67;
double blargh44 = 5.3243;
int a = 2;
int b = 3;
int result = 0;
cout << PI << endl;
cout << NAME << endl;
cout << hey << endl << endl;
// hey = 4 // this would generate an error because hey is a // constant
// PI = 4.1 // this would also cause an error
cout << "The value of x after initialization: " << x << endl;
x = 42;
cout << "The value of x after assignment: " << x << endl << endl;
cout << myChar << endl;
cout << myChar2 << endl << endl;
cout << blargh44 << "\t" << blargh44 / 2 << endl;
blargh44 = blargh44 + 4;
cout << blargh44 << endl << endl;
result = (int)pow(a, b);
cout << a << " to the power of " << b << " is: " << result;
cout << endl << endl;
cout << b << endl;
b += 3;
cout << b << endl;
b--;
cout << b << endl;
return 0;
}
output
------
3.14159
bob
3
The value of x after initialization: 0
The value of x after assignment: 42
A
C
5.3243 2.66215
9.3243
2 to the power of 3 is: 8
*
#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.
No comments:
Post a Comment