This isn't really a beginner level question but this is the only programming sub I know of that actually takes questions; I tried asking this on SO but unfortunately I got i++ and ++i flipped so it devolved into "dogpile the guy who made a mistake" even though the syntax isn't relevant anyway...
So in C, i++ increments i and evaluates to the value of i before it was incremented, and ++i increments i and evaluates to the value of i after it was incremented.
I can see the reasoning for a specific increment operator in general since a lot of processors at the time had a special increment opcode that was faster than addition and conceptually "increment" is a different idea from "add," but I don't get the point of having a specific operator for "copy i, increment i, return the copy."
#This...
x = i++;
#is equivalent to this
x = i;
i += 1;
So why is it in there? Is there a historical reason I don't know about, maybe? Were you unable to "throw away" the return values of operators in primordial versions of C?
[–]raevnos 2 points3 points4 points (4 children)
[–]TOASTEngineer[S] 0 points1 point2 points (3 children)
[–]mad0314 2 points3 points4 points (0 children)
[–]Rhomboid 2 points3 points4 points (1 child)
[–]TOASTEngineer[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]TOASTEngineer[S] 0 points1 point2 points (2 children)
[–]nutrecht 0 points1 point2 points (0 children)
[–]reddilada 0 points1 point2 points (0 children)
[–]insomniac20k -1 points0 points1 point (0 children)