C++ Character Data Types

Character Types

The char data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘c’:

Example

char myGrade = 'B';
cout << myGrade;

Alternatively, if you are familiar with ASCII, you can use ASCII values to display certain characters: Continue reading C++ Character Data Types

C++ Boolean Data Types

Boolean Types

A boolean data type is declared with the bool keyword and can only take the values true or false.

When the value is returned, true = 1 and false = 0.

Example

bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun;  // Outputs 1 (true)
cout << isFishTasty;  // Outputs 0 (false)

Continue reading C++ Boolean Data Types

C++ Numeric Data Types

Numeric Types

Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you need a floating point number (with decimals), like 9.99 or 3.14515. Continue reading C++ Numeric Data Types

C++ Data Types

C++ Data Types

As explained in the Variables article , a variable in C++ must be a specified data type:

Example

int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99;     // Floating point number
double myDoubleNum = 9.98;   // Floating point number
char myLetter = 'D';         // Character
bool myBoolean = true;       // Boolean
string myText = "Hello";     // String

Continue reading C++ Data Types

C++ User Input

C++ User Input

You have already learned that cout is used to output (print) values. Now we will use cin to get user input.

cin is a predefined variable that reads data from the keyboard with the extraction operator (>>).

In the following example, the user can input a number, which is stored in the variable x. Then we print the value of x: Continue reading C++ User Input

C++ Variables Examples

Real-Life Examples

Let’s get a bit more practical!

Often in our examples, we simplify variable names to match their data type (myInt or myNum for int types, myChar for char types, and so on). This is done to avoid confusion.

However, for a practical example of using variables, we have created a program that stores different data about a college student: Continue reading C++ Variables Examples

C++ Constants

Constants

When you do not want others (or yourself) to change existing variable values, use the const keyword (this will declare the variable as “constant”, which means unchangeable and read-only):

Example

const int myNum = 15;  // myNum will always be 15
myNum = 10;  // error: assignment of read-only variable 'myNum'

You should always declare the variable as constant when you have values that are unlikely to change: Continue reading C++ Constants

C++ Identifiers

C++ Identifiers

All C++ variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names in order to create understandable and maintainable code: Continue reading C++ Identifiers

C++ Declare Multiple Variables

Declare Many Variables

To declare more than one variable of the same type, use a comma-separated list:

Example

int x = 5, y = 6, z = 50;
cout << x + y + z;

Continue reading C++ Declare Multiple Variables

C++ Variables

C++ Variables

Variables are containers for storing data values.

In C++, there are different types of variables (defined with different keywords), for example:

  • int – stores integers (whole numbers), without decimals, such as 123 or -123
  • double – stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char – stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes
  • string – stores text, such as “Hello World”. String values are surrounded by double quotes
  • bool – stores values with two states: true or false

Continue reading C++ Variables