Ryan's Code Projects

Blog and showcase of Ryan Pridgeon

Part 2: Basic Variables

Hello, and welcome to the 2nd tutorial.
Last time, we learnt how to make a project and print some text onto the screen. In
this tutorial, we are going to learn about variables.
Like in maths, a variable is a value that can change. We use these all the time in
programming to store numbers and data in our programs.
For example, say we were making a game, and we wanted to store how many points the
player has.
We would make a variable, lets call it ‘x’.
At the start of the game, the player would have no points. So we would set x to 0
x = 0
When a player shoots an enemy, we would want him to gain a point. For this we would
have to increase x by one.
x = x + 1
Hopefully this explains the importance and use of variables to you.
In C, we have to declare the variable by name to tell the computer it’s there. We
declare a variable like so;
type name;
Where type is the type of the variable stored. Common types are int (integer
number), float (floating point, can store up to 7 significant figures), char (a
character) and double (a float with twice the precision).
The name is what we want to call the variable. In maths, we commonly name a variable
‘x’, so let’s use that as an example.
This is how you would declare a integer called x
int x;
Now, to set x to a value, we use the ‘=’ assignment operator. If we wanted x to
contain 5, we would do this;
x = 5;
To write an integer onto the screen, we use printf like so;
printf(“%i”, x);
Where %i says we’re printing an integer, and x is the name of the variable we’re
printing.
Consider the following program;

#include <stdio.h>
int main(){
int alpha;
int beta;
int gamma;
alpha = 3;
beta = 2;
gamma = alpha + beta;
printf(“%i”, gamma);
return 0;
}
Can you guess what is printed onto the screen? The answer is 5. Let’s go over this
code in more detail.
At the beginning of our main function, we declare 3 variables
int alpha;
int beta;
int gamma;
Their names are alpha, beta, and gamma, and they are all integers. This declaration
allows them to be used to hold integer numbers in our program.
Next, we assign numbers to alpha and beta.
alpha = 3;
beta = 2;
This puts the number 3 into alpha, and the number 2 into beta. Now alpha holds the
number 3 in memory, and beta holds the number 2.
Our next line does something more interesting. We now use the + operator to assign a
value to gamma.
gamma = alpha + beta;
This line tells gamma to hold the value held by alpha, ADDED TO the value held by
beta.
In our program, alpha holds 2 and beta holds 3. 2 + 3 = 5, so now gamma holds the
number 5.
printf(“%i”, gamma);
This prints the value held by gamma onto the screen. Gamma is holding 5, therefore,
our program prints the number 5 onto the screen.
You can experiment with this to try subtracting, multiplying, dividing or
combinations of all 4 using the +, -, * and / operators.
Once you are confident with the concepts in this tutorial, the next tutorial will go
into more depth about variables.

<< CLICK HERE TO GO BACK TO PART 1: GETTING STARTED WITH C PROGRAMMING <<

Hello, and welcome to the 2nd tutorial.

Last time, we learnt how to make a project and print some text onto the screen. In this tutorial, we are going to learn about variables.

Like in maths, a variable is a value that can change. We use these all the time in programming to store numbers and data in our programs.

For example, say we were making a game, and we wanted to store how many points the player has.

We would make a variable, lets call it ‘x‘.

At the start of the game, the player would have no points. So we would set x to 0

x = 0

When a player shoots an enemy, we would want him to gain a point. For this we would have to increase x by one.

x = x + 1

Hopefully this explains the importance and use of variables to you.

In C, we have to declare the variable by name to tell the computer it’s there. We declare a variable like so;

type name;

Where type is the type of the variable stored. Common types are int (integer number), float (floating point, can store up to 7 significant figures), char (a character) and double (a float with twice the precision).

The name is what we want to call the variable. In maths, we commonly name a variable ‘x’, so let’s use that as an example.

This is how you would declare a integer called x

int x;

Now we have a space in memory, named x, which can hold an integer number value.

Now, to set x to a value, we use the ‘=’ assignment operator. If we wanted x to contain 5, we would do this;

x = 5;

To write an integer onto the screen, we use printf like so;

printf(“%i”, x);

Where %i says we’re printing an integer, and x is the name of the variable we’re printing.

Consider the following program;

#include <stdio.h>

int main(){

int alpha;

int beta;

int gamma;

alpha = 3;

beta = 2;

gamma = alpha + beta;

printf(“%i”, gamma);

return 0;

}

Can you guess what is printed onto the screen? The answer is 5. Let’s go over this code in more detail.

At the beginning of our main function, we declare 3 variables

int alpha;

int beta;

int gamma;

Their names are alpha, beta, and gamma, and they are all integers. This declaration allows them to be used to hold integer numbers in our program.

01

Next, we assign numbers to alpha and beta.

alpha = 3;

beta = 2;

This puts the number 3 into alpha, and the number 2 into beta. Now alpha holds the number 3 in memory, and beta holds the number 2.

02

Our next line does something more interesting. We now use the + operator to assign a value to gamma.

gamma = alpha + beta;

This line tells gamma to hold the value held by alpha, ADDED TO the value held by beta.

In our program, alpha holds 2 and beta holds 3. 2 + 3 = 5, so now gamma holds the number 5.

03

printf(“%i”, gamma);

This prints the value held by gamma onto the screen. Gamma is holding 5, therefore, our program prints the number 5 onto the screen. You can experiment with this to try subtracting, multiplying, dividing or combinations of all 4 using the +, -, * and / operators.

Once you are confident with the concepts in this tutorial, the next tutorial will go into more depth about variables.

Thankyou for reading.

-Ryan

<< CLICK HERE TO GO BACK TO PART 1: GETTING STARTED WITH C PROGRAMMING <<


Categorised as: Tutorials


One Comment

  1. Your a natural at writing these tuturials you should do more. I found your explanations very informative compared to most. Look forward to a tutorial 3.

Leave a Reply

Your email address will not be published. Required fields are marked *