Wednesday, February 15, 2017

Getting good at: C++ | #9 - Constants and typedef

#9 - typedef long post

What's goin on everyone, this is FitMC. Today, we're going to learn about constants. Constants are essentially read-only variables, meaning that once you define them, you can't change them later in the program without modifying the value in the constant. You'll see what I mean later on.

There are a couple ways you can define a constant. 

1. Type #define CONSTANTNAME 1, where CONSTANTNAME is the name of your constant, and 1 is the value. You do not have to capitalize your constant name when defining it like this, but I recommend that you do for the sake of readability.

2. In any function, type const int net = 100;. The constant doesn't always have to be an integer. For instance, something like this will work: 
const char when_i_say_go[] ={'b','e','r','e','a','d','y','t','o','t','h','r','o','w'};

^This is not on a different line. It should look something like this:

This may look confusing. You don't have to know this yet, but keep it in mind. 


Now, we're going to look at a statement called typedef. This statement allows you to substitute keywords like int, char, float, double, etc for other words. For example, you could write something like this:

typedef int i;

The int keyword is being substituted by the letter 'i', and everywhere else in the program you can use the letter 'i' to fill in for the keyword int. You don't have to use a single letter to fill in for a keyword. If you really want to, you could write something like this:

You may be wondering why this is useful. In the next post, you'll see why. That's all I have for now. Until then...