Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why the output for below code is 19 but not 18....

0 votes
asked Dec 19, 2019 by loller boy 1 flag
#include<stdio.h>
int main()
{
    int i=4,x;
    x=(++i)+(++i)+(++i);
    printf("%d",x);
    return 0;
}

4 Answers

0 votes
answered Dec 20, 2019 by gameforcer (2,990 points)
edited Dec 20, 2019 by gameforcer
Thing you did here is called an undefined behavior. Basically it's a situation where program doesn't know which action out of many should go first. In this case it's whether to increment i variable or to first add it up to x variable.

Generally speaking it's hard to predict what the outcome will be in this type of situations. Just avoid using multiple increments of the same variable in one equation and you should be good.
commented Dec 22, 2019 by kotha kushal (560 points)
i thought you are wrong
0 votes
answered Dec 20, 2019 by Keerthi Reddy (140 points)
#include<stdio.h>

int main()

{

int i=4,x;

x=(++i)+(++i)+(++i);

printf("%d",&x);

return 0;

}
commented Dec 30, 2019 by venkataPrasad (100 points)
6+(6*6) = 42
First priority is for pre increment so i value will be 6 because two preincrements are there, then i value will be initiliazed.
0 votes
answered Dec 21, 2019 by anonymous
answer is   19
+1 vote
answered Dec 23, 2019 by anonymous
It's called undefined behaviour. Changes on what compiler is used etc. Other online compilers output 18
commented Jan 8, 2020 by Helloo (100 points)

#include<stdio.h>

int main()

{

int i=4,x;

x=(++i)+(++i)+(++i);

printf("%d",&x);

return 0;

}

commented Jan 8, 2020 by Helloo (100 points)
6+(6*6) = 42
First priority is for pre increment so i value will be 6 because two preincrements are there, then i value will be initiliazed.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...