Literals

Consider the following two statements:

1 2 |
std::cout << "Hello world!"; int x{ 5 }; |

What are *“Hello world!”* and *5*? They are literals. A literal (also known as a literal constant) is a fixed value that has been inserted directly into the source code.

Literals and variables both have a value (and a type). However, the value of a literal is fixed and can’t be changed (hence it being called a constant), whereas the value of a variable can be changed through initialization and assignment.

Operators

In mathematics, an operation is a mathematical calculation involving zero or more input values (called operands) that produces a new value (called an output value). The specific operation to be performed is denoted by a construct (typically a symbol or pair of symbols) called an operator.

For example, as children we all learn that *2 + 3* equals *5*. In this case, the literals *2* and *3* are the operands, and the symbol *+* is the operator that tells us to apply mathematical addition on the operands to produce the new value *5*.

Author's note

For reasons that will become clear when we discuss operators in more detail, for operators that are symbols, it is common nomenclature to append the operator’s symbol to the word *operator*.

For example, the plus operator would be called *operator+*, and the extraction operator would be called *operator>>*.

You are likely already quite familiar with standard arithmetic operators from common usage in mathematics, including addition (+), subtraction (-), multiplication (*), and division (/). In C++, assignment (=) is an operator as well, as are << (insertion) and >> (extraction). Some operators use more than one symbol, such as the equality operator (==), which allows us to compare two values to see if they are equal. There are also a number of operators that are words (e.g. new, delete, and throw).

The number of operands that an operator takes as input is called the operator’s *arity* (almost nobody knows what this word means, so don’t drop it in a conversation and expect anybody to have any idea what you’re talking about). Operators in C++ come in three different *arities*:

**Unary** operators act on one operand. An example of a unary operator is the *- operator*. For example, given `-5`

, *operator-* takes literal operand *5* and flip its sign to produce new output value *-5*.

**Binary** operators act on two operands (known as *left* and *right*). An example of a binary operator is the *+ operator*. For example, given `3 + 4`

, *operator+* takes the left operand (3) and the right operand (4) and applies mathematical addition to produce new output value *7*. The insertion (<<) and extraction (>>) operators are binary operators, taking std::cout or std::cin on the left side, and the item to output or variable to input to on the right side.

**Ternary** operators act on three operands. There is only one of these in C++, which we’ll cover later.

Note that some operators have more than one meaning depending on how they are used. For example, *operator-* has two contexts. It can be used in unary form to invert a number’s sign (e.g. to convert 5 to -5, or vice versa), or it can be used in binary form to do subtraction (e.g. 4 - 3).

Chaining operators

Operators can be chained together such that the output of one operator can be used as the input for another operator. For example, given the following: *2 * 3 + 4*, the multiplication operator goes first, and converts left operand *2* and right operand *3* into new value *6* (which becomes the left operand for the plus operator). Next, the plus operator executes, and converts left operand *6* and right operand *4* into new value 10.

We’ll talk more about the order in which operators execute when we do a deep dive into the topic of operators. For now, it’s enough to know that the arithmetic operators execute in the same order as they do in standard mathematics: Parenthesis first, then Exponents, then Multiplication & Division, then Addition & Subtraction. This ordering is sometimes abbreviated *PEMDAS*, or the mnemonic “Please Excuse My Dear Aunt Sally”.

Quiz time

Question #1

For each of the following, indicate what output they produce:

a)

1 |
std::cout << 3 + 4; |

b)

1 |
std::cout << 3 + 4 - 5; |

c)

1 |
std::cout << 2 + 3 * 4; |

1.9 -- Introduction to expressions |

Index |

1.7 -- Keywords and naming identifiers |

Hi, I think there's a typo; "Consider the following two statement: ". There should be an s on the end of statement.

what is operand?

[CODE]

1 + 2

[CODE]

1 and 2 are operands

+ is an operator

How can you identify the difference between literals and variables?

I mean isn't a literal printed out the exact same way as a variable?

How do we know the difference. You wrote here...

"Literals and variables both have a value (and a type). However, the value of a literal is fixed and can’t be changed (hence - called a constant), whereas the value of a variable can be changed through initialization and assignment."

std::cout << "Hello world!";

int x{ 5 }; // you said “Hello world!” and 5 are literals.

OKAY... you're saying "5" is a literal even though it has underwent the procedure of initialization by using { }.

The "hello world" text, I understand can be viewed as both a literal and variable(data)

but the - 'int x {5}' - I don't understand.

Is identifying the difference between literal and variable/ data important? As I'm struggling to understand this topic.

A literal is the value you write down in your code. 5 and "Hello world!" are literals, what you do with them doesn't matter. `x` is a variable.

If you don't understand it, you'll probably get the hang of it as you go along.

Thank you so much for this, man. It's really helpful and easy to understand

Does this not turn 5 into a variable?? int x { 5 }

I can change the value of x after this which means it's not a constant right?

It sets the value of @x to 5. You can change to value of @x, but you can never change 5.

dude i really love you ,this is like a online c++ bible for me and its free and really easy to understand ,god bless you. I am a student and i dont earn ,but this is helping me alot. = ) thanks again!

Hi,

"For example, the plus operator would be called operator+, and the extraction operator would be called operator<<." Should be operator>> !?

Fixed. Thanks!

This is sooooo useful Thanks SOOO much whoever made this <3

You're welcome. Thanks for visiting.

It's PEMDAS, not PEDMAS according to https://en.wikipedia.org/wiki/Order_of_operations

Acronym fixed! Thanks for pointing out the omission.

The acronym works both ways, and I've heard PEDMAS myself. I think it might be a USA vs UK thing?

Yeah, I learned PEDMAS, but I think PEMDAS is more common and better because "Please Excuse My Dear Aunt Sally" is an easy mnemonic.

PEDMAS, PEMDAS and BODMAS all work. Multiplication & division are actually done in the same order, similar to addition and subtraction.