Variables

Declare Variables in C++

Every variable in C++ must be declared before it can be used. It means that, we should provide a type and a name for the variable. The type tells your compiler how much memory to set aside for the variable, and it defines what you can do with the variable.

C++ is a strongly typed language.

Datatype Modifiers

Datatype modifiers are used with built-in data types to modify the length of data that a particular data type can hold. Data type modifiers in C++ are:

  • signed

  • unsigned

  • short

  • long

Const

const (constant) variables cannot be changed by your program during execution.

const double quarter = 0.25; // and now variable quarter can only be 0.2c

Simply add the keyword const before the data type during declaration to make the variable not modifiable.

Type Conversion

A type cast is basically a conversion from one type to another.

The notation (type) value means “convert value to type“. So for example:

Note: Going from a double to an int simply removes the decimal. There’s no rounding involved.

Last updated