This is the second tutorial I made about c++ , The last one teach you how to code your first program generally called " Hello world program "
Now that We learn the structure of c++ and how a program will look like now we will study variables, how to declare them and how to use them.
First, you will need to know the rules of naming your variable which you can find here
Now that you know how to name your variable let's talk about variables.
We use variable to stock information that we will use later in our program, let's say that our program is calculator and we want to calculate the addition of two numbers but how do my computer can remember the first number after I write the second one ?
This is where variable are for, you can stock first number in a variable and then use it to calculate the addition..
How to declare a variable ?
In order to use a variable in C++, you must first declare it specifying
which type you want it to be. The syntax to declare a new variable
is to write the specifier of the desired type (like int,
float...) followed by a valid variable identifier. For example:
int age ;
Bool Answer ;
long int Numb
Now for example if we have 4 variable with same type how can we declare them ?
int age,Numb ;
Global Variable or local variable
when you declare a variable as a global one all program functions can use it anytime with declaring it since it's already declared but if it's local variable then only the function where it have been declared can use it.
Watch this YouTube video to understand all what you have to know about variables :
Aucun commentaire:
Enregistrer un commentaire