+ 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 ?

7th Dec 2018, 4:37 PM
👑 Ak-king 👑
👑 Ak-king 👑 - avatar
9 Respostas
+ 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.
8th Dec 2018, 12:14 PM
Seniru
Seniru - avatar
7th Dec 2018, 5:23 PM
HonFu
HonFu - avatar
+ 1
Seems like j stores the temp value of the post-increment - again and again. Change to j = ++j and suddenly it works.
7th Dec 2018, 5:26 PM
HonFu
HonFu - avatar
7th Dec 2018, 8:06 PM
MO ELomari
MO ELomari - avatar
+ 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...)
7th Dec 2018, 9:44 PM
HonFu
HonFu - avatar
0
Not j=j++! Either ++j, j++, j+=1 or j=j+1!
7th Dec 2018, 4:52 PM
HonFu
HonFu - avatar
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?
7th Dec 2018, 5:02 PM
HonFu
HonFu - avatar
0
because in this code firstly assign the value after increase the value of J.
10th Dec 2018, 6:44 PM
Asmita Srivastva
0
It being good but why the progrem write (stoped)and dont give me the exemple can u help me
29th Jan 2019, 8:49 PM
Youcef Almenssori
Youcef Almenssori - avatar