Practice Questions - 4 - Operators
Practice Questions - 4 - Operators
Operators
What will be the output of the following C code?
2.
1. #include <stdio.h>
#include <stdio.h>
int main() int main()
{ {
int x = 2, y = 0; int x = 1;
int z = (y++) ? y == 1 && x++ : 0; int y = x == 1 ? getchar(): 2;
printf("%d\n", y);
printf("z=%d\n", z);
}
printf("x=%d\n", x); a) Compile time error
printf("y=%d\n", y); b) Whatever character getchar function returns
return 0; c) Ascii value of character getchar function returns
} d) 2
3. #include <stdio.h>
4. #include <stdio.h>
int main()
int main()
{ {
int x = 1; int a = 2;
int b = 0;
short int i = 2;
int y = (b == 0) ? a :(a > b) ? (b = 1): a;
float f = 3; printf("%d\n", y);
if (sizeof((x == 2) ? f : i) == sizeof(float)) }
printf("float\n");
else if (sizeof((x == 2) ? f : i) == sizeof(short int))
printf("short int\n");
}
5.
6. #include <stdio.h>
#include <stdio.h>
int main()
int main()
{
{
int i=-1;
int y = 1, x = 0;
+i;
int l = (y++, x++) ? y : x;
printf("i = %d, +i = %d \n",i,+i);
printf("%d\n", l);
}
}
7.
#include <stdio.h> 8.
int main() #include <stdio.h>
{ void main()
int i = -5; {
i = i / 3; int x = 5.3 % 2;
printf("%d\n", i); printf("Value of x is %d", x);
return 0; }
}
9. #include <stdio.h> 10. #include <stdio.h>
int main() int main()
{
{ int i=2;
int ans=12,m=10,k; int j=i+(1,2,3,4,5);
k=!((ans<2) && (m>2)); printf(“%d”,j);
printf(“\n%d”,k); }
}
11. #include <stdio.h> 12. #include <stdio.h>
main() int main(){
{ char not;
float i=12.5,j=2.2; not=!2;
printf(“%f”,i%j); printf("%d",not);
} }
14.
13.
int main()
int main(){
{
const int i=4;
int a=1,b;
float j;
b=a,++a,++a;
j = ++i;
printf("\n%d %d",a,b);
printf("%d %f", i,++j);
a=1;
}
b=a++,++a,++a;
printf("\n%d %d",a,b);
a=1;
b=++a,++a,++a;
printf("\n%d %d",a,b);
a=1;
b=(++a,++a,++a);
printf("\n%d %d",a,b);
}
15.
#include <stdio.h>
int main()
{
int a;
a=printf("\nThree") + printf("\nfour") * printf("\nfive") - printf("\nsix");
printf(“%d”,a);
}
16.
#include <stdio.h>
int main()
{
int c = 2 ^ 3;
printf("%d\n", c);
}
17. 18.
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
unsigned int a = 10; int a = 2;
a = ~a; if (a >> 1)
printf("%d\n", a); printf("%d\n", a);
} }
19.
20.
#include <stdio.h>
int main(){
int main()
int i=4,j=7;
{
j = j || i++ && printf("YOU CAN");
printf("hai")||printf("bye");
printf("%d %d", i, j);
return 0;
}
}