A const int is not a constant.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so is that a constant you sure you sure that's a constant or is it a const int hey folks today we're talking about why const int is not a constant at least not in c [Music] hey folks welcome back today i want to talk about constance which is something i've touched on a little bit here and there in some of my videos but today i just want to point out something that i think is really counterintuitive about c and something that can be potentially confusing to new programmers or some of you not so new programmers who just haven't looked too deeply into it how c handles the const keyword and it really all boils down to what it actually means when we declare one of our integers or one of our variables as const now before i get too far in i do want to say a big thank you to all of you who make this channel possible those of you who like who subscribe who support this channel on patreon if you're liking the content you're seeing here if you are getting something out of it if it is helping you please consider supporting this channel in some way also on patreon you can get access to source code from all my videos including this one and you can get access to my monthly office hour so thank you so much now back to const now i think this is going to be easiest if we look at it through an example now say i have a new program here now this program doesn't do anything yet it's empty it just has some includes and a main function but i have a make file here that's going to compile it nothing complicated there check out my make videos if you haven't seen make but i can come down here and i can compile it and i can even run it and of course like i said it doesn't really do anything yet but we're going to use this as a canvas to explore the const keyword just a little bit so now let's just say that i as i envision my program what it's going to do i know that i'm going to want some constant let's call it a so i'm going to come up here and let's say that i one way to do this is to say pound define a and let's say i know it's going to be 10. now in the past i've talked about how we can declare a constant like this using the preprocessor using pound define now this is basically going to copy and paste a 10 in my code anywhere that i use the name a and we'll leave this in here as a comment but the other way that we commonly declare some kind of constant in our program sometimes is by saying const int a equals 10 like this now the advantage i really like this approach the advantage is that this allows the compiler to do a bunch of checking it gives a a type an a is now an int and so the compiler can check to make sure that it makes sense to have an in different places and that allows the compiler to do a bunch of things for us that we can't do if we're using pound define so why not use this one all the time of course some people do use this approach all the time and that's fine as long as you know what you're getting yourself into because if you do you're probably going to eventually discover that const int is not actually declaring a constant but you may wonder why not well this is in my opinion one of the more frustrating things about c see the const keyword looks like constant it's clearly related to the word constant i mean const constant but const in c actually means something else in c const int simply means i want an int variable that is read-only after i initialize it okay so i initialize it up here to be 10 right but now if i jump down into main now if i say something like a equals 12 it is not going to let me do this you don't believe me if i come down here and i say make now it's going to say you are assigning a read-only variable a to a new value that's not cool but you say okay read-only constant isn't that the same thing well it's close but there are some occasionally annoying differences so one difference is let's comment this out really quick one difference is that we could come down here and let's say i decided i wanted to print out the address of a that's something i couldn't do if i were using pound define pound define doesn't declare a variable there's no address of it but i can say let's say address of a equals let's print out a pointer and say sorry i need quotes and then let's come down here and just say address of a and like i said with pound define you couldn't do this because it just inserts the literal the integer literal 10 in this position and you can't take an address of an integer literal it's just not a meaningful thing to do but let's just make sure that this actually works i can come down here and compile it compile just fine and now you see i can actually print out the address no problem and of course because we can get the address we could use pointer tricks to actually change the value of a let me know if you'd like to see that in a future video it's not something you would normally want to do but of course that hasn't stopped us in the past and most of our compilers actually let us do it even if it doesn't make any sense so in this video i'm just gonna stick to stuff that you might actually do someday and so what if you did something like this say i wanted to come up here and i want to have another constant another const int we're going to call it b and let's say that i want to base it off of the previous constant this is not unusual you have constants that then are used to define other constants and so in this case maybe i want to do something like a plus 1. so whatever a is b is gonna be the number after it pretty straightforward now what happens if i come down here and compile it you're gonna notice this works great my compiler let me do this but older versions of gcc and clang would complain saying that this isn't allowed since a plus one is not a constant expression now you can see that here if i drop the code into the compiler explorer you can see that i get an error but even if we are dealing with newer compilers a plus one really is still not a constant expression but the compilers are just trying to be helpful and they're basically fudging things for us you know we know what you mean programmer let's just we'll just let this one slide but depending on the compiler you're using maybe you get an error that you wouldn't get if you were using the preprocessor now let's try one other thing what if i came down here and i wanted to declare an array of integers my array and i wanted the size of that array to be a maybe this constant defines the size of this array this is a pretty common thing to do defining array lengths with constants that is now if i do this and we come down here and try to compile it you notice that we're going to get this error and it says that this variably modified my array is declared at file scope and that's not allowed and again the reason for this is that a is actually a variable expression but note that if we took this if we copy this i'll just cut it down here and let's say that we put this down in our main function and now if i try to compile it if i can type now you notice my error disappears i do still have a warning because i haven't used this array but the compiler isn't giving me any errors it has no problem with a variable length array as long as it's in in a local scope as long as it's a local variable so that could be a little confusing for some of you and again the whole core reason is that a is a const int not a constant okay but let's put i'm gonna put a right back up where it was i'm going to comment it out so my program will still compile but i want to leave it up there just uh as an example of what doesn't work that way if any of you are looking at this code and trying to remember what we talked about you know this this thing doesn't work and maybe at this point you're starting to be convinced that a is not a constant and you're thinking okay yeah it's not a constant but i can get around it easily enough but let's look at one more place where this crops up that can also be really annoying and that is if we ever want to use these cons in a switch statement now this is actually a really common thing to do let's say i have a constant somewhere and i want to give it a name like a or b in this case although hopefully you use better names in yours but then somewhere in my code i want to test to see if something is the same as this constant and so to see this let's come down here and make a new function i'm going to have it return a boolean true or false call it is a or b and we're going to take in an integer value now what this is going to do is it's going to take a value and then we're going to compare it to some constants specifically we're going to compare it to a and b and let's just do that with a switch statement really quick and so if i switch on value and then i'm interested in two cases case a and case b and then if it's either of those then i'm going to return true and otherwise we will return false so that's really straightforward super simple and then if we come down and we try to compile it we're gonna get errors specifically it's gonna say case label which has to be an integer constant does not reduce to an integer constant in both a and b and that is once again because const int is not a constant in c and if anyone's really frustrated personally i wish it were a constant just like clang and gcc seem to be selectively wishing that it were a constant as well at least pretending that it's a constant sometimes but for now if you're programming in c it's important to remember that it's not and of course i can see a bunch of the c-plus plus people in the back raising their hands going me me me i mean i know you've got some tools at your disposal that can actually help with this specifically cost expressions hopefully you'll have time to talk about those in a future video but i hope you learned something new today i hope this helps you avoid some errors in the future if you did please click something like subscribe whatever click something on your way out and until next week i'll see you later
Info
Channel: Jacob Sorber
Views: 51,993
Rating: undefined out of 5
Keywords: const int *, c programming
Id: 8a3HyL1VN0Q
Channel Id: undefined
Length: 9min 16sec (556 seconds)
Published: Tue Aug 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.