Learn C Programming on Raspberry Pi - 06 - char and int Varial Types

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video we're going to start to talk a little bit about variables in C programming and what variables are are there they are spaces in memory that hold values that we can change and manipulate in the programs to do useful things in this video we're going to just talk about a couple of the basic ones the char and the end so let's go ahead and get started here we've written a program that's going to both demonstrate the use of variables as well as find the minimum and the maximum value that a variable can hold experimentally with a function I'm not going to go into functions in this video and we're just going to talk about the variables themselves so we're going to start down here and I will go over the functions in a future video but right now this is how we create a variable first we need to identify the variable type so in this case it's a char or also perhaps a car it's a short for character and it is this variable type is one byte in size and one byte is equal to eight bits and if you're not quite sure what that is and basically computers speak really one language binary ones and zeroes and so this is a series of eight ones or ones or zeros and really when you get down into the hardware level it's really high and low voltages those ones and zeros are used in programs to represent high and low voltages on the hardware and what a variable is is it's a space and memory and memory is a series of high and low voltages and so each bit represents a higher low voltage at a point in memory and a char is eight bits and you could always if you want to calculate how how many different very values are possible in a datatype if you know how many bits are in it you can raise that two to the power of a number of bits and now I'll tell you how many how many possible values there are so a two to the power of eight is 256 so HR can hold 256 different values and so remember 2 raised to the N for later all right and so the maximum value for this and this is a unsigned variable type so there's it's only positive numbers and it goes to 255 now wait a minute I just set up here it can hold 256 values so why is the maximum value 255 well the reason is because we start counting at 0 so it's it's any value from 0 to 255 all right so getting back to how you declare this first week we defined type net that reserves that the the correct amount of space in memory and then we give it any name we like well that's kind of true we can use any letter number combination or underscores we can't use other special characters and it needs to begin with either a letter or an underscore so as long as you follow those rules we can use any name we like and one convention that I'm using here is a camel type our camel case where the first first word is all lowercase letters and then eats each subsequent word is capitalized and all squeezed together like this now another way to do this this make a stand out would be to do this and put underscore in between each word and that allows us to use a multiple word name I'd it was something that is meaningful to us so so here I'm making it one byte max because I know this is a byte one size the size of this is one byte so it's 1 byte equals 255 and I put any number I want in there but it's for now I know that the maximum value of this is 255 but I'm gonna get risk with my program won't work if I leave those in there all right and so at the this in this method this way of doing it we're assigning the value to this space in memory given this name at the time that we're actually creating it and every line in NC ends in a semicolon here or just like you see here another way to do this is we can reserve the space in memory and first without assigning a value and then some assign the value later in this case I'm calling this one one byte min and I'm not assigning the value here I'm actually down here when I'm assigning it down here now notice that what once it's declared with the the type here I don't have to use this anymore I can just use the name so on this line I'm just using a name one byte man and then I'm using the equal sign to assign the value 0 to that space in memory ok so that's how we create a variable down here I'm I'm just creating another one and I'm calling another bite and we can also assign the values of other variables to our variable so here I've declared it here now I've assigned a value to it down here and it's equal to 1 byte max which is up here divided by 2 now I could write 255 divided by 2 here and it would do the same thing so because this just 255 is assigned of this this right here is is the same as 255 divided by 2 and then that's a sign to the disk space in memory that's called another bite ok and when we run this you're going to see something interesting here and we'll get to that when it happens now I was going to go over string and a raise in this video but it got really long so I'm gonna skip over us for now just right here in these lines if you're wondering what they are I'm just creating some strings which are our are basically how you write words and again I'm not gonna go into this in depth this is for another video but here is where I'm actually going to print the values to the screen then I created up here so on this line here I'm going to print the the value of one bite Max and on this line here I'm going to print a value of one bite min and then down here I'm going to print the value of another byte so it should be 255 0 and 255 divided by 2 okay on these two lines I'm actually going to do something called it's very very similar syntax here but instead of inserting the variable name here and notice up here I am using a variable name and that's that's the nice thing about variables anytime we want to access that value we just need to remember what the name is and it does need to match exactly it is case sensitive that was something I forgot to mention earlier so it has to be exactly the same as I use it there it won't work but down here instead of putting that variable name I'm calling a function and we'll talk about functions later but basically what a whatever you stick a function if it returns a value it's gonna stick that value right right in its place so that's what this is going to do this function is gonna go run some some some functions and it's going to find what the maximum value of this data type is and it's gonna it's kind of it's gonna print on this line right here and then down here this function is going to go find the minimum value of this data type and print up here so we're going to actually experiment test the minimum and maximum values for his data type or its variable type okay printed a screen now I said it was to the max was up here I said the max is 255 time in of zero so we'll just see if it works out be right to sing there all right now the int or short for integer now here I am creating it again I'm giving it a saying what kind of variable it is in this case is an int and I'm calling it whole number max and I'm making equal to this int underscore Max and you might wonder what that is and what that is is that that's a constant from a library it's from the limits H library and I could go showed it to you but just know this is a this is a constant and it's it's traditionally a good practice to make all your constants in all caps like this and we'll cover constants after we cover variables but int max is a value stored in a library and if you have watched my video in libraries you can go go look at that find out what libraries are and it just is just going to load that value here so whatever that library thinks is the maximum value for an integer it's going to place that here and I'm doing the same thing down here as I did above I'm declaring it first and I'm signing the value I'm signing the int min value to it down here now this is let me only pause this for one second here and an int is assigned value there's a signed data type mean it can have both a positive and a negative value and so down here we should see a negative number and up here we should see a positive number if we want to just use positive numbers we can use an unsigned int and then so I'm calling this unsigned int positive integer max and the reason why we might want to use an unsigned instead of a sign is we can have the maximum value can be twice as big if we have negative numbers we'll have those negative half of the numbers are going to be negative half the numbers are going to be positive if we can only have so many values so that maximum value is lower than unsigned value variable type so they're there we have three different type variables there they were going to get printed to the screen down here okay and I'm going a little bit more about input/output like this later and then again dad here I'm going to call some functions defines the minimum maximum values experimentally so here this function is going to go find the maximum value of an integer experimentally and return it and it's gonna print on the screen and it's gonna take a little while and you'll see and then and on down on this line is going to find a minimum value integer and printing here and remember that should be a negative number for an unsigned integer I don't need to find the minimum because I know it's zero zero is going to always be the minimum for an unsigned number and then but we are going to find the maximum value it now this number should be about twice as much as this number here so let's go ahead and run this program and we'll see see what it does bird compile and and build now I'll go ahead and run okay so you can see that it it a Pyrenees lines this is it the program printing those values to screen these are exactly the values that I entered when I declared this variable type okay so 255 0 and 127 now wait a minute 255 divided by 2 is 127 and a half well the reason why it just gives 127 is these these two variable types that we're working with the char and int are whole numbers only so it they cannot hold fractional numbers of decimal places and when you just do this it's going to give you the lower lower value so in the case of 120 7.5 you're just going to get 127 you need to keep that in mind when you're dividing with whole number values like this and in the next video we'll talk about the float and the double which are floating point variables and those are that's where we have decimal places okay so down here the program found the minimum maximum value experimental e you can see that we were correct the maximum value is 255 and the minimum value is 0 and it did it relatively quickly because they're small values and it didn't take long to find them now you down here these three values are printed directly from the values we entered any they showed up pretty quickly you see a positive number for the maximum of an int and I'm negative over for the minimum value I'm not gonna read it out for you that's a big number and then you can see that an unsigned int is twice as big as the int now down here you can see two lines have showed up already but it if you've been watching they've been just popping up real slowly and that's because the function to it that's how long it takes to count the distant alyou and that's actually what the function is doing is its counting and it's it's iterating that value until it does something called rollover and I'm going to demos demonstrate rollover here as soon as we're done with this this right here but basically it's counting to the maximum value that our minimum value and then reporting it here and it's still trying to find the maximum value of unsigned int and that's gonna pop up in just a second see 19:44 is a time I might just pop a positive video and then we'll cut I'll start it back up when this shows up all right so it only took maybe another 30 seconds or so but I clipped out a few seconds so you didn't have to sit here and just listen listen to silence but you can see that the pie in your activity bar here pile is running at max capacity while I was finding that value and it took quite a while to count that high and now it's it's relaxed a little bit so because the program is finished well go ahead and hit enter to exit out of this now I'm gonna tell you about a little bit how rollover works but it's because the reason why we need to know how large our data types are or minimum maximum values are so we do kind of avoid accidental rollover sometimes it's it's or overflow I'm sorry I sometimes it's overflow is useful and we'll use intentionally sometimes but if it happens unintentionally you'll get unexpected results in your program might crash or just give you wrong values up here I just I had this line comment it out and that's something that's common to do in programming that we can comment a line they add it or remove it to our program so we're gonna add it back in here and all I'm doing here is I'm changing the value of one bite max now this might seem strange but you can do math on a value and then assign the new value back to itself and this is a way to do that now remember we said the maximum value of one bite max as if this type is 255 so what happens if we add 1 well I can tell you that it's gonna go to the minimum value because it's gonna roll over or it's gonna overflow and and so let's rerun this program and see what happens to this message okay now remember down here one byte max is gonna get printed as screen and and so instead of it show up as 255 it's gonna show up as 0 and I'll show you that in just a second there you can see it now says the maximum value of a char is 0 well that isn't right that's because it it rolled over or it overflowed from its max value to its min value and that's kind of like something just something you need to be aware of and that's the reason why we're finding these values now now why did I really have to do this experimentally well the reason is that there are some some standards for how large DS these different data types can be or should be but they're not always followed and I've worked in some environments with some tools that might have different rules for these sizes and so if you're working with a an IDE or a compiler or different operating system or different different something different factoring or your environment it's a good idea to try to figure out your your actual minimum maximum values experimentally just to make sure they're what you think they are all right so I'm not going to make you sit through this again that's about it for this video in the next video we'll talk about the floating point type variables if you like this video please click like it motivates me to make other videos just like it if you want to find out when I post more videos please subscribe you can also read about completed projects on our website net or calm on our website we include details of how we did some of the projects that are on these videos and some of the source code that you can use yourself as much as you like if you want to track progress of projects we have going on step by step be sure to check out our pages on Twitter and on Facebook
Info
Channel: SpazzTech
Views: 13,274
Rating: undefined out of 5
Keywords: Tutorial, C (Programming Language), Integer, Data Type, Programming Language (Software Genre), Raspberry Pi (Computer), Software (album), Help, Tips, Tricks, Learn, Howto
Id: iEAaodL33ys
Channel Id: undefined
Length: 20min 3sec (1203 seconds)
Published: Fri Jan 30 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.