C++ Variables

C++ Variables

Variables are like containers for storing data values. In C++, there are different types of variables based on what type of data they hold.

Declaring Variables

To create a variable, you must specify the type and assign it a value: type variableName = value;

Here are common variable types:

Example Usage

Example

#include <iostream>
#include <string>
using namespace std;

int main() { int myNum = 15; double myFloatNum = 5.99; char myLetter = 'D'; string myText = "Hello"; bool myBoolean = true;

cout << myNum << "\n"; cout << myText << "\n"; return 0; }

Variable names must start with a letter or an underscore, and they are case-sensitive.


Exercise

?

Which data type is used to store floating point numbers with decimals?