Question #25223

tell me the logic of this(it is a program of c)
main()
{int x,y,z;
x=y=z=-1;
z=++x&&++y&&++z;
printf("%d%d%d",x,y,z);
}

Expert's answer

1) First line in the "main" function is justdeclaration of variables of type "int" (without initialization).

2) Second line is initialization of these variables. Whenassignments are written in such sequential form it must be done from right to

left, that is firstly "z=-1" is executed, then the result of this

assignment (which is -1) is assigned to "y", and then the result of

the last assignment "y=z=-1" (which is -1 again) is assigned to x.

Thus, all three variables x, y, z equals -1 after second line is executed.

3) Third line is the most complicated. It assignsexpression "++x&&++y&&++z" to variable z. Firstly

prefix increment of all three variables is executed: "++x",

"++y" and "++z" (Since these increments are of prefix

variant, their priority is highest). After that values of these three variables

are greater by one then they were, that is now x, y and z equals 0. After that

logical operation AND (denoted as "&&") is performed: firstly

++x AND ++y, and then the result of (++x AND ++y) with ++z. Since operands are

equal to zero they are interpreted as FALSE, and the result is FALSE itself

(that is zero too). Thus the whole result of this expression is zero. Then it

is assigned to the left part of assignment, that is "z". But

"z" was zero already, so it is not changed. Summarizing, all three

variables are equal to zero.

4) The last line prints three variables x, y and z indecimal format (%d denotes integer in decimal format). So 000 will be printed on

display.

2) Second line is initialization of these variables. Whenassignments are written in such sequential form it must be done from right to

left, that is firstly "z=-1" is executed, then the result of this

assignment (which is -1) is assigned to "y", and then the result of

the last assignment "y=z=-1" (which is -1 again) is assigned to x.

Thus, all three variables x, y, z equals -1 after second line is executed.

3) Third line is the most complicated. It assignsexpression "++x&&++y&&++z" to variable z. Firstly

prefix increment of all three variables is executed: "++x",

"++y" and "++z" (Since these increments are of prefix

variant, their priority is highest). After that values of these three variables

are greater by one then they were, that is now x, y and z equals 0. After that

logical operation AND (denoted as "&&") is performed: firstly

++x AND ++y, and then the result of (++x AND ++y) with ++z. Since operands are

equal to zero they are interpreted as FALSE, and the result is FALSE itself

(that is zero too). Thus the whole result of this expression is zero. Then it

is assigned to the left part of assignment, that is "z". But

"z" was zero already, so it is not changed. Summarizing, all three

variables are equal to zero.

4) The last line prints three variables x, y and z indecimal format (%d denotes integer in decimal format). So 000 will be printed on

display.

## Comments

## Leave a comment