Can u check Difference?



#include <stdio.h>
 int main()
{
int i = 1, 2, 3;
printf("%d", i);
return 0;
}
 
(A) 1 
(B) 3
(C) Garbage value
(D) Compile time error

Answer: (D)
Explanation: Comma acts as a separator here. The compiler creates an integer variable and initializes it with 1. The compiler fails to create integer variable 2 because 2 is not a valid identifier.

#include <stdio.h>
int main()
{
int i = (1, 2, 3);
printf("%d", i);
return 0;
}
(A) 1
(B) 3
(C) Garbage value
(D) Compile time error

Answer: (B) 

Explanation: The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right
but it is always the result of the last expression which gets assigned.
 
 #include <stdio.h>
 int main()
{
int i;
i = 1, 2, 3;
printf("%d", i);
return 0;
}
(A) 1
(B) 3
(C) Garbage value
(D) Compile time error

Answer: (A)
Explanation: Comma acts as an operator. The assignment operator has higher precedence than comma operator. So, the expression is considered as (i = 1), 2, 3 and 1 gets assigned to variable i.








SHARE

About df

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment