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();
f1, f2, f3
f3, f2, f1
Order may vary from compiler to compiler
None of above
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 1 of 2.

Akash c said:   4 years ago
Logic operators always tell true or false. if it false, then ans =0 if it's right then ans =1.
I think the answer will be 0!!!
Is it correct?

Subrat said:   8 years ago
int x=3,y=5,p=10,z;
z = (x>y)||(x==0)&&(y<<1);
printf("%d",z);

Value of z is 0.
(1)

Akhil said:   8 years 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.
z=1
i.e; false.

Akshay said:   8 years ago
Explain anyone this a = f1 (23, 14) * f2 (12/4) + f3 () with an example.
(1)

Prakash said:   9 years ago
@Nikita Sahane.

The z value is 0 because && operator takes precedence over || operator, so in that expression (x==0) && (y<<1) will execute first i.e 0 and (x>y) || 0 will execute next, so the value of Z is 0.

Loppers said:   10 years ago
According to Sri Krishna like in the example provided by him we gave the precedence to && operator not to the () or left to right associativity, here also although multiplication is having higher precedence we can't be sure whether inside the operand surrounding * i.e. f1 and f2 would be called or not.

Am I right?

VISHWAS.R said:   1 decade ago
Hi.

It depends on compiler.

Because there is internal concept of big endian and small endian. It takes associativity whether left to right or right to left.
(1)

Reshma S Shivalli said:   1 decade ago
Firstly we should know how the compiler compiles i.e. from left to right or right to left hence the option C is right.

Sam said:   1 decade ago
If there are three parenthesis in an expression then according to associativity parenthesis will be solved from left to right isn't it?

Nikita sahane said:   1 decade ago
What will be the answer of above example you gave i.e. value of z ?


Post your comments here:

Your comments will be displayed after verification.