# Sequence of Function Execution

Printf("%d%d",f1(),f2());

What is the sequence of function execution of the above statement.
1. printf, f1(), f2()
2. printf, f2(), f1()
3. f1(), f2(), printf
4. f2(), f1(), printf.

mohinuddinkhan

• Member Since Oct-2009 | Oct 30th, 2009

Let's understand this by going through the following code

#include<stdio.h>
int f1();
int f2();

void main()
{
printf("%dt%d",f1(),f2());
}

int f1()
{
printf("f1() enteredn");
return 4;
}

int f2()
{
printf("f2() enteredn");
return 5;
}

O/P-

f2() entered
f1() entered
4        5

we can conclude f2() is 1st executed
then f1() and in the end printf().hence we can say the precedence of execution of functions are from right to left.

• Sep 16th, 2009

4. f2(), f1(), printf.

#### aniket_tal Profile Answers by aniket_tal

• Sep 19th, 2009

1. printf, f1(), f2()

#### mohinuddinkhan Profile Answers by mohinuddinkhan

• Oct 30th, 2009

Let's understand this by going through the following code

#include<stdio.h>
int f1();
int f2();

void main()
{
printf("%dt%d",f1(),f2());
}

int f1()
{
printf("f1() enteredn");
return 4;
}

int f2()
{
printf("f2() enteredn");
return 5;
}

O/P-

f2() entered
f1() entered
4        5

we can conclude f2() is 1st executed
then f1() and in the end printf().hence we can say the precedence of execution of functions are from right to left.

#### avd80 Profile Answers by avd80

• Dec 11th, 2009

Good approach to solve the problem. But you misinterpreted the program output. This of it this way..... f1 () and f2 () are arguments to printf (). So when the program execution reaches printf, it MUST execute printf first. It cannot jump to either f1 or f2. The reason why you get the output as f2, f1, printf is because when printf execution begins, it does not know the value of f1 () or f2 (). Hence, it must first execute these functions in the sequence f2 () and then f1 (). When these functions return a value, the printf "completes" its execution...!

Sounds incredible? Trace that program. You'll see the sequence of execution as printf, f2, and finally f1. After executing f1, the control jumps to the statement after printf.

To sum up, the answer is printf, f2(), f1()

#### filipe Profile Answers by filipe

• Dec 15th, 2009

How could be f1 and f2 be called from printf() ??? Even doesn't make sense.

The compiler generates code to evaluate f2() and f1() before call printf().
The correct is f2(), f1(), printf()

Look, this is the code generated for the example above, I added the comments:

// Call the function f2()
call    f2

// The result is in eax, just copy to ebx
movl    %eax, %ebx

// Call the function f1()
call    f1

// .LC2 is the string used to format the printf, just copy it to edx
movl    \$.LC2, %edx

// Now, insert all the three parameter in the stack
movl    %ebx, 8(%esp)   // ebx that has the result from f2()
movl    %eax, 4(%esp)   // eax that has the result from f1()
movl    %edx, (%esp)     // edx that has the string address

// finally, call the printf() function
call    printf

--Filipe

#### genius_mjk Profile Answers by genius_mjk

• Dec 19th, 2009

First of all there is a call to printf but printf is depend on f1 and f2 to execute first therefore the sequence is f1 () f2() printf().