Skip to main content

Section 3.1 Values and Literals

A value is one of the fundamental things—like a letter or a number—that a program manipulates. A literal value is a value that is written directly in the code instead of being calculated in some way.
The only values we have manipulated so far are the string literal values we have been outputting, like “Hello, world.”. You (and the compiler) can identify string literal values because they are enclosed in quotation marks.
But there many different kinds of values other than strings. Another basic type of value are whole numbers, like 1 or 17. These are called integers. We can write integer literal values as numbers without quotation marks. To print the number 17 in C++ we could write:
cout << 17 << endl;
The kind of thing a value is known as its type. The type of 17 is “integer” (or int for short). The type of "Hello, world." is “string”. A value’s type determines what you can do with it. For example, you can multiply two integers together, but you cannot multiply two strings (what would "Hello" * "World" even mean???).
It is easy to confuse different types of values, like "5" and 5, but if you pay attention to the punctuation, it should be clear that the first is a string and the second is an integer. Multiplying 5 by 2 makes sense, but multiplying "5" by 2 does not. It may look like a number, but it is actually a string.

Checkpoint 3.1.1.

Checkpoint 3.1.2.

You have attempted of activities on this page.