C++/C/C++
Expert: Zlatko - 2/1/2011
Question#include <stdio.h>
#include <conio.h>
int f1(int x)
{
return x*x;
}
int f2(int y)
{
return 2*y;
}
int f3(int z)
{
return 3*z;
}
int f4(int p)
{
return 4*p;
}
int main()
{
printf("%d",(f1(1)+(f2(2)*f3(3))/f4(4)));
return 0;
}
Sir Will you please tell me in the printf statement,in which order the fuctions f1,f2,f3 & f4 get executed according to associativity rule?
Thanks in Advance Sir.
Hope you will help me.
AnswerHello Murali
You can check the order by putting in print statements into the functions. With my compiler, the functions are evaluated in this order
f1, f2, f3, f4.
However, the results of the functions are used in the order dictated by the rules of parenthesis and order of operations. The program would have to use the results in this order.
1) multiply the results of f2 and f3
2) divide by the result of f4
3) add the result of f1
The program
#include <stdio.h>
#include <conio.h>
int f1(int x)
{
printf("f1\n");
return x*x;
}
int f2(int y)
{
printf("f2\n");
return 2*y;
}
int f3(int z)
{
printf("f3\n");
return 3*z;
}
int f4(int p)
{
printf("f4\n");
return 4*p;
}
int main()
{
printf("%d\n",
(
f1(1) + (f2(2)*f3(3)) / f4(4)
)
);
return 0;
}
The output
f1
f2
f3
f4
3