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.

Questions by pn_5449   answers by pn_5449

Editorial / Best Answer

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.

Showing Answers 1 - 39 of 39 Answers

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

  • 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

  • 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

  • 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().

  Was this answer useful?  Yes

TejaswiniJ

  • Dec 20th, 2009
 

The sequence of execution has to be printf, f2() and then f1(), the functions f1 and f2 are compiled from before, but not executed until the printf function calls them, the return values of these functions act as arguments to the statement which are then printed on the screen according to the format specifier. Hence printf function reads its arguments from right to left. but it is impossible for f2 and f1 to get executed without executing the printf function! 

  Was this answer useful?  Yes