+ 5
#include <stdio.h> int main() { int j=0; for(int i=0; i<100; i++) { j=j++; } printf("%d", j); } Output = 0
Explain the output ?
9 Réponses
+ 3
Generally post increment add 1 to that variable after the end of the statement.
For example.
int x = 1;
x++; //x is now 2
printf("%d",x++); //prints 2 - still x has not changed by 1 as the statement has not ended.
So
j = j++ will store 0 always as the increment do not have effect on that statement.
But ++j has another effect. It add 1 before the statement ends. So as HonFu said j = ++j could work.
+ 1
Okay, it seems this has been discussed. ^^
https://stackoverflow.com/questions/226002/whats-the-difference-between-x-x-vs-x
+ 1
Seems like j stores the temp value of the post-increment - again and again.
Change to j = ++j and suddenly it works.
+ 1
read this:
https://www.viva64.com/en/w/v567/
+ 1
Mohamed ELomari, interesting read - and quite scary for a beginner!
Initially I assumed it was just a typo, leading to a funny result, leading to a 'why'... guess I was being naive. ;-)
I have a question to the people who actually write C++:
Why would anyone even risk writing such statements? Isn't there some 'Zen of C++' or something that gives confused people a reliable list of things NOT to do? (Because obviously there is hardly any safety rail in the Cs...)
0
Not j=j++!
Either ++j, j++, j+=1 or j=j+1!
0
OP wrote a whole loop to leave j just as it was? Well then it's a success. ;-)
But maybe he will clarify himself.
EDIT: Wait, did he clarify or have I just not seen the 'explain the output' before?
0
because in this code firstly assign the value after increase the value of J.
0
It being good but why the progrem write (stoped)and dont give me the exemple can u help me