Variables
Well, now we introduce the variables. To understand the concept, I'll put a very simple example: imagine we have boxes that we use to store objects. But these cases are rare forms that only allow us to keep a certain type of object. There are shoe boxes, bottle crates, boxes for mosquitoes, etc ... As well, these boxes are variables, and store data: numbers, words, etc ... Are called variables because you can change the object that they have inside, but whenever the same type.
For example, the following code:
_________________________________________________________________________________________
#include
#include
int main(int argc, char *argv[])
{
int age_jhon = 5;
int age_mike = 12;
printf("Age of John: %d\n", age_jhon);
printf("Age of Mike: %d\n", age_mike);
getch();
return 0;
}
_________________________________________________________________________________________
int age_jhon = 5;
_________________________________________________________________________________________
We define a variable of type int with the name age_jhon, integer, and assign a value of 5.
_________________________________________________________________________________________
int age_mike = 12;
_________________________________________________________________________________________
Here another variable of type integer (int) age_mike named, and assigned the value 12.
_________________________________________________________________________________________
printf("Age of John: %d\n", age_john);
_________________________________________________________________________________________
As we know from the class II, printf() prints on the screen. But here you will see that it prints the value of the variable age_john. You can try changing the value of the variables (you change to 5 other number) and see how it changes what is written on the screen. You see there is a sentence between two double quotes ("Age of John: %d\n"), then a comma, and a variable. In fact, there may be several variables. For example:
_________________________________________________________________________________________
#include
#include
int main(int argc, char *argv[])
{
int age_john = 5;
int age_mike = 12;
printf("Age of John and Mike: %d y %d\n", age_john, age_mike);
getch();
return 0;
}
Here you'll see that both ages appear in the same printf(). Place all the variables we want to show on the screen separated by a comma after the phrase. Then in the phrase put % followed by a character type (d for int for now, that is, %d). Each % is in order with the list of variables. In any case, don't worry so much.
_________________________________________________________________________________________
getch();
Wait for a keystroke.
Another example:
_________________________________________________________________________________________
#include
#include
int main(int argc, char *argv[])
{
float theoretical_note = 5.0;
float practical_note = 2.0;
printf("Theoretical examination mark: %f\n", nota_teorico);
printf("Practical examination mark: %f\n", nota_practico);
getch();
return 0;
}
_________________________________________________________________________________________
Now we have declared float variables, ie real numbers or decimals (comma):
_________________________________________________________________________________________
float theoretical_note = 5.0;
float practical_note = 2.0;
_________________________________________________________________________________________
And as you will see, in printf() no longer use %d, but %f, because the variables are "float" and not "int".
Another example, this time with strings:
_________________________________________________________________________________________
#include
#include
int main(int argc, char *argv[])
{
char* student_name = "Fernando Alonso";
char* teacher_name = "Melendi";
printf("Name of the teacher: %s\n", teacher_name);
printf("Name of the bad student: %s\n", student_name);
getch();
return 0;
}
_________________________________________________________________________________________
Now we use the data type char* and as result we have changed %f with %s in printf().
It's important to note that variable names can't have spaces or start with a number. Can only be composed of letters, numbers and the underscore character (_). If we stick to these rules, the compiler will give an error indicating the line of error. You can try it.
And as an exercise, I let it make me a small program that, watched these examples, print:
(Student name) scored a note (Note theory) in the review of the day (Date of theory)
Of course replacing parentheses content by the corresponding variables. Hint: the date is as the name.
That I leave you to the next chapter.