Cisco Systems Interview Question
Software Engineer / DevelopersCountry: United States
I wrote the code and tested no matter what you do in both the cases x & y will be incremented first as they are closely bind to "++" and after they are incremented then only they are added, refer wiki for operator precedence.
if x = y = 0 initially, both cases will have answer 2,
if x = 1, y =2 then both cases will have answer 5.
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
int x=0,y=0,z;
z = x++ + ++y;
printf("%d %d %d \n",x,y,z);x=z;
y = ++x + ++y;
printf("%d %d \n",x,y);
getch();
return(0);
}
output 1 1 1
2 4
x will become x+y+3 & y will be x+2y+5
#include<stdio.h>
int main()
{
int x=5,y=15;
x= x++ + ++y;
y = ++x + ++y;
printf("%d %d",x,y);
return 0;
}
Output: 23 40
same problem comes with:
int x=2,y;
void main()
{
y=++x + ++x + x++; //gives output 12
y=++x + ++x + x++; //gives output 17
}
kindly explain
int x=2,y;
void main()
{
y=++x + ++x + x++; //gives output 12
y=++x + ++x + x++; //gives output 17
}
it gives 21 not 17
#include<stdio.h>
int main ()
{
int x=2, y=4;
int z=x+x+x+x+(x++);
printf("x=%d \n y=%d \n z=%d",x,y,z);
return 0;
}
OUTPUT:
x=3
y=4
z=10
#include<stdio.h>
int main ()
{
int x=2, y=4;
int z=(x++)+x+x+x+x;
printf("x=%d \n y=%d \n z=%d",x,y,z);
return 0;
}
OUTPUT:
x=3
y=4
z=14
Please someone explain the code. Why the output are different to each other?
#include<stdio.h>
int main ()
{
int x=2, y=4;
int z=x+x+x+x+(x++);
printf("x=%d \n y=%d \n z=%d",x,y,z);
return 0;
}
OUTPUT:
x=3
y=4
z=10
#include<stdio.h>
int main ()
{
int x=2, y=4;
int z=(x++)+x+x+x+x;
printf("x=%d \n y=%d \n z=%d",x,y,z);
return 0;
}
OUTPUT:
x=3
y=4
z=14
Please someone explain the code. Why the output are different to each other?
#include<stdio.h>
int main ()
{
int x=2, y=4;
int z=x+x+x+x+(x++);
printf("x=%d \n y=%d \n z=%d",x,y,z);
return 0;
}
OUTPUT:
x=3
y=4
z=10
#include<stdio.h>
int main ()
{
int x=2, y=4;
int z=(x++)+x+x+x+x;
printf("x=%d \n y=%d \n z=%d",x,y,z);
return 0;
}
OUTPUT:
x=3
y=4
z=14
Please someone explain the following code above. Why the output of z are different to each oher?
x = x++ + ++y; (1)
- Psycho May 07, 2012y = ++x + ++y; (2)
In (1) this is the following operations to be done.
i) ++y
ii) x = x+y
iii) x ++
In (2) this is the following operations to be done.
i) x ++
ii) y++
iii) y = x + y