Types of Variables

Arkadiusz Włodarczyk
A free video tutorial from Arkadiusz Włodarczyk
Excellent teacher, Expert in Programming
4.4 instructor rating • 18 courses • 248,570 students

Lecture description

We learn basics types of variables in the c++ language like:
- int, float, double, string, char, bool 
We learn about address of variable, how to combine string, what are unsigned variables, what are constant variables and when to use them.

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++

15:44:00 of on-demand video • Updated November 2020

  • 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 more types of variables in the C++ language. This is quite logical that there are more types. Right now we only know how to create variables that are integer numbers but we also need to know how to store real numbers character sequence of characters before we start. Let's remind about how to define variables and let's talk about one problem that might happen when we use Varietes if we want to define variable we have to write what type of table and then name of vibe and uncynical we can also define in one line more than one variable in order to do it. We should write something like to memorize the one name for it but don't name over a frame and we could just continue with it. Let's see if it's working and a B and C. Bill Bill as you can see our programs are working well. We can also as saying one of that if we're able in the same time for example and saying that one the same one. OK. And that's Billett. And he's working well. Let's see what values are stored in that area. But right now see out a lot. And now let's just do that thing to the b c. OK. Now that's compiled as you can see here. Variable names A and C has good value that we assign right for it and Twente by the Y variable B has a number here. If you compile that program on your computer you might have even different numbers. And why is it happening. It's because we haven't the same and the value to the variable named B. But this might be a bit confusing. As I said in the previous lesson every variable has got something like address. So let's address this. Verey was to cancel out. So that's something like that . Now let's come by . So as you can see in the address is some weird numbers kinetics connected with some letters like the f right and the notation is called hexadecimal code. We don't need to understand it right now but we should notice one thing each of the other S is different right. Here I have four to four four to three and see. So they differ from each other and as I said in the previous lesson when they define able. So when you write something like and a. We are then allocating reserving memory in the same time it also means that we are allocating address for our variable but allocating doesn't mean clearing the value that is under duress and locating needs that we just want to reserve that space for other value. OK so if you write for example just in B. We are just allocating memory that already might have something. S. But how is it possible still how and in programs that are running on our computer. Could you use that space before for a special reason for example to start actually. And when we stop using programs that start for example the actual year the spacing them more is again free. It can be then again allocated but other programs like the one that have just written. But the value is still under the value under did address is not being removed. That's why under variable name B we have some random value. So to avoid complications before using. And the variable we should add same default values or just and the right. And now we are sure what value ease under the variable name B. Now we are sure that it will be zero not some random number. OK. And let's start from either integer type. Let's move right. Right now we can so in. Ain't that the one numbers from about minus 2 billions to about plus 2 billions. Right. But in the same time we have to allocate 4 bytes of memory as you can probably guess. You don't need soul large numbers in every situation. And if we don't need large numbers and we want to save memore we can use for example something like that short integer. And let's name it the one that's type. Let us assume values that range from minus thirty two thousand seven hundred sixty eight to plus thirty two thousand seven hundred seventy sixty seven. And so now we can assume smaller values but we need only two bytes of memory. So let's not get two buckets and we don't need to write in and when we define short integers we can do it that way like now. Or we can just remove we can just remove the end and it's still the short integer. Let's say some value to that guy for example 5 that sends it to the console output. As you can see it's working fine. Here we have five arcane studies study something you now what if I wanted to use real numbers numbers it's a fractional part. Now when I write something like for example that and I compile it as you can see if you are losing the value after the point right we lose that seeks we have only five on the cancel out the type that can solve real numbers are called in programming floating point numbers. There are two types. Two of the types float. That's the same I with the guy doubled and that's the cause. Mmm hmm. That's Kampar. As you can see now we are not losing the part after the dot and now was the difference between float and double float allocates four bytes of memory. Double locates a bytes of memory. So it's just doubling the float. Right. And to the float variable we can is saying numbers that are up to thirty eight zeros. And here we can see numbers that up to for us. So it's big the fourth is right. And in addition to these double has got greater precision which means that it can save more digits after the point. So it means that you can save more digits here right. And it's because double very you can see more digits after the point which means we should use doubles we are calculating something important where the precision is needed or when to so very large not very large numbers. Now let's talk about saving car US car park their car the four car is an abbreviation for character here. OK. And that sends it to the council. I'll put say something first of all we can't do it that way because the compiler we try to is saying here are the variable named A. So that guy who if you want to sing the character a we should use something like applesauce like that . Now as you can see everything is working fine. What if I wanted to store more than one character in the verb. You want a sequence of characters that are called strings. We should do string type so string. That's something like that. And as you can see text should be inside this quotes signs for the sequence of cards. Let's Cyndy to council output is working well let's learn how to combine two different streaks. So I have for example sync X saying you heard one Tsering y two and now three ink combine strings equals x Blas y see how to combine strings and C as you can see Paduan is now combined with part 2. And that bold that is called combines things we can add. For example space between them. Now like that. So to combine two strings we should just use the plus signs. Right. It's working. Well let's talk about balling right now. Boo. True or false. What Bowl is a type that can store only two values. True or false. So maybe let's send it to the con.. I'll vote count pi as you can see. We don't. Something like to have one here right here is true. Why ever number these not zero is going to be true in the C++ language. Zero is representing false so false is always zero. Every think every other number is true. Let's check it out. For example it falls as you can see we have zero here and there's no write something like that. As you can see we have one here. One is representing true. One is representing true and that's the same the negative number as you can see it's 1 0 0 0. OK so everything is working well. Like I said. Now let's talk about unsigned types of variables. When we look at the guy here. I told you that say well he was for or minus thirty two thousand seven hundred sixty two thirty two thousand seven hundred sixty seven. And there are many situations where we don't need negative numbers. Then we can remove the minus signs removed right now we are making them unsigned stipes because we have all that some by removing negative part of integer we are increasing amount of positive numbers but the amount of negative numbers. So for outside unsigned short end we could assign a value the branch from Zuid for unsigned short and zero to seek the five thousand five hundred thirty five. And that number here is a sum of the numbers here. You can check it out. And let's send it to the council as we've asked. We see as you can see it's working. Well let's talk about that. We can also remove that teacher unsigned char this on such short integer. All right. It's working well. We can add unsigned to all other dives. Flaw double. So it's working the same. And let's talk about Colson's royals. Now there might be situations where we gonna use some things in our program many times and we also want to make it change a ball by mistake in order to do it. We have to be for type of a way variable award Cohn's which is abbreviation for constants. So for example we have strings you name it. Name of name calling. Where has for example. And I don't want the name being changed so at comes to here and now let's send it to cancel out but as you can see it's working well. And as you have noticed I'm using uppercase for the name of constant array bool. You don't have to do it that way but is good idea because when your program gets longer you will know what is constant variable just by looking at a name over a bit right. OK. So is a constant for sure and we all know that we can't change the value of these variable from the other part of program. Let's throw it out. So you see if I'm lying or not. OK name of game. And now something let's build it. And the secrecy we have here. We can do it. So I didn't lie. OK. That's all. In this lesson. Thank you very much