Basic Variables & Operators
Tags: c, beginners
Published: 2019-06-11 08:24:53

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](https://en.wikipedia.org/wiki/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:
```cpp
[variable_type] [variable_name];
```
For example:
```cpp
int x;
```

####__Notice! at the end of each line, we must put a semicolon (';')__

What is "int"? It's a type of a variable. The basic types of variables are:
 
#__Basic variable types__:
- __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:
```cpp
int x = 10;
```
Just like in algebra! We "told the computer" that the box which we named "x" contains the numeric integer value 10.

#__Comments__:
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:
```cpp
// 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:
```cpp
// 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:
```cpp
/* 
Some text
Some more text
And... that's it!
*/
```
#__Basic operators__:
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:
```cpp
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:
```cpp
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:
```cpp
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:
```cpp
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.

####__Always remember, it's much C-mpler than you thought!__

Regards,
Gilad