How to evaluate multiple increments in the same line? (++a + a++ - ++a)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i want to take a look at a very simple expression that has been asked quite a bit and everybody should actually understand exactly why it is wrong and why you shouldn't actually do it so here's the expression it's a simple variable definition right we have a equals zero and what we do with it is inside a single line we increment it multiple times using the plus plus operator that perceives it and sometimes after it and before executing this code i want us to go over what the result should be right so you can simply have here a comment and let's think about it a bit for a second so i have plus plus a so because it's a it's an increment but it's before uh the actual evaluation that means that we first increment a and then we evaluate it so that means that we have a become one because it's zero in the beginning and then we evaluate it to well one okay cool plus that plus remains the same we don't have anything to do with then we do that again right we increment a from one to two and then we evaluate it to well our value 2. amazing then the minus still stays the same that's nothing different and then another one of these operations so plus plus a that means that we increment the now a is actually two because we have incremented it twice so the third increment increment is gonna bring it up to three and we're gonna evaluate it to free okay that that addition is gonna stay the same and then the last one is going to first evaluate the value of a which is going to be well it's three because we have three increments it's going to be evaluated to three but then it's gonna make a b four right so that is the really these are the operations that we should see being done one after the other so in this case what the result for this well this minus three with this plus three kind of nullifies themselves so it should be actually one plus two which is actually just three so we should see on the screen the number three wow that was very difficult right let's uh let's take a look so oh we actually see four as a result that's that's not what we expected right that's there's an issue with that what's what's up with this why is it not actually working as we calculated it to be hmm well this really has to do with the c standard itself and how the compilers really uh handle these situations um the first things first there are two important statements inside the uh standard that is basically implemented by all the compilers the first one is that there shouldn't be more than one statement with side effects on the same identifier what does that mean well side effects are um something that change a certain identifier or certain let's say variable in this case and these increments actually are side effects so this a right we said first we increment and then we evaluate the value so uh before evaluating we had to change the value the variable in some way we had to uh have a side effect and in this case we actually have more than one side effects we have four of them okay that's that's fine i guess even though the standard says that we shouldn't have more than one but the second uh statement you can find in the standard relating to this issue uh says that the order of uh calculating basically evaluating the operands or the arguments we have operands in this case uh is not defined so here we have actually assumed that the that each operand here is evaluated from left to right right you know it goes like this but it might be that it's actually from right to left and if it is from right to left we can let's try to calculate it if it is um if the order of evaluation was from right to left if we try to do that instead well i guess we're going to start at this last a right so that's going to be a bit different because now we have a plus plus a plus plus says well uh evaluate to the current value so i'm going to give it zero and increment a to one okay so we have a b the value one now that plus stays the same there's nothing different with that but then the the second evaluation is going to be this one which is going to say well a is now 1 we first increment so we make it 2 and then we evaluate so then we print it to printing 2 here right and the subtraction stays the same the next operation again well we have value two so we went from we go from two to three and then we evaluate it so we get three here okay amazing then we have here a plus that's gonna stay the same and the last one is gonna be well it's gonna go from three to four and evaluate it to exactly four so now what's the result of this set of operations well this is four plus three which is seven minus two is five okay so this is uh everybody probably was expecting that we would get a four as we got here but remember uh the order of operations might not be even like this it could be like completely random right so we went from left to right from right to left but it could be like completely random and the fact that uh the order of evaluation gives you different results is really really bad that shouldn't ever happen in uh in ac program at least so what should we do when somebody asks us about how to get the value of this well this is really undefined behavior nowhere in the standard is it defined and every compiler can really give us whatever value they want and multiple compilers might give us different values so it depends on well the implementation that you're working with so here i get right here i get four so let me launch this again to make sure we do get four but if i get the same code so the exact same code and i move it to uh msvc so microsoft's compiler and i think this is already the same code yep if i try to launch this we're gonna get six so this means that the order of evaluating the operands is actually different from this compiler from microsoft compiler to this uh gcc to this gcc compiler that we have here on a linux machine so that's the most important takeaway uh from this video that this is undefined behavior and really if you do this you shouldn't do this if you have to increment a multiple times simply increment it uh beforehand so for example here i can just say a plus equals let's say three then simply say a plus a minus a plus and then we can remove that a plus plus and just add it here let's see something like that and don't rely on the uh on the compiler to evaluate on the compiler but the runtime to actually evaluate the uh values in the right order all right i hope you got something out of this video if you do have any questions leave them down comments below or on our discord server again the source code can be found on our website link in the description below take care bye you
Info
Channel: CodeVault
Views: 671
Rating: 5 out of 5
Keywords: codevault, c (programming language), operators, ++a, a++
Id: _7Wok3JoOcE
Channel Id: undefined
Length: 8min 3sec (483 seconds)
Published: Tue Jan 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.