Gursheesh Singh - 11 months ago 36

C Question

Here Is my code:

`#include <stdio.h>`

int mul(int,int);

int main()

{

int sum,m,n;

scanf("%d%d",&m,&n);

sum=mul(10,mul(m,n));

printf("%d",sum);

}

int mul(int x,int y)

{

int sum;

sum=x+y;

return(sum);

}

Input

10

5

Output

25

Can someone tell me why I get 25 as output? Was the function called 2 times?

One during parameters and other time during sum?

Answer Source

It's perfectly simple:

```
sum=mul(10,mul(m,n));
```

You're calling `mul()`

with 10 as the first argument, and the return value of `mul(m, n)`

as the second argument.

`m`

and `n`

are 10 and 5, so `mul(10, 5)`

returns 15. The statement in your `main`

function then evaluates to this:

```
sum = mul(10, 15);
```

Which is 25.

TL;DR: *yes, mul() is called twice. Once with m and n as arguments. The second time with the sum of m and n, adding 10*

Using a debugger, or even looking at the assembler output generated by the compiler would've told you there were 2 successive calls to `mul`

.

And yes, as others have rightfully pointed out: reading the help section (in particular how to ask) would be a good idea. It explains that you're expected to do the sensible debugging/diagnostic steps yourself. Only if that didn't solve the problem should you post a question here:

Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself.

You merely state that, given input X, you get output Y, and you don't know why.