This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
(C) What is the difference between ++i and i++
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(“%-4d”,a[i][j]);
}
printf(“\n”);
}
In the above case, you won't recognize it.
But it's essentially the following:
int x = a[i++] first reads from à afterwards, increases i
int x = a[++i] first increases i, afterwards reads from a
++i Increments $i by one, then returns $i.
i++ Returns $i, then increments $i by one.
i++ is a post-increment operator ie., the current value of i is used for the operation and the value is incremented by 1 after the operation.
++i is pr-increment operator ie., the value of i is incremented and the new value of is used in the operation.
int i = 2;
int a = ++i; // a is 3, i is 3
int b = i++; // b is 3, i is 4
++i is a pre-increment, while i++ is a post-increment.
In that situation, because the type is an int and it happens in a for-loop, nothing; there is no performance benefit to either.
Related
This question already has answers here:
Why do logical operators in C not evaluate the entire expression when it's not necessary to?
(5 answers)
Closed 4 years ago.
In the following code why does the value of j remains zero even after the statement j=i+10.
#include <stdio.h>
int main()
{
int i = 10, j = 0;
if (i || (j=i+10))
printf("%d",j);// j=0
}
Why does value of j not change? Because i is non-zero and logical OR (||) properties is that if 1st operand is true don't check 2nd operand.
if ( i || (j=i+10))
| |
10(True) not evaluated
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Say I'm assigning codes to ASCII chars, where code 1 is assigned to the ASCII value 0, the NUL char, and so on. I'm doing this iteratively, currently with i+++1, that is:
for (int i = 0; i < 256; i++) {
// assign code i++ +1
arr[i].code = i++ +1;
}
If I were to use ++i instead, would that suffice?
This might help
y = i+++x -> Add x to current value of i and assign the result to y and then increment value of i
y = ++1 -> increment i by 1 and assign the result to y
I would prefer to write that i++ + 1. But yes, ++i has the same effect and value asi+++1.
The latter logically does more work to arrive at the result, though: it keeps the old value ofi, increments i, adds one to the old value and yields the sum as result. The preincrement operator just increments i and uses its new value as result.
Technically both should compile to basically the same code given an optimising compiler.
Yes. They are the same.
++ bounds tighter than + so the expression is evaluated from left to right as
s = (i++) + 1
which can further be expanded as
s = i + 1
i = i + 1
On the other hand, s = ++i does the same task. It can be expanded as
i = i + 1
s = i
It is preferred that you use the second version because it is more readable and extensible. But I don't think there will be a difference in speed.
Yes, i+++1 and ++i will both evaluate to the value of i plus 1, and will both leave i incremented by 1.
It's an odd way to do it, is there something wrong with just doing:
for (int i = 1; i < 256; i += 2)
Anyone reading your code later would prefer the latter, I believe.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
I always forget which of i++ and ++i return which value. To test this I wrote the fallowing code:
int i;
i = 6;
printf ("i = %d, i++ = %d\n",i, i++);
printf ("i = %d, ++i = %d\n",i, ++i);
The resulting (unexpected and strange) output is:
i = 7, i++ = 6
i = 8, ++i = 8
But when I break down the printfs to 4 separate commands, I get the expected result:
printf ("i = %d, ",i);
printf ("i++ = %d\n",i++);
printf ("i = %d, ",i);
printf ("++i = %d\n",++i);
gives:
i = 6, i++ = 6
i = 7, ++i = 8
Why does this happens?
You have both unspecified and undefined behaviour:
Unspecified behaviour: you don't know the order of evaluation of the parameters in the printf call.
(The C standard does not specify this: it's up to the compiler and it's free to choose a way that best matches the machine architecture).
Undefined behaviour: The commas in the function call are not sequencing points. The behaviour is undefined as you're attempting to read and modify the same object without an intervening sequence point.
Because in you first block i is 6 and after the printf statement it gets incremented because it's postfix!
After that in the second printf statement it's prefix and gets executed before it gets used so 7 + 1 = 8.
In your second code block i is 6 in the first printf statement, because it's postfix! That's why the second output is 7 and the last printf statement has a prefix increment so it gets incremented before it gets used and is 8!
Prefix/ Postfix:
i++ //gets incremented AFTER it gets used (e.g. in a operation)
++i //gets incremented BEFORE it gets used (e.g. in a operation)
EDIT:
The evaluation order of the arguments of a printf statement is unspecifyed !
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Undefined Behavior and Sequence Points
#include<stdio.h>
int main(){
int i=5,j=5,y,x;
int m=++i;
int n=++i;
x=m+n;
y=++j + ++j ;
printf("%d %d ",x,y);
return 0;
}
OUTPUT : 13 14
Can any one plz explain why 'y' value is 14 and not 13.
Most compilers will increment j twice before performing the addition and attributing the value to y, that is why the result in your case is 14.
The C standard doesn't specify the order of evaluation of that expression, though, so on another compiler the result could be 13 indeed.
In other words, this is undefined behavior and should be not be used other than in obfuscation contests and puzzles.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
I have an expression in a program, initial value of i = 10
int j = i++ + i++;
it sets j as 20
but
int j = i++ + ++i;
it sets j as 22
Why is there a difference of two between statements? I think, difference should be of 1.
I know this is undefined in C, but why GCC is doing such things?
For i++ it is like: first do the equation then increment i.
For ++ i is like: first increment i then do the equation.
so i guess i++ + ++i is interpreted as i + (++(++i)) so thats the result is 22.
Wired things happen with wired syntax just dont do stuff like this ;).
If i'm right ++i + ++i would be 22 also.