Variables in C++

The Cherno
30 Apr 201713:46

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

00:00

πŸ“š 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++.

05:01

πŸ”’ 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.

10:03

πŸ”£ 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

Variables are fundamental to programming as they allow data to be stored and manipulated within a program. In the context of the video, variables are used to hold data such as a player's position in a game, which can be changed as the game progresses. The script explains that variables occupy memory and can be of different types, each with a specific size and purpose.

πŸ’‘Memory

Memory, in the context of the video, refers to the computer's storage where data is held. The script mentions that variables are stored in memory, specifically in the stack or the heap. Understanding memory allocation is crucial for efficient programming, as it affects the performance and resource usage of a program.

πŸ’‘Primitive Data Types

Primitive data types are the basic building blocks for data in programming languages, including C++. The video discusses several primitive types such as int, char, and double, each with a specific size and purpose. These types are essential for creating variables that can store different kinds of data.

πŸ’‘Integer

An integer is a whole number that can be either positive, negative, or zero. In the video, the integer data type 'int' is used to store values within a certain range, typically from negative two billion to positive two billion, depending on the size of the data type in memory.

πŸ’‘Signed and Unsigned

The terms 'signed' and 'unsigned' refer to whether a data type can represent negative numbers. A signed integer has a bit reserved for the sign, limiting the range of values it can store. An unsigned integer, on the other hand, uses all bits forζ•°ε€Ό values, effectively doubling the range of positive values it can represent, as explained in the video with the example of 'unsigned int'.

πŸ’‘Char

The 'char' data type is used to store a single character or a small number. In the video, it is mentioned that 'char' is traditionally used for characters, but it can also store numeric values. The script illustrates this by assigning both numeric values and characters to 'char' variables and discussing how they are treated during output.

πŸ’‘Float and Double

Floating-point numbers are used to represent decimal values in a program. The video distinguishes between 'float' and 'double', explaining that 'float' typically occupies four bytes of memory, while 'double' occupies eight. The difference in size allows 'double' to have a larger range and higher precision than 'float'.

πŸ’‘Boolean

A Boolean data type represents one bit of information, but it is stored in a byte of memory. In the video, 'bool' is used to represent true or false values, with true being any non-zero value and false being zero. The script explains that even though a Boolean value conceptually requires only one bit, it is allocated one byte in memory for practical reasons.

πŸ’‘Sizeof Operator

The 'sizeof' operator is used in C++ to determine the size in bytes of a data type or a variable. The video script provides examples of using 'sizeof' to find out the memory allocation for different primitive data types, such as 'bool', 'int', and 'double', which is crucial for understanding memory usage.

πŸ’‘Pointers and References

While not deeply explored in the video, pointers and references are mentioned as advanced concepts related to variables. Pointers are declared with an asterisk and hold the memory address of a variable, while references are declared with an ampersand and provide an alias for a variable. These concepts are foundational for more complex programming techniques in C++.

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

play00:00

hey what's up guys my name is a Cherno

play00:01

welcome to another video so I'm on the

play00:04

couch this time after a long day of work

play00:06

for I've mixed it up let's talk about

play00:09

variables in step above

play00:10

so when we write a program in C++ we

play00:14

want to be able to use data most of what

play00:17

programming is about is actually using

play00:19

data we manipulate data that's what we

play00:23

do so any kind of data that we use in

play00:24

our program that we want to change that

play00:27

we want to modify that we want to read

play00:28

and write from we need to store this

play00:30

data in something called a variable so

play00:33

variables basically allow us to name a

play00:35

piece of data that we store in memory so

play00:38

that we can keep using it as an example

play00:39

pretend that you're making a game and

play00:41

you've got a player in your game and

play00:43

that player character has some kind of

play00:46

position on the map and the player

play00:47

character can move so we need to be able

play00:49

to store the player's position as some

play00:51

kind of variable in our memory so that

play00:53

when it comes time to draw the player on

play00:55

the screen or interact with the rest of

play00:57

the level we can actually see hey where

play00:59

on earth is the player so we would want

play01:01

to store the players position in a

play01:02

variable this is basically one of the

play01:04

fundamentals of writing a program in any

play01:06

language we need to be able to play with

play01:09

data and store that data somewhere when

play01:12

we create a variable is going to be

play01:13

stored in memory in one of two places

play01:15

the stack or the heap don't worry we're

play01:17

going to have a lot of videos discussing

play01:19

how memory actually work so if you're

play01:22

looking for a more more of an in-depth

play01:23

kind of explanation that'll definitely

play01:25

come but for now just know that

play01:27

variables do occupy memory that's where

play01:29

we actually store the data in our

play01:31

computer's memory in C++ were given a

play01:34

bunch of primitive data types

play01:36

these primitive data types essentially

play01:39

form the building blocks of any kind of

play01:41

data we store in our program each data

play01:43

type that Super Plus gives us has a

play01:45

specific purpose whilst it has a

play01:47

specific purpose you don't actually have

play01:51

to use it for that purpose if

play01:52

interesting because if applause is a

play01:54

very powerful language which means there

play01:56

are actually very few rules when you

play01:58

actually get down to it so when I

play02:01

explain variables I like to say that

play02:03

really the only distinction between the

play02:05

different variable types you have in

play02:07

Super Plus is the size how much memory

play02:10

does this variable occupy when it comes

play02:13

down to it

play02:14

really the only difference between these

play02:15

primitive data types how big are they

play02:17

let's go ahead and jump into Visual

play02:19

Studio and take a look at some examples

play02:20

so we've actually already got a variable

play02:22

type that we're using here int in stands

play02:25

for integer and it lets us store an

play02:27

integer in a given range if we want to

play02:30

declare a brand new variable we can do

play02:32

so by typing the type of the variable

play02:34

giving it some kind of name for example

play02:37

variable and then giving it a value

play02:39

now this last part is optional you don't

play02:41

have to give it a value immediately but

play02:43

for now let's just give it the value

play02:44

eight an integer is the data type that

play02:46

is traditionally four bytes

play02:48

large the actual size of a data type

play02:50

depends on the compiler so it may be

play02:52

different depending on what compiler

play02:54

you're using ultimately it's the

play02:55

compilers choice to tell you how big

play02:57

your data is going to be the Imps data

play02:59

type is meant for storing integers in a

play03:02

certain range because it's four bytes

play03:04

large we are limited as to what kind of

play03:07

numbers we can store with it

play03:08

specifically this is something called a

play03:10

signed integer that can store a value of

play03:13

around negative two billion to positive

play03:15

2 billion anything larger or smaller

play03:17

than that is going to require more data

play03:19

to store than this int actually support

play03:22

so with 4 bytes of data we can store a

play03:25

value between this range so let's go

play03:27

ahead and try and print out our variable

play03:29

to the console to see what it actually

play03:30

is I'll substitute this hello world with

play03:32

this actual variable this is how we can

play03:35

log a variable to the console at nf5 to

play03:37

run our program and you can see it

play03:39

prints out the number 8 awesome we can

play03:42

go ahead and modify our variable for

play03:43

example by reassigning it to something

play03:45

else like 20 let's go ahead and print it

play03:48

again and see what happens so we're

play03:50

printing it once here and then again

play03:51

here so we should get the value 8

play03:53

printing first and then the value 20 and

play03:55

you can see if we run our program that's

play03:57

exactly what we get cool so as I said an

play03:59

inch data type can store a value between

play04:01

negative 2 billion and positive 2

play04:03

billion so you might be like why is it

play04:06

negative 2 billion and positive 2

play04:08

billion it's not exactly 2 billion by

play04:09

the way it's like 2 point something

play04:11

billion where are these limits coming

play04:13

from do they make any sense and the

play04:15

answer is yes they make sense they are

play04:17

directly tied with the size of the

play04:19

variable that is how much data were

play04:21

allowed to store in it an integer is 4

play04:23

bytes with 4 bytes of data we can store

play04:26

values in that

play04:27

let's break this down a little bit so

play04:29

one byte is eight bits of data which

play04:31

means that four bytes is 32 bits of data

play04:34

because this variable is signed meaning

play04:37

it can be negative it contains a sign

play04:39

like a negative sign because this

play04:41

variable is find one of those bits one

play04:44

of those 32 bits has to be for the sign

play04:46

so that we know if it's positive or

play04:48

negative which only leaves 31 bits left

play04:51

for the actual number now a bit can

play04:53

either be 0 or 1 so there are 2 possible

play04:55

values for 1 bit of data so using a

play04:57

little bit of maths here we can say that

play04:59

we have 31 bits to play with 2 possible

play05:01

values took bit so what is 2 to the

play05:03

power of 31 if we crack open a

play05:05

calculator here and type in 2 to the

play05:07

power of 31 we will get about 2 billion

play05:10

that value there that 2.1 billion that

play05:13

is the maximum number that we can store

play05:16

with an integer now remember we also

play05:17

have one bit that is reserved for

play05:19

whether or not that number is negative

play05:21

so because of that we can store up to

play05:22

that number from 0 but also we can go

play05:24

the other way and store all the negative

play05:26

values down to negative 2 by 1 billion

play05:28

but I don't want negative values I hear

play05:31

you say is there a way to just get rid

play05:33

of that one bit being for the negative

play05:34

sign and just use it as part of my

play05:36

number why yes yes there is that is what

play05:40

we call an unsigned number that means

play05:42

it's the number that does not have a

play05:43

sign meaning is always positive in C++

play05:46

we can make one of those by just typing

play05:49

in unsigned in front of our integer so

play05:51

now what we've done is we have 32 bits

play05:53

to play with and 2 to the power of 32 of

play05:56

course is double what we have here for

play05:59

point two nine billion and that's

play06:01

basically what the unsigned keyword does

play06:03

in Sabre slot it lets us define an

play06:06

integer that does not have a sign bit

play06:08

okay so what other data types do we have

play06:10

available to us what if I don't want to

play06:11

for byte integer what other types are

play06:13

there so as far as integer values goes

play06:15

we actually have quite a few we've got

play06:17

char which is one byte of data we've got

play06:19

short which is two bytes of data we have

play06:22

int which is four bytes of data we have

play06:24

long which is also usually four bytes of

play06:27

data depending on the compiler and then

play06:29

we have long long which is usually eight

play06:32

bytes of data there's also other types

play06:34

like long and there are a few different

play06:35

modifications I'm not going to go

play06:37

through all of them but

play06:38

basic ones are these five you can also

play06:41

add on signs to any of these and it will

play06:44

remove that sign bit and let you set a

play06:45

larger number chart traditionally is

play06:47

also used for storing characters not

play06:49

just numbers so above I'm assigning

play06:51

numbers to it like 50 you can also

play06:53

assign characters to it like a now

play06:55

that's not to say you can't assign

play06:56

characters to other integers you can

play06:58

because at the end of the day this

play07:00

character that I five to this letter a

play07:01

is just a number in fact that number

play07:04

that numeric value associated with that

play07:06

character the character a is 65 now if

play07:10

numbers are just characters and if

play07:11

characters are just numbers then why

play07:13

exactly do we have this distinction why

play07:16

do I say that char is specifically used

play07:18

for characters whereas it's really not

play07:20

that is because we often as programmers

play07:22

make assumptions about certain data

play07:24

types if I pass in a char and call it

play07:27

something like character I usually

play07:29

expect you to actually assign a

play07:30

character to it so a good example of

play07:32

this is if you actually try and print

play07:33

out a char if I print out this variable

play07:36

a for example and I hit f5 I'm not going

play07:39

to get the number associated with it I'm

play07:41

going to get the character a printed out

play07:43

so if I replace this with this actual

play07:45

numeric value like 65 I'm also going to

play07:47

get the value a printed out as you can

play07:49

see over here because C out if I pass in

play07:52

a char into C out is going to treat it

play07:55

like a character not like a number if I

play07:57

change it to be some other type like a

play07:59

short for example and hit f5 you can see

play08:02

that C out no longer treats it like a

play08:04

character it's going to actually print

play08:05

out the numeric value and even if I

play08:07

assign a character here it's just really

play08:09

assigning the value 65 so if I run this

play08:12

again you can see that we get 65 so the

play08:15

reason I'm telling you all this is

play08:16

because I want you to understand that

play08:17

data types the usage of data types is

play08:20

just up to the programmer really there

play08:23

are certain conventions that we have in

play08:25

place but there's nothing concrete that

play08:27

you have to actually follow there are

play08:29

very little rules and c++ after all so

play08:32

because of that I do want you to realize

play08:34

that the only real difference between

play08:35

these data types is how much memory will

play08:38

be allocated when you create a variable

play08:40

with that data type

play08:42

so with those integer types aside what

play08:44

if I want to store a decimal value for

play08:46

example five point five how do I do that

play08:48

well for that we have two data types we

play08:50

have float and we

play08:51

have double there are also some modifies

play08:53

that you can do like long double we're

play08:55

not going to get into those so a float

play08:57

is basically a decimal value that we can

play09:00

store that occupies four bytes of data

play09:02

so let's define a variable here such as

play09:04

5.5 how do we do that

play09:06

let's also replace this variable a we're

play09:08

printing out our float variable and

play09:09

compile our file

play09:10

let's hit f5 to run our program and you

play09:12

can see we get 5.5 printed out fantastic

play09:15

now you may think that you've defined a

play09:17

float here but you actually haven't

play09:19

you've actually defined a double if we

play09:22

go back to visual studio and we hover

play09:23

our mouse over this value you can see

play09:25

that in brackets it says double as I

play09:26

just mentioned we have two different

play09:29

variables that we can use to store

play09:30

decimal numbers float and double so how

play09:33

do we discern between what a double is

play09:35

and what a float is the way we do that

play09:37

is by basically appending an F to our

play09:40

float variables it can be lowercase or

play09:42

uppercase doesn't matter but the point

play09:44

is if we have an F you can see that

play09:46

we've actually declared a flirt so

play09:48

floats are basically four bytes large

play09:50

and doubles are eight bytes large

play09:53

finally we have one more primitive

play09:55

datatype to play with and that is bull

play09:57

now bull stands for bullying and it can

play09:59

either be true or false if we try and

play10:03

print it to our console and hit f5 you

play10:05

can see that we'll actually get a

play10:06

numeric value one because of course

play10:09

there's no such thing as true or false

play10:11

those are English words computer deal

play10:14

with numbers so basically zero means

play10:16

false and anything except zero any other

play10:19

number means true in this case we'll

play10:22

actually get one printing to the console

play10:24

indicating that it is true if we change

play10:26

the default and run our program we will

play10:29

get 0 which means false the bull' data

play10:31

type occupies one byte of memory now you

play10:34

might be wondering one byte why the bull

play10:37

can either be true or false

play10:39

surely that only takes one bit to

play10:41

represent and you are correct it does

play10:43

take one bit to represent however when

play10:45

we're dealing with addressing memory

play10:47

that is we to retrieve our ball from

play10:49

memory or stored in memory there is no

play10:51

way for us to actually address

play10:53

individual bits we can only address

play10:55

bytes so because of that we can't

play10:57

actually create a variable type that is

play10:59

one bit because we'd need to be able to

play11:01

access it and we can't we can only

play11:04

access by now course one thing you could

play11:06

do on the other hand is be really smart

play11:08

about how you store data and store eight

play11:10

all in one bite that's totally okay one

play11:14

bit per bull but you still have that one

play11:16

byte of allocating memory we'll probably

play11:19

talk about advanced fun tricks like that

play11:21

in the future but for now a bull is one

play11:24

byte of memory so with all this talk of

play11:27

sizes and bytes and how much everything

play11:30

takes how how do we actually know how

play11:33

big a data type is it is dependent on

play11:35

the compiler after all is there

play11:37

somewhere we can check yes yes there is

play11:39

there's an operator we have available to

play11:41

us and zip applause called size of so if

play11:43

we come over here and we print size of

play11:46

wool for example we basically just type

play11:48

in the word size of and then either in

play11:50

brackets or not doesn't really matter

play11:52

although I do prefer to use brackets or

play11:55

parentheses I should say we type in our

play11:57

data type header five you can see it

play11:59

tells us that a bull is one byte if I

play12:01

replace this with int and hit f5 we have

play12:04

four and if I do something like double

play12:06

and hit f5 we have eight awesome

play12:09

critical stuff so that's basically all

play12:12

there is to variables or at least the

play12:14

primitive types that I've covered there

play12:16

are many different types that you can

play12:17

actually create in C++ and that have

play12:19

already been created for you however

play12:21

they're all custom types that are all

play12:23

based on these primitive types these are

play12:25

the building blocks that we use to

play12:27

define and store any kind of data we

play12:30

could possibly create now with any of

play12:32

these primitive data types we also have

play12:34

the ability to turn them into pointers

play12:35

or references pointers can be declared

play12:38

by writing an asterisk next to your type

play12:40

like this and references by an ampersand

play12:44

next year type pointers and references

play12:46

are such huge and complicated and vital

play12:49

topics that I really want to save them

play12:51

for separate videos so that you guys

play12:53

understand them properly so for now for

play12:54

this video we're just kind of stick with

play12:56

these primitive types make sure you

play12:57

understand them they're going to be the

play12:59

basis for pretty much every scene you

play13:01

ever write so they're really important

play13:03

but anyway I hope you guys enjoyed this

play13:05

video if you did please hit that like

play13:06

button you can also follow me on Twitter

play13:09

and Instagram and if you really like

play13:11

this video and you want to be a part of

play13:13

how to

play13:13

videos get made you want to contribute

play13:15

to the planning of these videos as well

play13:17

as receive early drafts of videos as I'm

play13:20

making them then please support me on

play13:22

patreon link will be in the description

play13:24

of this video your support there is what

play13:26

makes these videos possible thanks for

play13:28

watching guys I'll see you next time

play13:29

goodbye

play13:33

[Music]

play13:43

you

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
C++VariablesData TypesMemoryIntegersCharFloatDoubleBooleanPointersReferences