Variables, Types and Operators

An introduction to the main built-in types in C++, alongside how to create objects of those types and apply operators to them
This lesson is part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

8a.jpg
Ryan McCombe
Ryan McCombe
Posted

This lesson is a quick introductory tour of variables and operators within C++. It is not intended for those who are entirely new to programming. Rather, the people who may find it useful include:

  • those who have completed our introductory course, but want a quick review
  • those who are already familiar with programming in another language, but new to C++
  • those who have used C++ in the past, but would benefit from a refresher

It summarises several lessons from our introductory course. Anyone looking for more thorough explanations or additional context should consider Chapter 1 of that course.

Previous Course

Intro to Programming with C++

Starting from the fundamentals, become a C++ software engineer, step by step.

Screenshot from Cyberpunk 2077
Screenshot from The Witcher 3: Wild Hunt

Comments

In C++, single line comments start with // and comment blocks extend from /* to */

// This is a single line comment

/*
This is a multiline
comment
*/

Variables and Types

C++ is a strongly typed language, so we always declare the types of our variables. Statements end with a semicolon.

// A boolean - a true or false value
bool MyBoolean;

// An integer such as 42
int MyInteger;

// A floating point number such as 3.14
float MyFloat;

// "Double Precision" floating point numbers
// use extra memory to improve accuracy
double MyDouble;

Variables can be initialized at the same time they are declared, using braces { and }:

int MyInteger{42};

// Single precision float literals have an f suffix
float MyFloat{5.2f};

double MyDouble{5.2};

bool MyBool{true};

Literals are code that directly represents a value. Based on the formatting of the literal, the compiler can implicitly understand the type of the value.

true is a bool literal, 2 is an integer literal; 2.5f is a float literal, and 2.5 is a double literal

Operators and Expressions

Once they’ve been created, variables are updated using the assignment operator =:

int MyInteger{42};
MyInteger = 10;

Operators are characters that perform defined actions on variables and values.

The objects they are operating on are known as operands.

A binary operator has two operands - a left and a right. The = operator used above is an example of this, where the left operand is MyInteger and the right was 10

Some operators only have a single operand - those are called unary operators.

Some symbols can be either a unary or binary operator, depending on how they are used. For example, the unary - as in -x indicates negation (returning the negative value of x) whilst the binary - as in x - y indicates subtraction.

A sequence of operators and operands is called an expression. MyInteger = 10 was an expression. Expressions may return a result, which we can use as the operand of another operator. thereby creating a larger expression. This is what makes code like 1 + 2 + 3 work as we’d expect.

Literals, variables, and the result of expressions can be used interchangeably:

int MyInteger{2};
int AnotherInt{2 + 2};
int IntegerFromVariable{MyInteger};
int IntegerFromExpression{MyInteger + 1};

White Space

C++ is generally insensitive to white space. We can space our code as preferred:

int SomeInt{5};
int AnotherInt { 5 };

bool SomeBool {
  false
};

bool AnotherBool
  { false };

int A{5}; int B{10}; int C{15};

Automatic Type Deduction using the auto Keyword

When providing an initial value, we can ask the compiler to infer the type using the auto keyword automatically.

Note, that auto is not dynamic typing. The type will be fixed once the variable is created:

// Creates a variable of type int
auto MyVariable { 10 };

This should be used sparingly, as visually concealing the types from our code can make it harder to understand and debug.

Type Conversions

C++ does implicit type conversion at compile time where possible:

// 2.5 (double) converted to 2.5f (float)
float MyFloat { 2.5 };

// 2.5 (double) converted to 2 (int)
int MyInteger { 2.5 }; 

// 2 (int) converted to 2.0 (double)
double MyDouble = MyInteger;

// 3 (int) converted to 3.0f (float)
float MyFloat = 1 + 2;

We can explicitly ask the compiler to do conversions using static_cast:

static_cast<int>(2.5); // Returns 2
static_cast<float>(2); // Returns 2.0f

// It's reasonable to use auto here, as we already specify
// the type in the call to static cast
auto MyInt { static_cast<int>(2.5f) };
auto MyFloat { static_cast<float>(MyInt) };

There are other ways to initialize variables, including the = operator, but uniform initialization using { and } is recommended. It includes some safeguards against potential human error.

For example, it will throw a compiler error if we attempt an implicit narrowing conversion which would cause data loss.

// Implicit narrowing conversion allowed
int MyNumber1 = 3.14;

// Implicit narrowing conversion disallowed
int MyNumber2{3.14}; 

// We make it explicit if this was intended
int MyNumber3{static_cast<int>(3.14)};

Numbers

Numbers support all the typical mathematical operators we’re familiar with from other programming languages. Below, we demonstrate this with integers, but the techniques work with any numeric type.

int x { 4 };

// Addition
x = x + 2; // x is now 6

// Subtraction
x = x - 2; // x is now 4

// Multiplication
x = x * 2; // x is now 8

// Division
x = x / 2; // x is now 4

Integer Division and Modulus

// Integer division returns the quotient
5 / 2; // 2

// Modulus is available using % operator
5 % 2; // 1

Order of Operations

C++ operators are subject to an order of operations. This can be manipulated by introducing brackets, ( and )

4 + 2 / 2;    // 5 (4 + 1)
(4 + 2) / 2;  // 3 (6 / 2)

Integer and Floating Point Interaction

Different numeric types generally interact in predictable ways. When either operand of division is a floating point number, the result will also be a floating point number

5 / 2.0; // 2.5
5.0 / 2; // 2.5

Increment and Decrement Operators

The increment and decrement operators (++ and --) are available. They can come before the operand (as a prefix operator) or after the operand (as a postfix operator)

The prefix and postfix operators modify their operands in the same way. However, the prefix operators return the new value of the operand, whilst the postfix operator returns the previous value.

int x { 1 };
int y;

y = ++x; // both x and y are now 2
y = x++; // x is now 3, but y is still 2
int x { 1 };
int y;

y = --x; // both x and y are now 0
y = x--; // x is now -1, but y is still 0

Compound Assignment

A variable can be modified in place using the compound assignment operators, +=, -=. *= and /=.

These are shorthand ways of writing longer expressions. For example, x += 2 is equivalent to x = x + 2

More examples are below:

int x { 4 };
x += 5; // x is now 9
x -= 5; // x is now 4
x *= 2; // x is now 8
x /= 2; // x is now 4

Floating Point Shorthand

When the decimal component of a floating point number is 0, it can be omitted

float MyFloat { 2.f }; // 2.0f
double MyDouble { 2. }; // 2.0

Thousands Separator

The ' character can be added to numbers for readability. Typically, this is used as a thousands separator for large numbers:

int ABigInteger { 1'000'000'000 };
float ABigFloat { 2'000'000'000.f };

Booleans

Booleans in C++ have the bool type, and their literal values are either true or false

bool Bool1 { true };
bool Bool2 { false };

A boolean value in C++ can be generated in the same way as almost every other modern programming language. We have the 6 usual comparison operators:

  • == returns true if the two operands are equal
  • != returns true if the two operands are not equal
  • > returns true if the left operand is larger than the right operand
  • >= returns true if the left operand is larger than or equal to the right operand
  • < returns true if the left operand is smaller than the right operand
  • <= returns true if the left operand is smaller than or equal to the right operand
bool MyBool { false };

// The equality operator: ==
MyBool = 5 == 5; // true

// The inequality operator: !=
MyBool = 5 != 5; // false

// The greater than operator: >
MyBool = 5 > 2; // true
MyBool = 5 > 5; // false

// The greater than or equal to operator: >=
MyBool = 5 >= 5; // true

// The less than operator: <
MyBool = 5 < 2; // false
MyBool = 5 < 5; // false

// The less than or equal to operator: <=
MyBool = 5 <= 5; // true;

Combining Booleans

Booleans can be combined using the logical and operator, && and the logical or operator, ||

// The logical and operator: &&
true && true; // true
true && false; // false
5 > 2 && 5 > 10; // false

// The logical or operator: ||
true || true; // true
true || false; // true
5 > 2 || 10 > 5; // true

Brackets

Similar to other operators, && and || are subject to an order of operations. We can introduce brackets to control this:

MyBool = (true && false) || true; // false
MyBool = true && (false || true); // true

Boolean Inversion

Boolean expressions can be inverted using the ! operator

!true; // false
!false; // true
!(5 < 2); // true
!(5 > 2 || 10 > 5); // false

Boolean Implicit Conversion

Numeric types can be implicitly cast to booleans. 0 is falsey; anything else is truthy

A “falsey” object is something that can be converted to a boolean, and will have a value of false

A “truthy” object is something that can be converted to a boolean, and will have a value of true

bool MyBool { 0 }; // false
bool MyBool { 1 }; // true

Similarly, the opposite is true, although less useful. Boolean true is implicitly cast to numeric 1, whilst false is cast to 0

int MyInt { true }; // 1
float MyFloat { false }; // 0.f

Was this lesson useful?

Edit History

  • — First Published

Ryan McCombe
Ryan McCombe
Posted
This lesson is part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Whirlwind Tour of C++ Basics
7a.jpg
This lesson is part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access!

This course includes:

  • 106 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Next Lesson

Functions and Scope

A quick guide to functions in C++, and how global and local scopes interact
a.jpg
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved