Variables in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is a Cherno welcome to another video so I'm on the couch this time after a long day of work for I've mixed it up let's talk about variables in step above so when we write a program in C++ we want to be able to use data most of what programming is about is actually using data we manipulate data that's what we do so any kind of data that we use in our program that we want to change that we want to modify that we want to read and write from we need to store this data in something called a variable so variables basically allow us to name a piece of data that we store in memory so that we can keep using it as an example pretend that you're making a game and you've got a player in your game and that player character has some kind of position on the map and the player character can move so we need to be able to store the player's position as some kind of variable in our memory so that when it comes time to draw the player on the screen or interact with the rest of the level we can actually see hey where on earth is the player so we would want to store the players position in a variable this is basically one of the fundamentals of writing a program in any language we need to be able to play with data and store that data somewhere when we create a variable is going to be stored in memory in one of two places the stack or the heap don't worry we're going to have a lot of videos discussing how memory actually work so if you're looking for a more more of an in-depth kind of explanation that'll definitely come but for now just know that variables do occupy memory that's where we actually store the data in our computer's memory in C++ were given a bunch of primitive data types these primitive data types essentially form the building blocks of any kind of data we store in our program each data type that Super Plus gives us has a specific purpose whilst it has a specific purpose you don't actually have to use it for that purpose if interesting because if applause is a very powerful language which means there are actually very few rules when you actually get down to it so when I explain variables I like to say that really the only distinction between the different variable types you have in Super Plus is the size how much memory does this variable occupy when it comes down to it really the only difference between these primitive data types how big are they let's go ahead and jump into Visual Studio and take a look at some examples so we've actually already got a variable type that we're using here int in stands for integer and it lets us store an integer in a given range if we want to declare a brand new variable we can do so by typing the type of the variable giving it some kind of name for example variable and then giving it a value now this last part is optional you don't have to give it a value immediately but for now let's just give it the value eight an integer is the data type that is traditionally four bytes large the actual size of a data type depends on the compiler so it may be different depending on what compiler you're using ultimately it's the compilers choice to tell you how big your data is going to be the Imps data type is meant for storing integers in a certain range because it's four bytes large we are limited as to what kind of numbers we can store with it specifically this is something called a signed integer that can store a value of around negative two billion to positive 2 billion anything larger or smaller than that is going to require more data to store than this int actually support so with 4 bytes of data we can store a value between this range so let's go ahead and try and print out our variable to the console to see what it actually is I'll substitute this hello world with this actual variable this is how we can log a variable to the console at nf5 to run our program and you can see it prints out the number 8 awesome we can go ahead and modify our variable for example by reassigning it to something else like 20 let's go ahead and print it again and see what happens so we're printing it once here and then again here so we should get the value 8 printing first and then the value 20 and you can see if we run our program that's exactly what we get cool so as I said an inch data type can store a value between negative 2 billion and positive 2 billion so you might be like why is it negative 2 billion and positive 2 billion it's not exactly 2 billion by the way it's like 2 point something billion where are these limits coming from do they make any sense and the answer is yes they make sense they are directly tied with the size of the variable that is how much data were allowed to store in it an integer is 4 bytes with 4 bytes of data we can store values in that let's break this down a little bit so one byte is eight bits of data which means that four bytes is 32 bits of data because this variable is signed meaning it can be negative it contains a sign like a negative sign because this variable is find one of those bits one of those 32 bits has to be for the sign so that we know if it's positive or negative which only leaves 31 bits left for the actual number now a bit can either be 0 or 1 so there are 2 possible values for 1 bit of data so using a little bit of maths here we can say that we have 31 bits to play with 2 possible values took bit so what is 2 to the power of 31 if we crack open a calculator here and type in 2 to the power of 31 we will get about 2 billion that value there that 2.1 billion that is the maximum number that we can store with an integer now remember we also have one bit that is reserved for whether or not that number is negative so because of that we can store up to that number from 0 but also we can go the other way and store all the negative values down to negative 2 by 1 billion but I don't want negative values I hear you say is there a way to just get rid of that one bit being for the negative sign and just use it as part of my number why yes yes there is that is what we call an unsigned number that means it's the number that does not have a sign meaning is always positive in C++ we can make one of those by just typing in unsigned in front of our integer so now what we've done is we have 32 bits to play with and 2 to the power of 32 of course is double what we have here for point two nine billion and that's basically what the unsigned keyword does in Sabre slot it lets us define an integer that does not have a sign bit okay so what other data types do we have available to us what if I don't want to for byte integer what other types are there so as far as integer values goes we actually have quite a few we've got char which is one byte of data we've got short which is two bytes of data we have int which is four bytes of data we have long which is also usually four bytes of data depending on the compiler and then we have long long which is usually eight bytes of data there's also other types like long and there are a few different modifications I'm not going to go through all of them but basic ones are these five you can also add on signs to any of these and it will remove that sign bit and let you set a larger number chart traditionally is also used for storing characters not just numbers so above I'm assigning numbers to it like 50 you can also assign characters to it like a now that's not to say you can't assign characters to other integers you can because at the end of the day this character that I five to this letter a is just a number in fact that number that numeric value associated with that character the character a is 65 now if numbers are just characters and if characters are just numbers then why exactly do we have this distinction why do I say that char is specifically used for characters whereas it's really not that is because we often as programmers make assumptions about certain data types if I pass in a char and call it something like character I usually expect you to actually assign a character to it so a good example of this is if you actually try and print out a char if I print out this variable a for example and I hit f5 I'm not going to get the number associated with it I'm going to get the character a printed out so if I replace this with this actual numeric value like 65 I'm also going to get the value a printed out as you can see over here because C out if I pass in a char into C out is going to treat it like a character not like a number if I change it to be some other type like a short for example and hit f5 you can see that C out no longer treats it like a character it's going to actually print out the numeric value and even if I assign a character here it's just really assigning the value 65 so if I run this again you can see that we get 65 so the reason I'm telling you all this is because I want you to understand that data types the usage of data types is just up to the programmer really there are certain conventions that we have in place but there's nothing concrete that you have to actually follow there are very little rules and c++ after all so because of that I do want you to realize that the only real difference between these data types is how much memory will be allocated when you create a variable with that data type so with those integer types aside what if I want to store a decimal value for example five point five how do I do that well for that we have two data types we have float and we have double there are also some modifies that you can do like long double we're not going to get into those so a float is basically a decimal value that we can store that occupies four bytes of data so let's define a variable here such as 5.5 how do we do that let's also replace this variable a we're printing out our float variable and compile our file let's hit f5 to run our program and you can see we get 5.5 printed out fantastic now you may think that you've defined a float here but you actually haven't you've actually defined a double if we go back to visual studio and we hover our mouse over this value you can see that in brackets it says double as I just mentioned we have two different variables that we can use to store decimal numbers float and double so how do we discern between what a double is and what a float is the way we do that is by basically appending an F to our float variables it can be lowercase or uppercase doesn't matter but the point is if we have an F you can see that we've actually declared a flirt so floats are basically four bytes large and doubles are eight bytes large finally we have one more primitive datatype to play with and that is bull now bull stands for bullying and it can either be true or false if we try and print it to our console and hit f5 you can see that we'll actually get a numeric value one because of course there's no such thing as true or false those are English words computer deal with numbers so basically zero means false and anything except zero any other number means true in this case we'll actually get one printing to the console indicating that it is true if we change the default and run our program we will get 0 which means false the bull' data type occupies one byte of memory now you might be wondering one byte why the bull can either be true or false surely that only takes one bit to represent and you are correct it does take one bit to represent however when we're dealing with addressing memory that is we to retrieve our ball from memory or stored in memory there is no way for us to actually address individual bits we can only address bytes so because of that we can't actually create a variable type that is one bit because we'd need to be able to access it and we can't we can only access by now course one thing you could do on the other hand is be really smart about how you store data and store eight all in one bite that's totally okay one bit per bull but you still have that one byte of allocating memory we'll probably talk about advanced fun tricks like that in the future but for now a bull is one byte of memory so with all this talk of sizes and bytes and how much everything takes how how do we actually know how big a data type is it is dependent on the compiler after all is there somewhere we can check yes yes there is there's an operator we have available to us and zip applause called size of so if we come over here and we print size of wool for example we basically just type in the word size of and then either in brackets or not doesn't really matter although I do prefer to use brackets or parentheses I should say we type in our data type header five you can see it tells us that a bull is one byte if I replace this with int and hit f5 we have four and if I do something like double and hit f5 we have eight awesome critical stuff so that's basically all there is to variables or at least the primitive types that I've covered there are many different types that you can actually create in C++ and that have already been created for you however they're all custom types that are all based on these primitive types these are the building blocks that we use to define and store any kind of data we could possibly create now with any of these primitive data types we also have the ability to turn them into pointers or references pointers can be declared by writing an asterisk next to your type like this and references by an ampersand next year type pointers and references are such huge and complicated and vital topics that I really want to save them for separate videos so that you guys understand them properly so for now for this video we're just kind of stick with these primitive types make sure you understand them they're going to be the basis for pretty much every scene you ever write so they're really important but anyway I hope you guys enjoyed this video if you did please hit that like button you can also follow me on Twitter and Instagram and if you really like this video and you want to be a part of how to videos get made you want to contribute to the planning of these videos as well as receive early drafts of videos as I'm making them then please support me on patreon link will be in the description of this video your support there is what makes these videos possible thanks for watching guys I'll see you next time goodbye [Music] you
Info
Channel: The Cherno
Views: 340,263
Rating: undefined out of 5
Keywords: thecherno, the, cherno, project, thechernoproject, c++, how c++ works, learn c++, c++ tutorial, game, programming, development, engine, game programming, game development, how to make a game, tutorial, source, code, complete, game engine, how to make a game engine, opengl, glfw, C (Programming Language), variables, variables in c++
Id: zB9RI8_wExo
Channel Id: undefined
Length: 13min 46sec (826 seconds)
Published: Sun Apr 30 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.