Lesson 1 of 8
In Python you write x = 5 and move on. In C++ you must say what kind of thing x is:
int x = 5;
That extra word is doing real work, and this lesson is about what.
Why C++ makes you choose
A type answers two questions the machine needs answered: how many bytes does this occupy, and what do those bytes mean.
The same 32 bits can be the number 1,078,530,011 or the number 3.14159, depending entirely on whether you agreed to read them as an integer or as a floating-point value. There is no marker in memory saying which. The type is that agreement, and in C++ it's fixed when you compile.
That's the trade. Python carries the type around with every value at run time, which is flexible and costs memory and speed. C++ decides at compile time, which is rigid and costs you nothing at run time — and it lets the compiler catch a whole category of mistakes before your program ever starts.
The four you'll use constantly
#include <iostream>
int main() {
int count = 42;
double price = 19.99;
char grade = 'A';
bool is_ready = true;
std::cout << count << "\n";
std::cout << price << "\n";
std::cout << grade << "\n";
std::cout << is_ready << "\n";
return 0;
}
42
19.99
A
1
int— a whole number, positive or negative. Your default for counting anything.double— a number with a fractional part. "Double" refers to double precision; there's alsofloat, which is half the size and half the precision. Preferdoubleunless you have a measured reason not to.char— a single character, written in single quotes.'A', not"A"— lesson 3.5 is entirely about that distinction.bool—trueorfalse.
That 1 is not a bug
Look at the last line of output. is_ready was true and printed 1.
By default C++ prints booleans as integers, because that's what they fundamentally are: false
is 0, true is 1. To get words instead, ask for them:
#include <iostream>
int main() {
bool is_ready = true;
bool is_done = false;
std::cout << is_ready << " " << is_done << "\n";
std::cout << std::boolalpha;
std::cout << is_ready << " " << is_done << "\n";
return 0;
}
1 0
true false
std::boolalpha is a stream manipulator — it changes how the stream formats things from that
point on, rather than printing anything itself. std::endl from lesson 2.3 was one too.
How big is each one?
sizeof tells you how many bytes a type occupies:
#include <iostream>
int main() {
std::cout << "char: " << sizeof(char) << "\n";
std::cout << "bool: " << sizeof(bool) << "\n";
std::cout << "int: " << sizeof(int) << "\n";
std::cout << "double: " << sizeof(double) << "\n";
return 0;
}
char: 1
bool: 1
int: 4
double: 8
Those numbers are not guaranteed. The C++ standard specifies only minimums and relative
ordering — it does not promise int is 4 bytes. On the desktop and server platforms you'll use
in this course, the values above are what you'll see; on an embedded microcontroller, int may
well be 2 bytes.
sizeof(char) is the one exception: it is defined as 1, always. Everything else is measured in
units of char.
If you need a guaranteed size, C++ gives you types that promise one. That's lesson 3.2.
Initialize your variables
This is legal C++, it compiles, and it is a bug:
int count; // no value given
std::cout << count << "\n"; // reading it anyway
Note that it compiles — this is a warning, not an error. With -Wall -Wextra g++ says
warning: 'count' is used uninitialized, which is exactly why you turn those flags on. But the
compiler can only catch the obvious cases; in more complex code it often can't tell, and stays
silent.
An uninitialized local variable contains whatever bytes were already at that memory address. Not zero. Not random in any useful sense. Just leftovers from whatever used that memory before.
Reading one is undefined behaviour (lesson 8.7), and it produces the worst kind of bug: works on your machine, fails on someone else's, changes when you add an unrelated line.
Always give a variable a value when you declare it.
Try it
Declare one variable of each of the four types describing a product in a shop — a name
character code, a quantity, a price, and whether it's in stock. Print them all, with booleans
as true/false rather than 1/0.
Checkpoint
- A type says how many bytes a value takes and how to interpret them; C++ fixes it at compile time.
intfor whole numbers,doublefor fractional,charfor one character,boolfor true/false.- Booleans print as
1/0unless you sendstd::boolalphato the stream. sizeofreports bytes; onlysizeof(char) == 1is guaranteed by the standard.- Never declare a variable without initializing it.
Questions about this lesson
Log in to ask a question.