C Programming - Expressions - Discussion
Discussion Forum : Expressions - General Questions (Q.No. 4)
4.
Which of the following is the correct order if calling functions in the below code?
a = f1(23, 14) * f2(12/4) + f3();
a = f1(23, 14) * f2(12/4) + f3();
Answer: Option
Explanation:
Here, Multiplication will happen before the addition, but in which order the functions would be called is undefined. In an arithmetic expression the parenthesis tell the compiler which operands go with which operators but do not force the compiler to evaluate everything within the parenthesis first.
Discussion:
12 comments Page 2 of 2.
Sri Krishna said:
1 decade ago
The () doesn't have the highest precedence but grouping is more visible with high precedence operators. take a case:
int x=3,y=5,p=10,z;
z = (x>y)||(x==0)&&(y<<1);
printf("%d",z);
The output should be like (x>y)||(x==0) evaluated first and then the and operation. but as and has higher precedence, it forma a group leading to evaluation of (x>y) first and then anding with ((x==0)&&(y<<1)).
If you get the thing then try to answer the above question.
int x=3,y=5,p=10,z;
z = (x>y)||(x==0)&&(y<<1);
printf("%d",z);
The output should be like (x>y)||(x==0) evaluated first and then the and operation. but as and has higher precedence, it forma a group leading to evaluation of (x>y) first and then anding with ((x==0)&&(y<<1)).
If you get the thing then try to answer the above question.
Mithranjan said:
1 decade ago
() has the highest priority and it has left to right associativity so first f1, f2, f3 will be evaluated and then * is performed between the value returned from f1 and f2 and the + is computed with f3. So ans is A. Am I right?.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers