In this lecture, we'll learn how we can represent and use integers in C. Integers are those numbers that have no fractional or decimal part. They are whole numbers like 0, and 42, and -11. There are a number of different data types in C that we can use to represent whole numbers, the ones that we'll discuss in this part of the lecture are short, int, and long long. I'll tell you that throughout this course, we're pretty consistently going to use int. What are the differences between those three datatypes? The difference is that there are a different number of bits used to represent each datatype. If we have a variable that we declared to be of type short, there are 16 bits of memory reserved for that variable, 2-byte. Int have 4-byte and long longs have 8-bytes, so there are different numbers of bits. Big deal, what does that tell us? Well, because we know that two to the b equal n, that tells us that if b is smaller n is smaller, so the short datatype, a variable of that type can store a smaller range of whole numbers than int can, and int stores a smaller range of whole numbers than long long can. This two to the b equal n thing really matters. Now, if you're in a memory-constrained environment, you might decide to use short rather than int for example, but you would have to know that the numbers that you are storing fall within the range that we can represent with 16-bit. As I said in this course, we're pretty much going to use int. The operations in variables are pretty much what you'd expect, except maybe for division. Let's actually go take a look at how we can use int in RC programs, and we'll explore how division works for ints as well. I've already started up Visual Studio. Remember, to create a new project, we come down here on the lower right, and click create a new project. We're going to select "Windows desktop wizard" almost at the bottom and say "Next". Change the project name, I'll call mine integer data types. You can change the location that you're storing your project at if you want to, and say create. Remember, for C projects we're going to click the empty project checkbox and say "Okay". Because we selected an empty project, we need to add our main.cfile. I'm going to do that here, and of course I'm going to copy in my template code as well. The block comment that I'm going to add above the main function says, it demonstrates integer datatype. Let's solve a particular problem. Let's say we have a movie, and we know how many minutes that movie is. I'm going to declare an integer variable, holding how many minutes the movie is. To declare a variable, we start with the datatype and this isn't int, then we give it a variable name. I will call this variable total minutes. Now optionally, we can assign a value to this variable. If we choose not to do so we just put a semicolon here, but if we already know the value of the variable, we can just assign it. I'll say this movie is a 113 minutes long. What I'd like to know is how many hours, and how many minutes is this movie. Most normal human beings would just calculate that in their head and tell you, but if you're really geeky and you want to own your C programming skills, then you're going to actually write a program to do that and that's what we're doing here. I'm going to add a line comment that says calculate and print hours. I'm going to declare another variable, this one called hours, and now I need to calculate how many hours the movie is. I can do that by taking total minutes, and dividing by 60. Now, you might think that this will give us a floating point number right because this is one point something hours, but that's not how integer division works in C. Integer division works like you might have learned in third grade, right, where you get a whole number result, and you get a remainder. That's how integer division works. This will in fact give us a whole number, the whole number of hours there are. Now, having 60 here is not a great idea because 60 is what's called a magic number. It has semantic meaning. It means, how many minutes there are in an hour. We might have other instances of the number 60 throughout our code, and every time we see 60 we would have to think about well which 60 is this with this special meaning. So instead of using 60, we're going to declare a constant. Remember, from last lecture, we do that using pound defined, a preprocessor directive, and we give it a name 'minutes per hour', and we give it a value 60. This is called a macro, and anytime the preprocessor sees this in our code, it will replace it with this instead. So ultimately this will be 60 even if I change it to minutes per hour here. But this makes our code more readable for humans which is always a good goal for us, but it still has the same exact behavior once we've compiled it and run our code. We went to print the hours as well. We'll use the printf function like we did with our message, but we're going to do it slightly differently here. We certainly will have a string literal, and we'll label our output instead of just printing out numbers, we'll actually say what those numbers mean, but the next thing we need here is something called a format specifier. We're about to print a number, we're about to print the number of hours, and we want to use the format specifier that tells us the printf function that we want this to be printed out as an integer. The format specifier for an integer is percent D. So we're going to use percent D to specify the format of that numeric output. The next thing we include, we put comma. We need to say, well, what are you going to format as an integer, and we put to the variable. Of course before we run this, I realized that I shouldn't have typed 113 here, I should've typed total minutes instead, because we went to the trouble of declaring this variable to hold those total minutes so we should just use that variable here. I'll "Control F5" to run our code, and as you can see, it says one hour as expected. Let's go ahead and calculate and print the minutes as well. I will declare another integer variable, this one called minutes. This is going to be total minutes. We don't want to divide here, we need to get it the remainder. Even though mathematically there's a difference between modulus and remainder, they're the same for this particular chunk of code and most of the things you'll do in C. So I'm going to use the modulus operator which is the percent sign, and again, it will be remainder of total minutes divided by minutes per hour. We'll print that out as well. Again with a format specifier saying I want to do an integer, and I'll put the name of the variable. When I "Control F5", we're actually getting the right answers here. It's one hour and 53 minutes, but I don't like that these are all mashed together on the same line. I'm going to do an escape sequence like I showed you last time, with a backslash n, and I'll put that after each of these. That way when I "Control F5", I actually get the output on separate lines. That's great. That actually concludes the example. We learned a lot of things here. We learned how to declare a variable by putting the data type followed by the variable name, we saw that we could also optionally when we declare a variable give it a value. We saw how to declare a constant using the pound define preprocessor directive, we saw how to use a format specifier to format our numeric output, and we were reminded how to use escape sequences as well. First thing to note that in C if we have an int variable with a value of one and we add one to it, then the new value of that variable is two. You should be excited and proud about this because you're taking an online course with a college professor, and you've just learned after several lectures that one plus one is two. That's not the interesting part. The interesting part is if we have an int variable with a value of 2,147,483,647, and we add one to it, the new value is negative 2,147,483,648. The question is why does this happen? Well this is actually a direct result of two to the b equal n. It turns out that 2 billion and change number, is the largest number that we can represent using 32-bits. The largest positive number we can represent if we're going to split the integers half negative half positive. We can't go any higher than that number. Though it doesn't work exactly this way, you can think of this as if we add one to that super big number, then our carry bit falls off too far on the left goes into the 33rd bit and we don't have enough room for it. We end up actually wrapping around to the minimum negative number that we can represent with 32-bits. It doesn't work exactly that way. If you want to learn how it actually works you should go explore something called two's complement arithmetic, and how that works. I'll actually upload a document that describes why that happens. It is a direct result of two to the b equal n. That might've felt theoretical when we were talking about two to the b equal n, but this really matters in practice. Let's say you didn't actually want to store any negative numbers. It turns out that C does have datatypes for unsigned integers, and they're only positive numbers, including zero. What does that bias? It means that if we have 16-bits, and we're only going to store positive numbers, then we can store a positive number that's twice as high as we could have if we were using those 16-bits to store both positive and negative numbers. If we know our numbers are only going to be positive, it buys us a larger range of positive numbers. Those datatypes are unsigned short, unsigned int, and unsigned long long. We will only use signed numbers in this particular course, but if you discover you need just to store positive numbers, you can use these unsigned datatypes instead. To recap in this lecture, we learned how to store and use whole numbers, integers, in RC programs.