Variables and Arithmetic Operators

Now we can move to something more exiting than saying “Hello world!”. Next step is to use numbers and do some math operations. Consider the following code:

#include <stdio.h>

int main(){
printf("%d\n", 5+3);
return 0;
}

This code should look familiar to you, the only difference is what is inside of the “printf”. In order to print some numbers that are going to be calculated, and we want them to be treated as numbers, we need to tell the “printf” built in function that we want to print a decimal number by writing “%d”. Each type of data has their own way and we will see that later.

What if we want to add two numbers and store the output in some memory location for further use? We can use a variable for this. Variables are simply names used to refer to some location in memory, we do not have to know in which memory location it is because it could be hard to remember something like this 0xbffd9b3f. A variable need a type, this is, what kind of value is going to store. Some common variable types are:

• char – For characters. 1 byte. 0 to 255
• int – Most popular for variables. Typically 4 bytes (depends on machine or microcontroller). -2,147,483,648 .. 2,147,483,647
• float – A single-precision floating point value. 4 bytes. -3.4E38 .. 3.4E38
• double – A double-precision floating point value. 8 bytes. -1.7E308 .. 1.7E308

If you do not know how big is a specific type of variable, you can use “sizeof” function to find out. Example:

#include <stdio.h>

int main(){
printf("Char: %d\n", sizeof(char));
printf("Int: %d\n", sizeof(int));
printf("Float: %d\n", sizeof(float));
return 0;
}

In my cause it output:
Char: 1
Int: 4
Float: 4

Now let use variables to store data from different operations. Have a look to the following code:

#include <stdio.h>

int myIntVar;
float myFloatVar;

int main(){
// Addition and subtraction
myIntVar = 2+4-1;
myFloatVar = 2+4-1;

printf("My Integer Variable: %d\n", myIntVar);
printf("My Float Variable: %f\n", myFloatVar);

// Multiplication
myIntVar = 2*3;
myFloatVar = 2*3;

printf("\nMultiplication\n");
printf("My Integer Variable: %d\n", myIntVar);
printf("My Float Variable: %f\n", myFloatVar);

// Division
myIntVar = 10/3;
myFloatVar = 10/3;

printf("\nDivition\n");
printf("My Integer Variable: %d\n", myIntVar);
printf("My Float Variable: %f\n", myFloatVar);

// Division 2
myIntVar = 10/3;
// Operandos in the division are float too
myFloatVar = 10.0/3.0;

printf("\nDivition 2\n");
printf("My Integer Variable: %d\n", myIntVar);
printf("My Float Variable: %f\n", myFloatVar);

// Modulus of a division
myIntVar = 10%4;
printf("\nModulus of a division\n");
printf("10/4 modulus: %d\n", myIntVar);  // 10/4 = 2 and remains 2

return 0;
}

Look that if we perform divisions with integers operators, the result will be integer too, even if the variable is float. Another way to force one operation to be performed as floating point is by “casting” one of the operators using “float”. Example:

#include <stdio.h>

int main(){
float result;

result = (float)10/3;
printf("%f\n", result);

return 0;
}

Also, as you noticed we can perform different kind of math operations, such as additions, subtractions, multiplications, divisions and modulus.