6.7 – Increment/decrement operators -part 2

by subbu on September 28, 2013

Quiz 2:

1) What would be the output of following program?

#include<stdio.h>
int main()
{
int a=10;
printf("%d\t%d\t%d",--a,a++,++a);
return 0;
}

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int a,b,c;
a=10;
printf("%d\t%d\t%d",c=++b,b=a++,--a);
return 0;
}
Show Answers
1)
Output:
11      11      11

Because the evaluation of comma (,) operator is from right to left, ++a increments the value of “a” by 1 and prints (11) , a++ prints (11) the value of “a” and then increments by 1 and   – – a decrements the value of “a” by 1 and then printed.

2)
Output:
10      9        9

White space

Quiz 3:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=10,y=5;
y=x-----y;
printf("x=%d\ny=%d",x,y);
return 0;
}

2) Write the output of following program?

#include<stdio.h>
int main()
{
int x=10,y=5;
y=x++ - --y;   /* can't be written as y=x++---y; */
printf("x=%d\ny=%d",x,y);
return 0;
}

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int x,y;
x=++10;
y=--x;
printf("x=%d\ny=%d",x,y);
return 0;
}

4) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=10,y=5;
y=-x-- - ++y; /* can be written as y=-x---++y; */
x=++y-x;
printf("x=%d\ny=%d",x,y);
return 0;
}

5) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=10,y=20,a,b;
a=x++ +10;     /* can be written as a=x+++10; */
b=++y +15;     /* can be written as b=++y+15; */
printf("a=%d\nb=%d",a,b);
return 0;
}
Show Answers
1)
Output:
error: lvalue required as decrement operand
y=x – – – – – y;
^
Compiler falls into confusion. It can be rectified by giving space at both sides of – operator
y=x – –   –   – – y; is the correct way of writing statement.

2)
Output:
x=11
y=6

As the associativity of increment/decrement operators is right to left, – – y evaluates first and y becomes 4

Because x++ is a post increment, the value of y is subtracted from x (10-4) and assigned to y and then x would be incremented by one (11).

3)
Output:
Error

++10 is invalid because increment/decrement operators can’t be applied to the constants and expressions.

4)
Output:
x=-24
y=-15

y=- x – –   –   ++y;
In this statement ++y evaluates first (y is 6), y is subtracted from -x (-10-6 is -16) and assigns to y (-16) and then x is decremented by 1 (10-1 is 9)

x=++y-x;
In this statement y is first incremented by 1 (-16+1 is -15), the value of x is subtracted from y (-15-9 is -24) and assigns to x.

Note: -x would not change the value of x

5)
a=20
b=36

a=x++ +10;   //x is added to 10 first and then x would be incremented

b=++y +15;   //y is incremented by 1 and then added to 15

White space

Quiz 4:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=10,y;
y=x--- +1;    /* can be written as y=x---+1; */
printf("x=%d\ny=%d",x,y);
return 0;
}

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=10,y;
y=x++ + ++x;     /* can't be written as y=x+++++x;  */
printf("x=%d\ny=%d",x,y);
return 0;
}

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=10,y;
y=x++ + x++;    /* can be written as y=x+++x++;  */
printf("x=%d\ny=%d",x,y);
return 0;
}

4) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=2,y;
y=++x * ++x * ++x;    /* can be written as y=++x*++x*++x; */
printf("x=%d\ny=%d",x,y);
return 0;
}

5) What would the output of following program?

#include<stdio.h>
int main()
{
int x=10,y=x,z=y;
y-=x--;
z-=--x;
x-=--x-x--;
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}
Show Answers
1)
Output:
x=9
y=9

2)
Output:
x=12
y=22

y=x++ + ++x;
++x evaluates and increments the value of x by 1 (11), the value of x is added with the value of x (x++ + 11) and assigns to y (22). x is incremented by 1 (12) after assigning the value to y.

3)
Output In turbo C
x=12
y=20

y=x++ + x++;
Values of x are added and assigns to y and then x would be incremented for twice

4)
Output In Turbo C
x=5
7=125

y=++x * ++x * ++x;
Here x is incremented for three times and then added, assigned to y;

5)
Output In Turbo C:
x=6
y=0
z=2

y-=x – -;                  // y=y-x   x=x-1
z-= – -x;                  // x=x-1   z=z-x
x-= – -x   –   x- -;   // x=x-1   x=7-(7-7)  x=x-1

White space

Quiz 5:
1) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=2,y,z;
y=++x * x++ * ++x;  /* can be written as y=++x*x++*++x; */
printf("x=%d\ny=%d",x,y);
return 0;
}

2) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=10,y=5,z;
z=x-- -y;      /* can be written as z=x---y; */
printf("x=%d\ny=%d\nz=%d",x,y,z);
return 0;
}

3) What would be the output of following program?

#include<stdio.h>
int main()
{
int x=10,y;
y=--x--;
printf("x=%d\ny=%d",x,y);
return 0;
}

4) What would be output of following program?

#include<stdio.h>
int main()
{
int x=10,y;
y=(++x)++;
printf("x=%d\ny=%d",x,y);
return 0;
}
Show Answers
1)
Output In turbo C
x=5
y=64

y=++x * x++ * ++x; // x=x+1  x=x+1  y=x*x*x  x=x+1

2)
Output:
x=9
y=5
z=5

z=x- –   –  y;  // z=x-y   x=x-1

3)
Output:
error:   // an operand can’t have both prefix and postfix operators

4)
Output:
error:  // can’t use increment/decrement operators with expression

Previous post:

Next post: