C-mpler (13 Part Series)
As in algebra, in programming we also have variables. Variables are "boxes“, which have a name and contain a value. The name can be one letter, like 'x', one word like "grade" or even a few words like "maxGrade" (the convention in C is that the first word always starts with a lower case letter, and each other word starts with a capital letter, with no intervening spaces or punctuation. This convention is called camel case). You can add a number to the name, but it has to come only after at least one letter, for example: 'x1' (and not
'1x'). The value can be the number 10 (as in algebra: x=10), the character 'h', the word "hello" or even a car!
In C each box has a type. Only values of this type can enter the box - just like the babies toy which has a box with holes in different shapes. If a baby tries (and they always try, of course) to put a shape in the wrong place - it doesn't enter the box. In our case, "the computer" will tell us that there is a problem: a mismatch between a variable and the type of value which we try to put in it.
Ok, so far so good. But how do we create a variable and how do we put something in it?
The answer for the first question is:
What is "int"? It's a type of a variable. The basic types of variables are:
- char: a shortcut to the word "character". In this variable type, we save one letter or one digit. The value of this variable has to be between ' '. For example, the letter 'a'. Notice, in C there is a difference between ' and ". (The double quote marks are used for strings, which are groups of chars. We will talk about it later).
- int: a shortcut to the word "integer". In this variable type, we save one whole number - a number without a point (that can be a negative number). For example, 10.
- float: a shortcut to "floating-point arithmetic". In this variable type, we save one real number, what of course includes integers. For example, 10.25.
- double: like float, but can contain larger numbers. We will talk about it later. (For now, you can use both float and double and assume they are the same).
Ok, now, to the answer of the second question (remember?).
In order to insert a value into a variable, we have to use the '=' operator which means: "insert". For example:
int x = 10;
Just like in algebra! We "told the computer" that the box which we named "x" contains the numeric integer value 10.
Before we go on and talk about more mathematic operators, I want to teach you about comments. Comments are really important lines in the code, in which the programmer writes in English about his/her code. Sometimes, the comments are like: "When I wrote this code only me and God know what it does - now just God knows" - but often they are really informative like:
// The variable "maxGrade" contains the max grade you can get in the test int maxGrade = 100;
As you can see, the format for comments is:
// Some text before a line int x = 1; int y = 2; // Some text at the end of a line // This doesn't work, because the code is also in the comment: int z = 3;
If you want to write a long comment, which appears in more than one line you can use:
/* Some text Some more text And... that's it! */
Ok. That's all you need to know about comments, for now at least. Let's go back to talk about mathematic operators. As in algebra, we have the basic mathematic operations: addition, subtraction, multiplication, and division.
In C, for these operations, we can use the next mathematics operators:
int x = 10; // The plus operator, for addition int a = x + 1; // a is 11 // The minus operator, for subtraction int b = x - 1; // b is 9 // The asterisk operator, for multiplication int z = x * 2; // z is 20 // The slash operator, for division int w = x / 2; // w is 5
If we want to make a change in a variable and save the change in the same place, we can do that this way:
int x = 1; x = x + 1; // x is 2 x = x - 1; // x is 1 x = x * 2; // x is 2 x = x / 2; // x is 1
Or even in the shorter way:
int x = 1; x += 1; // x is 2 x -= 1; // x is 1 x *= 2; // x is 2 x /= 2; // x is 1
The last two operators for this post are the "++" operator and the "--" operator. These operators can come before a variable or after it - and they just increase the value of the variable by 1 (with "++") or decrease it by 1 (with "--"). For example:
int x = 1; x++; // x is 2 x--; // x is 1 ++x; // x is 2 --x; // x is 1
Can you guess what is the difference between if the operator comes before the variable and if the operator comes after the variable? To be honest, the difference is not really important and we will talk about it later. In order to make your code more readable, you should put it after the variable, unless you have a really good reason for putting it otherwise.