C Programming :: Floating Point Problems - Discussion
Home > C Programming > Floating Point Problems > MCQs Questions Discussion
3 / 6
What would be the value of i and k in below C program?
#include<stdio.h>
void main() {
int i,j,k;
j = 5;
i = 2 *j/2;
k = 2 *(j/2);
}
#include<stdio.h>
void main() {
int i,j,k;
j = 5;
i = 2 *j/2;
k = 2 *(j/2);
}
Ai = 5, k = 5
Bi=4,k=4
Ci = 5, k = 4
Di = 4, k = 5
Answer: Option (Login/Signup)
in first statement i = 2 *j/2; three operators have been used i.e = , * , / .
Show Explanation
but if you see in second statement () has been used. why?
in first statement i = 2 *j/2; three operators have been used i.e = , * , / .
operator = has priority 14 and both operator * & / have priority as 3.
however operator * has high precedence than / so in above statement first multiplication will take place and then division will happen so that i will carry 5.
Now, in second statement () has been used for j/2. operator () (function call operator has priority 1 and highest precedence in c operator.) so the first j/2 operation will take place and then 2 * result of j/2 (i.e 2). so, k will carry 4.
An answer will be i = 5 and k = 4.
Asked In ::
STEP-BY-STEP
at first, glance looks like i value will be 4 and k value will be 4 too.
but if you see in second statement () has been used. why?
in first statement i = 2 *j/2; three operators have been used i.e = , * , / .
operator = has priority 14 and both operator * & / have priority as 3.
however operator * has high precedence than / so in above statement first multiplication will take place and then division will happen so that i will carry 5.
Now, in second statement () has been used for j/2. operator () (function call operator has priority 1 and highest precedence in c operator.) so the first j/2 operation will take place and then 2 * result of j/2 (i.e 2). so, k will carry 4.
An answer will be i = 5 and k = 4.
Read Full Answer
Report Error
Please Login First Click Here