0x03 - Ones and zer0es
Let’s say you want to solve a simple math problem in C++. For example, you have two numbers that you want to add together. What you can do is store the numbers in variables and then use the +
operator to perform the addition.
Now, let’s talk about variables. Just like in math, a variable has a name that can have a value assigned to it. For example: x = 5
. However, because C++ is a strongly typed language, you also need to specify the type of the variable when you declare it. Declaration is a term used to express that we’re creating a new variable. I’ll be using this term throughout this series together with the term initialization, which is just a fancy term for assigning a value to the declared variable. So, if you want to declare a variable and initialize it with an arbitrary value, you first need to pick an appropriate type. C++ has several built-in types, but I’ll describe just the basic ones here:
int
is the basic integer type and can only store whole numbers without decimals (-3, 5, 123)float
can store real numbers with decimals (3.14, -2.0, 123.678)unsigned int
is just likeint
, however, it can’t store negative values (0, 3, 7, 256)bool
can only store two values:true
orfalse
char
is a basic character type and is used to store single characters (‘a’, ‘Z’, ‘.’)
So, let’s create a variable called a
and assign the number 11 to it. First, we have to pick the type of our variable, and since 11 is an integer, int
is a good choice:
int a = 11;
We can also declare the variable without initialization and initialize it later:
int a;
a = 11;
You just have to make sure that the variable gets initialized before it is accessed (when printing it or using it in a math operation, for example). Also, you can see that we only had to specify the type (int
) once when we declared the variable. After the variable is declared, we don’t repeat the type when accessing or altering the value.
We have now declared a variable a
and initialized it with the number 11. So, now we know how to create variables, let’s use the knowledge to do our calculation: (addition.cpp)
#include <cstdio>
int main() {
int a = 11;
int b = 17;
int result = a + b;
std::printf("The result of %d + %d is %d\n", a, b, result);
return 0;
}
(Let’s not worry about the %d
s in the std::printf
function for now, we’ll tackle it in an upcoming chapter)
Compile and run our application (Linux):
g++ addition.cpp -o addition
./addition
For Windows, use the build and run button in your IDE.
You should see: The result of 11 + 17 is 28
output from our application.
Just to prove that we’re indeed dealing with variables and not with constants, we can try changing the value:
int a = 11;
std::printf("Initial value of 'a' is %d\n", a);
a = 3;
std::printf("New value of 'a' is %d\n", a);
This code prints:
Initial value of 'a' is 11
New value of 'a' is 3
Some rules about variable naming:
- You can only use letters, numbers and an underscore (
_
) in a variable name - You can’t start a variable name with a number or an underscore
- You can’t use reserved C++ keywords* as variable names (
int
,char
,return
) - Variable names are case-sensitive (
int a;
is different toint A;
)
Declaration | OK |
---|---|
int a; |
✓ |
bool ok_; |
✓ |
int camelCase; |
✓ |
int PascalCase; |
✓ |
int snake_case; |
✓ |
int SNAKE_UPPER_CASE; |
✓ |
int kebab-case; |
✗ (- not allowed in a variable name) |
int space odyssey; |
✗ (space not allowed in a variable name) |
int 3x; |
✗ (variable name starts with a number) |
char _y; |
✗ (variable name starts with an underscore)* |
* C++ keywords are like a vocabulary of the language. They cannot be redefined and used as identifiers (variable names).
In the next chapter, we will learn more about how printing of variables works and look into how text is interpreted by computers.