postfix or prefix increment?
Postfix or prefix increment?
For a for loop that's absolutely irrelevant so you should do what all others are doing and use i++.
In this case it produces the same results so pick the one to your linking.
>Increase i by one
vs
>Increase one by i
I wonder which one is better
i+=1
I know, I'm asking whether it's considered better style to use one over the other. It seems to me that it would be better to use ++i since you don't need the original value returned by i++ (even if that's optimized away)
++i makes more sense for a loop but that does not really matter. check the generate asm code to see how gcc handle it.
>It seems to me that it would be better to use ++i since you don't need the original value returned by i++ (even if that's optimized away)
You do realize ++i returns the original value +1... right?
>++i makes more sense for a loop
Why do you say that?
++i returns i after it's been incremented
i++ has to store the original value of i in a temporary register before it's incremented, then return that original value
In both cases a value is returned (if you can be bothered to use it or it indeed gets optimized out).
Yeah, but it won't because you're not using that value.
i++ makes a copy of the original value. ++i doesn't. Using i++ implies that you need that original value.
.....there's no reason it would be 'better'
this dude's talking out of his ass
you need to initialize in both cases, what're you on about?
int preincrement(int& i) {
i += 1;
return i;
}
int postincrement(int& i) {
int tmp = i;
i += 1;
return tmp;
}
Ideally they would be both the same in the for loop situation since any compiler worth using would optimize the copying away, but since ++i and i++ are both equally readable, why not use ++i?
Cause operators are specified after operands?
I find postfix style more readable. It literally doesn't matter as long as you're not using the result of the expression though.
All other unary operators come before the operand in C: & * - + ~ ! sizeof
++i increments the value then returns it.
i++ returns the value and then increments it.
++i reflects the intended behavior of a counter, nothing more.
Can't you fucking read?
I said
>++i makes more sense for a loop
which is true.
i++ or ++i will most likely be optimized in the same way by the compiler but writting exactly what you mean is the different between the knowledgable developer and the code monkey.
incr i
Operators come between their operands in infix notation, and unary operators typically come before their operand (unless you usually write 1- for "negative one").
It seems that prefix is better in all circumstances.
I dont want to
int v = 13;
printf("%i", v++); //prints 13
//now v is 14
why use postfix?
The typical case I can think of is combining something like:
a[i] = b;
++i;
into
a[i++] = b;
>irrelevant
>so you should do what all others are doing and use i++.
???
Something as important as a counter increase should have be on a dedicated line.
/thread
No.
this
Suffixed while as follows has values [0,c_initial-1]
while (c--) { }
This is obviously useful with arrays. On the other hand, similarly prefixed while would have values [1,c_initial-1], which is seldom useful.
This is just waste of space if the operation is otherwise an oneliner.
I won't necessarily disagree, but is an extremely common idiom in C, so you're likely in the minority if that is your language of choice. Several other operators or the behaviour of certain operators in C were designed around shorthand like that.
And in that case, an increment operator is barely necessary at all. Languages that tend to take that stance would simply reuse i = i + 1.
>is an extremely common idiom in C, so you're likely in the minority if that is your language of choice
That being said, garbage code like this and other shit like magic globals, reinventing linked lists instead of using proper dynamic array libraries with capacities, shit names and other crap are extremely common idioms in C. Since C programmers are mostly wannabe leet haxxorz and quality C coders are a rarity, it is safer to avoid most C code out there and people that produce stuff like a[i++] = b;, because those people would in the same breath produce stuff like a = a++;.
No it doesn't, the prefix version skips the 0'th index. Which might be want you want, or it might not be want you want. Depends on what you intend for the loop to do.
inb4
>I was merely pretending to be retarded
.map() or . flatMap() and no incrementing because i'm not a c/c++ pajeet
Aside from that, the a[i++] = b; usage is in K&R C, so it's not reserved solely for "garbage" coders. Like basically all of C in general it's a shorthand that you can use or abuse.
K and R are garbage coders for not learning from the past, creating C and allowing its standard in such a limited way in the first place.
What a waste of trips.
i = i + 1
for (i = 0; i < MAX; asm volatile("inc %0" : : "=r"(i)))
This