Variables in C++
Summary
TLDRIn this video, Cherno explores the fundamentals of variables in C++ programming. He explains how variables are used to store and manipulate data, highlighting the importance of understanding memory allocation in the stack or heap. Cherno covers various primitive data types like int, char, float, and bool, discussing their memory sizes and typical use cases. He also touches on the concept of signed and unsigned integers, and the significance of data type sizes in programming. The video is an informative introduction to variable usage in C++, setting the stage for deeper dives into pointers and references in future episodes.
Takeaways
- 📝 Variables in C++ are used to store data that can be manipulated, read, and written in a program.
- 💾 Variables are stored in memory, either on the stack or the heap, with the exact location depending on the programming context.
- 🔢 C++ provides primitive data types which are the building blocks for storing data, each with a specific size determining how much memory they occupy.
- 👉 The 'int' data type is typically four bytes in size and can store integer values ranging from approximately -2 billion to 2 billion.
- 🔄 The size of data types can vary depending on the compiler used, but the 'int' type commonly represents a signed integer with a 32-bit size.
- ➕ By using 'unsigned' before an integer type, you can create an unsigned integer that can store larger positive values without a sign bit.
- 🔤 The 'char' type is traditionally one byte and used for storing characters, but it can also hold numeric values corresponding to ASCII codes.
- 🔢 Other integer types include 'short', 'long', and 'long long', each with varying sizes and capable of holding different ranges of integer values.
- 📉 For storing decimal values, C++ offers 'float' and 'double', with 'float' being four bytes and 'double' eight bytes, and 'double' being the default for decimal values if not specified.
- 🔴 The 'bool' type represents Boolean values and occupies one byte, using 1 for true and 0 for false, despite the actual value only requiring one bit.
- 🔍 The 'sizeof' operator in C++ can be used to determine the size in bytes of a data type or a variable.
- 🔑 Pointers and references are advanced features in C++ that allow for more complex data manipulation, but they are covered in separate videos due to their complexity.
Q & A
What is the primary purpose of variables in programming?
-Variables are used to store data in memory so that it can be manipulated, read, and written to as needed during the execution of a program.
Where are variables typically stored in a C++ program?
-Variables in C++ can be stored in either the stack or the heap, depending on their declaration and usage.
What is the difference between a signed and unsigned integer in C++?
-A signed integer can represent both positive and negative values, while an unsigned integer can only represent non-negative values. The signed integer uses one bit for the sign, limiting the range, whereas an unsigned integer uses all bits for the value, effectively doubling the positive range.
What is the significance of the 'int' data type in C++?
-The 'int' data type in C++ is used to store integer values and typically occupies four bytes of memory, allowing it to store values from about negative two billion to positive two billion.
How can you determine the size of a data type in C++?
-You can use the 'sizeof' operator in C++ to determine the size of a data type, which will return the number of bytes allocated for that type.
What is the purpose of the 'char' data type in C++?
-The 'char' data type in C++ is typically used for storing characters, but it can also store integer values. It occupies one byte of memory.
Why does the 'bool' data type in C++ occupy one byte of memory?
-The 'bool' data type occupies one byte of memory because memory is addressed in bytes, not bits. This allows for easier memory management and access, even though a boolean value technically only requires one bit.
How do you declare a variable of type 'float' in C++?
-To declare a variable of type 'float' in C++, you append an 'f' or 'F' to the numerical value, such as '5.5f', to indicate that it is a floating-point number of type 'float'.
What is the difference between 'float' and 'double' in C++?
-A 'float' in C++ typically occupies four bytes and has a smaller range and precision compared to a 'double', which occupies eight bytes and offers a larger range and higher precision.
What are pointers and references in C++, and how are they declared?
-Pointers in C++ are declared with an asterisk '*' next to the data type, and references with an ampersand '&'. They are advanced features used for direct memory access and aliasing variables, respectively.
Outlines
📚 Introduction to Variables in C++
In this first paragraph, Cherno introduces the concept of variables in C++ programming. He explains that variables are essential for storing data that can be manipulated within a program. Using the analogy of a game where a player's position needs to be tracked, Cherno illustrates the need for variables to hold changing data. He also touches on the fact that variables are stored in memory, either on the stack or the heap, and introduces the idea of primitive data types, which are the basic building blocks for storing data in C++.
🔢 Understanding Data Types and Memory Allocation
The second paragraph delves deeper into the different primitive data types available in C++, such as 'int', 'char', 'short', 'long', and 'long long', and their respective sizes in bytes. Cherno explains the significance of the 'signed' and 'unsigned' modifiers, which affect the range of values a variable can hold. He also demonstrates how to declare and manipulate variables in C++, including printing them to the console and understanding the limits of integer values based on their size in memory.
🔣 Exploring Non-Integer Data Types and Boolean Logic
In the third paragraph, Cherno discusses non-integer data types, including 'float' and 'double', which are used for storing decimal values. He clarifies the difference between 'float' and 'double' by pointing out the suffix 'F' used for floats. Cherno also introduces the 'bool' data type, which represents Boolean values and occupies one byte of memory. He explains the reason behind this allocation, despite Boolean values theoretically requiring only one bit, due to the way memory addressing works in computers. Finally, he mentions the 'sizeof' operator, which can be used to determine the size of a data type as determined by the compiler.
Mindmap
Keywords
💡Variables
💡Memory
💡Primitive Data Types
💡Integer
💡Signed and Unsigned
💡Char
💡Float and Double
💡Boolean
💡Sizeof Operator
💡Pointers and References
Highlights
Introduction to variables in C++ programming, emphasizing their importance for data manipulation.
Explanation of how variables allow us to store data in memory for later use, using the example of a player's position in a game.
Discussion on the two possible memory storage locations for variables: the stack and the heap.
Introduction to primitive data types in C++ and their role as building blocks for data storage.
Description of the 'int' data type, its typical size, and the range of integer values it can store.
Demonstration of how to declare and initialize an integer variable in C++.
Explanation of the size limits of 'int' and how they are derived from the number of bits used for storage.
Introduction of the 'unsigned' keyword to create a non-negative integer variable.
Overview of other integer data types such as 'char', 'short', 'long', and 'long long', and their respective sizes.
Clarification on the use of 'char' for storing characters and its numeric representation.
Explanation of how to print the character or numeric value of a 'char' variable in C++.
Introduction to floating-point data types 'float' and 'double' for storing decimal values.
Differentiation between 'float' and 'double' based on the number of bytes they occupy.
Discussion on the 'bool' data type for storing boolean values and its memory size.
Introduction of the 'sizeof' operator to determine the size of a data type in C++.
Emphasis on the importance of understanding primitive data types as the foundation for all C++ programming.
Teaser for future videos on advanced topics like pointers and references in C++.
Conclusion and call to action for viewers to support the channel for more educational content.
Transcripts
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
5.0 / 5 (0 votes)