© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

9. What is operator precedence?

Ans:
Operator precedence defines the order in which C evaluates expressions. e.g. in the expression a=6+b*3, the order of precedence determines whether the addition or the multiplication is completed first. Operators on the same row have equal precedence.

Ex - 1: What is the output?

main()
{
int i=5,j=10;
i=i&=j&&10;
printf("%d %d",i,j);
}

Ans:
1 10

Explanation:
The expression can be written as i=(i&=(j&&10)); The inner expression (j&&10) evaluates to 1 because j==10. i is 5. i = 5&1 is 1. Hence the result.

Ex - 2: What is the output?
main()
{
int i=4,j=7;
j = j || i++ && printf("YOU CAN");
printf("%d %d", i, j);
}

Ans:
4 1

Explanation:
The boolean expression needs to be evaluated only till the truth value of the expression is not known. j is not equal to zero itself means that the expression’s truth value is 1. Because it is followed by || and true || (anything) => true where (anything) will not be evaluated. So the remaining expression is not evaluated and so the value of i remains the same.
Similarly when && operator is involved in an expression, when any of the operands become false, the whole expression’s truth value becomes false and hence the remaining expression will not be evaluated.
false && (anything) => false where (anything) will not be evaluated.

Ex - 3: What is the output?

main()
{
register int a=2;
printf("Address of a = %d",&a);
printf("Value of a = %d",a);
}

Ans:
Compier Error: '&' on register variable
Rule to Remember:
& (address of ) operator cannot be applied on register variables.


SLogix Student Projects
bottom