Arkadiusz Włodarczyk
A free video tutorial from Arkadiusz Włodarczyk
Excellent teacher, Expert in Programming
4.5 instructor rating • 18 courses • 267,075 students

Lecture description

We are exploring the concept of "variable", "variable type", "declaration", "definition". Exploring naming rules of variables and notations.

Learn more from the full course

C++: From Beginner to Expert

Designed for people who don't have any knowledge about the programming and want to program in C++

16:13:02 of on-demand video • Updated April 2021

  • How to use C++ language in action
  • What is compiler / IDE / Variables / types of variables etc.
  • How to work with files - fstream library (i/o operation on files)
  • operators - arithmetic, assigment, logical, bitwise
  • conditions like if / else / switch
  • arrays / multi-dimensional arrays
  • loops - for / while / do-while
  • functions, overloading functions, passing variables to functions etc.
  • structures
  • referencers
  • pointers
  • dynamic allocation of memory
  • creating project in IDE
  • classes
  • object oriented programming
  • class and function templates
  • namespaces
  • exceptions
  • input / output streams and validation of data
  • STL: vector
English Hello everybody. Today we're going to talk about variables in the C++ language. Let's note that in comments, slash slash and now Variables OK let's start this topic not from what is variable but why do we need variables. For example I want to send something to the console ouput. Lets's write. cout two angle brackets, four, semi-colon. So it means that I want to send a number four to the console output but hey what if I wanted to store this number somewhere because I would like to make some operations on that number or even change it a bit . Look right now we have the number four here right? And after that instruction is invoked. We can't do anything with that number anymore So we can't do anything here. With that number here. So this number disappears. We don't have any reference to that number. That's why we need variables. Variable as the name suggests is something that is able to vary. So it means that it is changeable it can be changee anytime you want. So variables are some kind of containers that can store values like numbers. Imagine that these beans/containers here. Are our variables. Each of them has different label name. Right. A B and C. As you can see each of that container is also a bit different. They have different shapes. It means that containers variables can store different values different type of values. Now we have discovered a new thing. The type of variable. The type describes what can we put into our containers . Variables Okay Lets's create a variable in our program. Let's name it 'a'. We have got only label now, name of our container variable. We have to also describe what can we put into our container variable. So we have to declare the type of variable. Let's start from the int, which stands for integer. Integer are numbers that have no fractional part and they can be negative. So now we can assign to our variable only integer. By assign I'm meaning putting something into that variable. But let's stop for a second and think about memory in our PC. Is it unlimited? No it is limited by our hardware because we can only assign subset of integer numbers. In our situation we are allowed to assign to our variable named 'a' numbers that range from minus 2 billions to about plus 2 billions Let's do that. a equals For example 4. The equal sign. Allow us to assign values to our variables. The process of assigning value to variable is called initilization initialization. OK. I can do that process of assigning also in one line I could write something like that I will comment that thing here. OK but let's assume I want to assign to variable a. Something like For example I can do it because this value is greater than two billions. OK. And it's a really good idea that we can do it because we would take too much memory in our PC. We don't need that number for all the situations. Right. OK. Now I will say something very important. When you look at any book what is the easiest way to find something inside? If I give you for example one section of book or even only one word I will tell you to find this section inside that book that has 1000 pages. I'm sure it will take a very long time to complete the task. Right. What about the same situation But now I'm giving you the number of page, you will find it probably almost instantly right? In programming world situation is almost the same. The section word in our program it called label or name of variable. The page number is called address address. OK. So by using variable address we can get to values faster. But this is a bit advanced topic and it is connected with pointers. We'll explain it later very precisely. But now I want you to know how to get address of variable. In order to do it... We have to write something like ampersand. It's an ampersand. And lets do in another line. Ampersand. And now the name of variable. OK. Let's send that things to the console output. Lets do something like that now. cout and cout ampersand A Now let's build it. And as you can see we have everything in one line. So we will add here something like endl; endl stands here for end of line. so that thing here is indicating the end of line. And it's adding the enter at the end right?. So now I build it as you can see we are having two lines for the first line there is four. Right. And in the second line we have the address of that four. So we have the address of the variable. When we write something like 'a' we are declaring variable. Declaring means that we are informing compiler that there will be somewhere in our program something named in our situation. 'A' When you write something like int 'a'. In the c++ the language We are also defining variable. Defining means declaring and in addition to this we are also reserving, allocating, space in memory, in our situation allocating space for the variable that is integer, right? int allocates 4 bytes of memory. One byte is 8 bits. So we are allocating 32 of bits in PCs and memory. Bits are zeros or ones signs. So we are allocating in one sequence somewhere in our memory thirty two places that can be filled with zero or ones. As I said in the first lesson that sequence of 0 and one can be translated to our language or our language can be translated to that sequence right? And as I said earlier. Variable's values can be changed any time. In the second lesson I was talking a lot about that compiler is reading everything from the top to the bottom right? So if I write something like that and we copy that. thing... What will our compiler do now? Let's start from the beginning because our compiler is doing it that way. So our complier is including the IO stream library it will use the std namespace and then it will invoke the main function. In that line int a equals 4, our compiler will be informed about existence of variable named a. It will also allocate somewhere four bytes of memory and because of initialization that equal(=) sign right? It will change a sequence of zero and ones to represent number four in our PC language. After compiler will print to the console output value an address of our variable. Here. Here a equals 10, our compiler will change the value of variable named a. So we will change a bit that sequence of zero and ones and. But it won't change the other variables of course Right? Let's compile it as we can see the number has changed but the address is still the same. OK let's sttart how we should name variables now. That's do it in comment and the first thing is that we can't have two variables that have the same name variables can't have the same name. So if I want to declare two variables I have to have different names. Right? That way we can run our program but we can do something like that. Error! re-declaration of int 'a'. we can't do something like that. We can start the name of variable from the number but we can use numbers in other places so variables can't start from the number lets's check it out. I will try to create int and now I'll do something like for example that right? I'm trying to build it. I can't do it but I can do it. that way easy. We can't use spaces but we can use underscores so we can't use spaces. I can't do something like int and now will do something like prime number I can't do something like that. but you can use Of course the underscores so I can do it that way. Prime number. That's all right. We can execute our program. When we declare variables we should try to make them self-descriptive. Self-descriptive means that we should be able to guess by looking at the name of variable. what is this variable created for. For example we can write int prime number Or maybe I can do it even that way in the prime number. I can do it primeNr I can do it also That way iPrimeNr; The i prefix here. Let us guess by just looking at the name that the type of variable is integer right? Because Int. This notation, that one here, is called Hungarian notation. You can type it in google and you can read about other types of notation. OK so let's note that. Our variables should be self-descriptive and we can't use special characters, keywords. Let's see what we can do. For example I can't do something like int 'using' Because it's a keyword. I can do it that way. It's not working. I can't use the special character like #. It's not working. OK so let's note that variables can't have special characters keywords can't be constructed Can't be constructed of special characters/keywords. OK. Variables should be nouns. They shouldn't be adjectives or other things like that they should be just nouns. OK that's all. And the next lesson we'll discover more types of variables and thank you everyone