C++ Variables:
- In C++, a variable is a named location in memory.
- Variable is used to store data of a particular type.
- A variable has a name, a data type, and a value.
Rules to create:
- Variable names must begin with a letter or an underscore and can be followed by letters, numbers, or underscores.
- C++ is case-sensitive, so “myVar” and “myvar” are two different variable names.
Integer variable:
#include <iostream> using namespace std; int main() { int num = 10; // integer variable cout << “The value of num is: ” << num << endl; return 0; } |
Floating point variable:
#include <iostream> using namespace std; int main() { float num = 3.14; // floating point variable cout << “The value of num is: ” << num << endl; return 0; } |
Character variable:
#include <iostream> using namespace std; int main() { char ch = ‘a’; // character variable cout << “The value of ch is: ” << ch << endl; return 0; } |
Boolean variable:
#include <iostream> using namespace std; int main() { bool isTrue = true; // boolean variable cout << “The value of isTrue is: ” << isTrue << endl; return 0; } |