Lesson 2

Variables and data types


Variables are something we use for storing different values in the memory (RAM) and is one of the most essential pieces you need to know in order to create a real program.

Let's take a look at some variables and how they work!

By looking at this program, you should already be familiar with some of its syntax. What we are doing is setting up 4 different variables with different data types and then printing them out to the screen.

Declaring variables

You can name your variables anything you want as long as the name doesn't start with a number or has any weird special characters or spaces.

Some valid variable names are: num1, myName, _age, $_years, my_score.

Some invalid variable names are: 123myNumber, my variable.

When naming your variables it's a good to choose a name that corresponds to what the variable is actually doing. For instance, if I want to declare a variable that stores the value of my income I should name it something like income or money and not my_variable.

Back to our program! Notice that our first variable, number1, is declared using the int data type and initialised with the value 10.

The data type is telling the compiler that the variable number1 is going to be used for storing an integer value (a whole number). Some examples of valid integers are: 11, 1717, 39203920 and 182182.

It's considered good practice to always initialise your variables by giving them a value in order to avoid bugs in your program.

The next variable in our program is number2 which uses the same data type as number1 but stores a different value.

The c variable uses the char data type for storing a character. A "char" is basically a letter or a sign that is printed on your keyboard!

Finally we have the pi variable using the double data type. What sets double apart from int is that it's capable of storing decimal numbers like 3.143.14.

Printing variables

In order to print out the number1 variable, we write printf("%d", number1);.

The first argument in our printf call, "%d", clarifies that we are going to print out a number (d stands for "digit").

Notice that our other printf's use %lf ("long float", for printing out decimal values) and %c ("char", for printing out characters).

As you may have seen, we also use "backslash n" in order to print out a new line at the end of each print statement. If we left this out, everything would be printed on the same line, which would look really confusing:

What if we want to divide two integers but we still want to get the answer with decimals? If we divide two integers with each other, it will be rounded down, so for instance 19 / 10 would result in 1.0. We could use something called type casting for this. Let's take this scenario as an example:

as we see, the result says 1.0 but it actually is 1.9.. To get the correct result, we can use something called type casting. Let's take a look at the same code using type casting.

Now that you've got a grip of some of the basics, let's try to create our first real project!


Johan Modée

Ska vara: double result = (double) y / x

Rickard Borg

@johan-mode: Tack! Har blivit på fel ställe här, fixar till det! :)

Johan Modée

"\ n " utan space

Rickard Borg

@johan-mode: Ja, det har försvunnit, ska försöka fixa till det! :)