You are on page 1of 10

CS 101 HOMEWORK 02

NAME : CHAMAN SINGH ROLL NUMBER : 21135047


BRANCH : MECHANICAL ENGNEERING GROUP : 01

Q. 1. Execute and write the outputs of the following programs and with valid
justifications:

(a)
#include<stdio.h>

int main()

int x =120;

printf("%d %d %d",x,x++,++x);

// compiler reads this part from right to left

// but print it from left to right

//compiler goes due to operator printed

// ++x preincrement increment first assign later 121 122 <---|

// x++ postincrement assign first increment later 122 121 |

// x as it is 122 122 --->|

// as it prints from left to right output is

// 122 121 122

printf("\n\n");

x =120;

printf("%d %d %d",x++,++x,x);

// compiler goes due to operator printed

// x as it is 120 /> 122


// ++x preincrement increment first assign later 121 /> / 122

// x++ postincrement assign first increment later 122 / 121

// as it prints from left to right output is

// 121 122 122

return 0;

OUTPUT:
122 121 122

121 122 122

(b)
#include<stdio.h>

int main(){

int x =120;

printf("%d %d %d",x,x++,++x);

//compiler goes due to operator printed

// ++x preincrement increment first assign later 121 122 <---|

// x++ postincrement assign first increment later 122 121 |

// x as it is 122 122 --->|

// as it prints from left to right output is

// 122 121 122

printf("\n\n");

x=120;

printf("%d %d %d",x,++x,x++);
//compiler goes due to operator printed

// x++ postincrement assign first increment later 121 120

// ++x preincrement increment first assign later 122 122

// x as it is 122 122

// as it prints from left to right output is

// 122 122 120

printf("\n\n");

x=120;

printf("%d %d %d",++x,x++,x);

//compiler goes due to operator printed

// x as it is 120 /> 122

// x++ postincrement assign first increment later 121 / 120

// ++x preincrement increment first assign later 122 -----> 122

// as it prints from left to right output is

// 122 120 122

printf("\n\n");

x=120;

printf("%d %d %d",x++,++x,x);

//compiler goes due to operator printed

// x as it is 120 122

// ++x preincrement increment first assign later 121 122

// x++ postincrement assign first increment later 122 121

// as it prints from left to right output is

// 121 122 122

printf("\n\n");

x =120;
printf("%d %d",x,x++);

// compiler goes due to operator printed

// x++ postincrement assign first increment later 121 120

// x as it is 121 121

// as it prints from left to right output is

// 121 120

printf("\n\n");

x =120;

printf("%d %d",x,++x);

//compiler goes due to operator printed

// ++x preincrement increment first assign later 121 121

// x as it is 121 121

// as it prints from left to right output is

// 121 121

printf("\n\n");

x =120;

printf("%d %d",x++,++x);

//compiler goes due to operator printed

// ++x preincrement increment first assign later 121 122

// x++ postincrement assign first increment later 122 121

// as it prints from left to right output is

// 121 122

printf("\n\n");

return 0; }

OUTPUT :
122 121 122
122 122 120

122 120 122

121 122 122

121 120

121 121

121 122

(c)
#include<stdio.h>

int main(){

int i=5,j;

j=++i + ++i + ++i;

//compiler never add three numbers, it first add two then next one

// due to operator i= 6 7

// ++i + ++i

// value at that place 7 + 7 =14

// due to operator i= 8

// 14 + ++i
// value at that place 14 8

// and hence j=14+8=22 and i=8

printf("%d %d",i,j);

return 0;

OUTPUT :
8 22

(d)
#include<stdio.h>

int main(){

int i =5,j;

j=++i + (++i + ++i);

//due to operator 6 7 8

// j=++i + (++i + ++i)

// vlue at that place 8 + 8 + 8

// and hence j=8+8+8 = 24 and i=8

printf("%d %d",i,j);

return 0;

OUTPUT :
8 24
(e)
#include<stdio.h>

int main(){

int a,b,c,d,i=2;

a= ++i + ++i;

//due to operator 3 4

// a= ++i + ++i

//value at that place 4 + 4

// a=8

printf("a=%d\n",a);

i=2;

b= i++ + i++;

//due to operator 3 4

// b= i++ + i++

//value at that place 2 +3

// b= 5

printf("b=%d\n",b);

i=2;

c= ++i + i++;

//due to operator 3 4

// c= ++i + i++

//value at that place 4+3

// c= 7

printf("c=%d\n",c);

i=2;
d= i++ + ++i;

//due to operator 3 4

// d= i++ + ++i

//value at that place 2 + 4

// d= 6

printf("d=%d\n",d);

return 0;

OUTPUT :
a=8

b=5

c=7

d=6

(f)
#include<stdio.h>

int main(){

int a,b,c,d,i=2;

a= ++i + ++i;

//due to operator 3 4

// a= ++i + ++i

//value at that place 4 + 4

// a=8

printf("a=%d\n",a);

i=2;
b= i++ + i++;

//due to operator 3 4

// b= i++ + i++

//value at that place 2 +3

// b= 5

printf("b=%d\n",b);

i=2;

c= ++i + i++;

//due to operator 3 4

// c= ++i + i++

//value at that place 4+3

// c= 7

printf("c=%d\n",c);

i=2;

d= i++ + ++i;

//due to operator 3 4

// d= i++ + ++i

//value at that place 2 + 4

// d= 6

printf("d=%d\n",d);

return 0; }

OUTPUT :
a=8

b=5

c=7
d=6

THANK YOU

You might also like