All the programs are well tested with gcc compiler The output may vary with different compiler as the way of execution (left to right of right to left) is different for different compiler.
Mostly following the programs are based on precedence and associativity.
Find the output of the following c programs
Mostly following the programs are based on precedence and associativity.
Find the output of the following c programs
1. #include <stdio.h>
int main()
{
int a=6;
printf("%d\t%d\n",a++,--a);
return 0;
}
2. #include <stdio.h>
int main()
{
int
a=6,b;
b=a++
+ --a;
printf("a=%d\t
b=%d",a,b);
return
0;
}
3. #include<stdio.h>
int main()
{
int
a=-2;
a=-a+a+!a;
printf("a=%d",a);
return
0;
}
4. #include<stdio.h>
int main()
{
int
a=2,b=1,c,d;
c=a<b;
d=(a>b)&&(c<b)
;
printf("c=%d,
d=%d\n",c, d);
return
0;
}
5. #include<stdio.h>
int main()
{
int a=9,b=15,c=16,e,f;
e=!(a<b || b<c);
f=(a>b)?a-b:b-a;
printf("e=%d, f=%d\n",e,f);
return 0;
}
6. #include <stdio.h>
int main()
{
int a=6,b=3;
a==b;
printf("a=%d\t
b=%d\n",a++,--b);
printf("a=%d\t
b=%d",a,b);
return 0;
}
7. #include<stdio.h>
int main()
{
int a=5; a=6 ;
a=a+5*a;
printf
("a=%d\n",a);
return 0;
}
8. #include<stdio.h>
int main()
{
int
a=2,b=3;
printf
("%d,%d\n",++a,b--);
printf
("%d,%d\n",++a,b++);
printf
("%d,%d",a,b) ;
return
0;
}
9. #include<stdio.h>
int main()
{
int a,b;
a=8++;
printf ("a
=%d b=%d ", a,b);
return 0;
}
10. #include<stdio.h>.
int main()
{
int x,y;
y=++x++;
printf
("x=%d, y=%d",x,y);
return 0;
}
11. #include<stdio.h>
int main()
{
int
x,y;
x==(x+y)--;
printf
("x=%d,y=%d",x,y);
return
0;
}
12. #include<stdio.h>
int main()
{
int a=3,b=5,c;
c=a++ + ++b;
printf
("a=%d,b=%d,c=%d",a,b,c);
return 0;
}
13. #include<stdio.h>
int main()
{
int a=3,b;
b=a++ + ++a;
printf
("a=%d,b=%d",a,b);
return 0;
}
14. #include<stdio.h>
int main()
{
int a=3,b;
b=--a + a++;
printf
("a=%d,b=%d",a,b);
return 0;
}
15. #include<stdio.h>
int main()
{
int
a=3,b=5,c=4,d=6,e;
e=a++ + ++b * c--
- --d;
printf
("a=%d,b=%d,c=%d,d=%d,e=%d",a,b,c,d,e);
return 0;
}
16. #include <stdio.h>
int main()
{
int
a=1;
printf("%d\t%d\n",a%2,a%4);
return
0;
}
17. #include <stdio.h>
int main()
{
printf("%d\t%c\n",2%3,100%51);
return
0;
}
18. #include<stdio.h>
int main()
{
int
a=14,b,c;
a=a%5;
b=a/3;
c=a/5%3;
printf
("a=%d, b=%d, c=%d\n",a,b,c);
return
0;
}
19. #include<stdio.h>
int main()
{
int
a=15,b=13,c=16,m,n;
m=a-3%2+c*2/4%2+b/4;
n=a=b+5-b+9/3;
printf
("m=%d,n=%d\n",m,n) ;
return 0;
}
20. #include<stdio.h>
int main()
{
int
a,b,c,k=10;
k+=(a=5,a=a+2,a=a+b)
;
printf("a=%d,b=%d,c=%d,k=%d\n",a,b,c,k);
return
0;
}
21. #include <stdio.h>
int main()
{
int
a=1,2,3;
printf("a=%d",a);
return
0;
}
22. #include<stdio.h>
int main()
{
int a;
float b;
b=15/2;
printf("%f\t",b);
b=(float)15/2+(15/2)
;
printf("%f\n",b);
return 0;
}
23. #include <stdio.h>
int main()
{
int
a=1+2,b=2,c=(3,4);
printf("a=%d\tb=%d\tc=%d",a,b,c);
return
0;
}
24. #include<stdio.h>
int main()
{
int
a=9; char ch='A';
a=a+ch+24;
printf("%d,
%c\t %d, %c\n",ch,ch,a,a);
return
0;
}
25. #include<stdio.h>
int main()
{
int
a,b,c,d;
a=b=c=d=2;
a*=b+1;
c+=d*=3;
printf("a=%d,
c=%d\n",a, c);
return
0;
}
26. #include <stdio.h>
int main()
{
int a=a+b,b;
printf("a=%d\tb=%d",a,b);
return 0;
}
27. #include<stdio.h>
int main()
{
int
a=3,b=9,temp;
temp=a,a=b,b=temp;
printf("a=%d,b=%d\n",a,b);
return
0;
}
28. #include <stdio.h>
int main()
{
int a=2;
{
int a=3;
printf("a=%d",a);
return 0;
}
}
29. #include<stdio.h>
int main()
{
int a=10, b=3,
max;
a>b?max=a:max=b;
printf("%d",max);
return 0;
}
30. #include<stdio.h>
int main()
{
int
a=5,b=6;
printf("d\t",a=b);
printf("%d\t",a==b);
printf
("%d\t%d\n",a,b) ;
return
0;
}
31. #include <stdio.h>
int main()
{
int
a=3,b=4,c=3,d=4,x,y;
x=(a=5)&&(b=7) ;
y= (c=5) || (d=8);
printf("a=%d,b=%d,c=%d,d=%d,x=%d,y=%d\n",a,b,c,d,x,y)
;
x=(a==6)&&(b=9) ;
y= (c==6) ||
(d=10);
printf("a=%d,b=%d,c=%d,d=%d,x=%d,y=%d\n",a,b,c,d,x,y)
;
return 0;
}
32. #include <stdio.h>
int main()
{
int a=6;
a=a++*a--;
printf("%d\n",a);
return 0;
}
33. #include <stdio.h>
int main()
{
int a=2,b=2,x,y;
x=4*(++a*2+3) ;
y=4*(b++*2+3) ;
printf("a=%d,b=%d,x=%d,y=%d\n",a,b,x,y);
return 0;
}
ANSWER
1. 5 6
2. a=6 b=12
3. a=0
4. c=0, d=1
5. e=0, f=6
6. a=6 b=2
a=7 b=2
7. a=36
8. 3,3
4,2
4,3
9. compilation error
hint:8++; is invalid
10. compilation error
hint:++x++; is invalid
11. compilation error
12. a=4,b=6,c=9
13. a=5,b=8
14. a=3,b=5
15. a=4,b=6,c=3,d=5,e=22
16. 1 1
17. 2 1
18. a=4, b=1, c=0
19. m=17,n=8
20. Garbase value
21. Compilation error
22. 7.000000 14.500000
23. a=3 b=2 c=4
24. 65, A 98, b
25. a=6, c=8
26. Compilation error
27. a=9,b=3
28. a=3
29. compilation error
30. d 1 6 6
31. a=5,b=7,c=5,d=4,x=1,y=1
a=5,b=7,c=5,d=10,x=0,y=1
32. a=42
33. a=3,b=3,x=36,
No comments:
Post a Comment
For any doubt or suggestion you can write here