It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
iS THIS TRUE OR FALSE ? explain?
i++ = ++i
No, it isn't. Either true OR false.
The problem is that C/C++ don't define when ++ happens within this expression.
So you have several possibilities:
Add 1 to i for the ++i then store that back in i, then add again for thei++`.
add 1 to i for the i++ and save thje result for later; add 1 to i for the ++i assign it to i and then put the saved value of i++ into i.
Add 1 to i for the i++ and then assign the result of ++i on top of it.
It gets even better when you consider, say, i = ++i++;
(See the link in the comments. The technical issue with whether there is a "sequence point" there, at which point all side effects should be resolved. In this assignment, there's not one.)
It depends what it is you're actually getting at:
If you meant does the following expression evaluate to true:
i++ == ++i
then it's undefined behaviour because i is modified twice between sequence points.
If you meant:
do i++; and ++i; do the same thing then the answer is sort of -- they both increment i. Where they differ however is if they are part of a larger statement, do they use the value before or after the increment.
In practice this means that i++ might possibly involve making a copy internally, in order to store the value before the increment, whilst ++i doesn't need to make such a copy.
If you were asking about i++ = ++i; as a statement on its own then it's not a valid statement for a more fundamental problem: the i++ cannot be the lefthand side of an assignment because of the "temporary" nature of its value.
See this link and this one for more discussion etc.
Just in case you don't know the difference between pre-increment and post-increment and you just formulated the question unintelligible:
i = 7;
printf("%d\n", i); // precondition: result 7
printf("%d\n", ++i); // PRE-INCREMENT: result 8 !!!
printf("%d\n", i); // postcondition: result 8
i = 7;
printf("%d\n", i); // precondition: result 7
printf("%d\n", i++); // POST-INCREMENT: result 7 !!!
printf("%d\n", i); // postcondition: result 8
Related
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:
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.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Concerning the following usage, i'm confused how --i evaluates to true and what determines when the loop exits:
while (--i) {
k = p[i];
p[i] = p[j = random() % B];
p[j] = k;
}
if as I understand it the -- prefix is decrementing the value of i before it begins the loop, are we then evaluating true=value > 1 and false=0 and thus the loop exits when the value reaches 0? Perhaps I've answered my own question, but if anyone could enlighten me on this. Also, how would this loop behave if the decrement operator was a suffix?
Yes, prefix decrement will decrement the variable, then the returned value (the result) from the expression is used for the condition.
The loop terminates when i becomes 1 (and it decremented to 0, the returned value of the condition).
As you have defined your code i can say that it totally depends on the value of i.
if the value of i=0 initially then it will go in infinite loop because --i will become -1 which will be true condition for the while loop.
so if the value of i is other than 0 (may be positive or negative) then it will always true but when it will become 0 the loop will terminate
and for prefix & suffix ... u should remember that in (-- variable) or (++ variable) case the variable will be incremented or decremented first then checked but in (variable --) or (variable ++) case the variable will be incremented or decremented later but checked first
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
#include<stdio.h>
void main()
{
int i = 10;
i=!i>14;
printf("i=%d",i);
}
I get the output : i=0
I get the same output even when I change the value of i to any other integer.
What does this code do?
Please explain
This line
i=!i>14;
is parenthesized (implicitly)
i= (!i) > 14;
Since the result of a ! is always either 0 (if i != 0) or 1 (if i == 0), the result is always smaller than 14.
Your printf call
printf("i=%d");
misses its second argument (thanks #DSM for spotting it), that invokes undefined behaviour, since each conversion specifier must have a corresponding argument of the correct type.
Also, your printf call just specifies a format but is missing i as argument.
This statement: i = !i > 14 assigns to the variable i the result of the expression: !i > 14.
(!i) > 14 is false because !i is zero for any non-zero number, i.e. 0 > 14. Since false is represented in C by 0, i gets the value 0.
Also, your printf call doesn't have a matching argument for the %d in the format string. The printf should be: printf(i=%d\n", i);
Just to complement the other answers:
There is an error in this line:
printf("i=%d");
It should be:
printf("i=%d",i);
Due to precedence rules, that line reads as:
i = (!i)>14;
So, i is 10, which, for the ! operator, is true; ! negate this, giving false, i.e. 0, so what you get is 0>14, which is obviously false, i.e. 0.
I think you wanted this
i=!(i>14);
The logic you wrote resolves from left to right completely, first not(i) then it's result is tested against 14 for greater than.
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 this code but i'm not getting how its functioning.
main()
{
int i=1;
i= ++i + ++i + ++i;
printf("%d",i);
}
I tried to compile it and im getting the output 10 but i've no idea how.
What I figured out is the two ++i are getting assigned the value 3 and the one ++i is getting the value 4, before the addition operation is performed. I cant figure out how increment operator is working in this case. Plz help me out with this.
The behavior is undefined .. there are a lot of posts similar to this if you search on SO.
For instance What would the evaluation order of x = x++ + ++x; be? or Why are these constructs (using ++) undefined behavior? and more.
Finally, just an opinion/comment: I don't think anyone would advocate writing that type of code as it's also hard to understand (hence the reason for your question).
I'm pretty sure you're not supposed to do that. Basically, don't modify a value more than once inside of the same expression. To do otherwise invokes "undefined behavior", which is a fancy way of saying "the compiler makes no guarantees of what will happen.
(Technically, the rule is don't modify a value more than once between the same sequence points)