Defining Constants. Should I use CONST or #DEFINE?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're talking about defining constants in your programs and whether you should use a pound of fine or a const welcome back everybody today i wanted to just take a few minutes to talk about a question that i get from a lot of beginning c and c plus programmers about the best way to define constants in their c and c plus programs so in case you're brand new a constant is just a value in your program that is never going to change it's not going to vary over time you set it and it just is going to stay the same throughout your program it could be the size of an array it could be an arbitrary threshold that i'm going to use someplace in my program and if you've looked at c and c plus code very long you're going to sometimes see them defined in a few different ways one of those ways is using a pound define like this and you'll also see things like this where it's defined as a const int so these are the two different styles you're going to see most often and students often wonder if there's a difference does it matter which one i use is one better than the other and the short answer is that usually it doesn't really make a huge difference but there are some differences and understanding those differences will help you make better decisions as a software developer so today i thought i would break it down and talk about the pros and cons of each so let's start with the pound define when i say pound define array length 240 what i am saying is i want the preprocessor to take everywhere where array length occurs in my program and replace that with 240. so it's handled by the preprocessor that means it happens before my code is even compiled and it's basically doing simple text replacement so on the other hand if i declare it as a const int then it declares this as a variable well a constant variable so it's a variable that isn't going to vary so maybe i don't want to confuse anyone by calling it a variable but i'm basically telling the compiler that i want a variable that i'm not ever going to change and so this array length thing is going to be handled like my other variables in my program except that the compiler is going to act under the assumption that it's never going to change so which one is better well that depends so one upside of the const in here is that it has a type it's an int it's not using just simple text replacement so the compiler knows that it's an int and it can do type checking so in case i do something strange that doesn't make sense for ins it can say hey this doesn't make sense for ins rather than just sticking 240 in there and hoping for the best so sometimes the const int will be the safer option it's also scope controlled and that means that i can limit its visibility so i can say maybe i only want this constant to be visible within main or some other function or maybe i only want it to exist within a particular translation unit i can do that with const int and this is helpful when we're trying to avoid naming conflicts in our programs for example let's say you have two modules created by different programmers they might use a constant with the same name with const and i can scope them so they only exist within the module and so we're going to be fine now with preprocessor macros like pound define they are globally defined so you have to be very careful that your names don't conflict macros can also be redefined which can be both annoying when you didn't mean to redefine it and helpful when you have a default value that you might want to change inside a particular module now another handy thing about preprocessor macros is that they can be defined outside of my source code when i compile my program with both gcc and clang i could use the d option to define a macro when i compile my program which can be useful if i have some constant value that might vary from one build to another so in that case i can simply build three versions of my program that each use a different value and i don't have to change the source code so let me quickly show you what i mean by this let's say this was commented out and let's bring up so normally we compile this as i don't know what's going on let's take a look at our make file really quick so if we jump into our make file right here i could say i want dash d array length equals 24 and now we can make this and it will compile and now if i run my program you're going sorry now if i run my program we can see that we get 24. so we could easily though change this to we could build a couple different versions like we could build one with 24 one with 240 and we'll call these let's say this one is that big there's a little hacky as probably isn't the way i should do my make file i'm just trying to demonstrate this but you can see that it created two different binaries and if i run test you can see it gets 24 if i do it with the dot big version you know this is the same source code i just passed in my preprocessor define to the compile command and it produced different results okay so this is handy you can't do this with a constant so let's put things back the way we had them before because now i just want to look into here and see what happens if i try to redefine a const end okay so say we're using the constant version and let's see down here i say array length equals 24. if i try to redefine this i should get some complaints so yeah so it says you declared it const meaning you promised you weren't going to change this thing and you went ahead and changed it so that's a problem now of course it is possible to try to get around this and so we could do something like this and say let's make an end pointer called p and we'll set it to the address of array length don't ever do this by the way in your programs but but i'm just trying to demonstrate here that i can actually have a pointer that points to the address of this constant and i can say something like this and you notice if we try this you notice that it warns us it's basically saying you're throwing away the const and so like i said don't ever do this this is really something that you should never do in your programs and whether or not it even works is going to depend a lot on the compiler you're using sometimes the compiler will let you get by with this sometimes it will not in this case if we run this you notice that it didn't actually let me change the value to 57 but some compilers will in fact i tried this earlier today on a linux virtual machine that i was using and basically got different results it allowed me to set the 57 in there even though it did warn me but even in that case once i turned on compiler optimizations then it went back to 240. and the problem here is that we are telling the compiler that this is constant so when we tell the compiler that it's constant the compiler will first of all i mean we get the warning because it's just a stupid thing to do to try to change a constant but the bigger problem is that the compiler once you've told it's a constant it begins to make assumptions so it may store this value differently it may actually do exactly what the pound define was doing up above and it may just actually stick a 240 everywhere where array length is used we don't really know it will try to make decisions that make code fast and occasionally those decisions may make code like this down here absolutely moot so it just like doesn't actually have any effect so the short answer is don't try to redefine a constant it's just doesn't make any sense and while we're talking about optimizations this brings me to another difference and that is that without compiler optimizations a pound define is probably going to be slightly faster okay you would have to inspect the general you would have to inspect the generated assembly to be sure about this but usually the generated assembly when optimizations are turned off is going to be a bit faster with a macro than with a const in and that's because of the way that variables are loaded but once you turn on optimizations or if some optimizations are turned on by default then performance wise they're going to be about the same now this won't always be the case with function like macros and i don't have time to get into those drop me a comment down below if you'd like to see a video about function style macros but as far as simple pound defined constants the macro and the const int should be about the same nearly all the time and of course there's a lot more that i could say about the preprocessor but i hope this is helpful drop the video a like if it was helpful subscribe if you don't want to miss my next video and until next week i'll see you later
Info
Channel: Jacob Sorber
Views: 19,282
Rating: undefined out of 5
Keywords: const or #define, const, const c, const c++, #define, const or preprocessor define, program constants, c constants, c const, const int, preprocessor macro, preprocessor #define c, const cpp, c/c++ tutorial, const c/c++, c/c++ constants, programming tutorial, C language, define preprocessor in c, preprocessor macro c++, preprocessor macros, #define in c, c constants and variables
Id: ZIhy4uy5uJM
Channel Id: undefined
Length: 7min 58sec (478 seconds)
Published: Tue May 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.