# 005 # puzzles on c

@dholey (1383)
India
January 23, 2007 8:56am CST
/* what will be the output explain it in detail why it is there */ void main() { int x=10,y=5,z; z= (x=10 && y=6)?100:200; printf("\n z = %d ",z); } please be specific ......
2 people like this
6 responses
• India
24 Jan 07
First of all, this program will not compile... WHY? Let me explain... See the statement: x = 10 && y = 6 The precedence of ‘&&’ is more than assignment… so it would become like “x = (10 && y) = 6” Now, “10 && y” is actually a constant.. so it would become like “x = (const number) = 6; Here… “const number = 6” is an error known as ‘L-value’ error. I hope this is clear now… Again, let me try to correct you, may be u want to write your program in this way:- void main() { int x=10,y=5,z; z= (x=10 && (y=6))?100:200; printf("\n z=%d ",z); } Now .. It will compile and result would come ‘z = 100’ because x = 10 is a true condition and y=6 is a assignment of 6 in y which becomes true. So … z = true ? 100 : 200,,,, very clear that the value of z would become 100 here.
1 person likes this
• India
24 Jan 07
Ohh I didn't know that this program had a twist like this. Good going.
@dholey (1383)
• India
28 Jan 07
thats the correct answer ....
• India
28 Jan 07
Hi dholey, output is simple. tell me whether i am right. output: z=100 Reason: 1. As x=10 so x=10 condition is true. 2. y=6 is a nonzero value even it is true. 3. So condition of ternary operator is true. Hence 100 is returned and assigned to z 4. finally we get x=10 and y=6 and z=100
@dholey (1383)
• India
28 Jan 07
the post with the best response is correct
@_Greeneye_ (1526)
• India
29 Jan 07
hi dholey me back aftr a long time u nd tmahes.... doin extremely well. i dont know much abt programming but these discussions doing a great job for beginners
@dholey (1383)
• India
31 Jan 07
nice to see you again dude .... and many thanks for the encouragement .....
• India
29 Jan 07
z=200
@dholey (1383)
• India
29 Jan 07
sorry .. your answer is wrong it gives an error message just check MAHESHWARI JI'S response