C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 12)
12.
What will be the output of the program?
#include<stdio.h>

int addmult(int ii, int jj)
{
    int kk, ll;
    kk = ii + jj;
    ll = ii * jj;
    return (kk, ll);
}

int main()
{
    int i=3, j=4, k, l;
    k = addmult(i, j);
    l = addmult(i, j);
    printf("%d %d\n", k, l);
    return 0;
}
12 12
No error, No output
Error: Compile error
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
91 comments Page 3 of 10.

Dhiraj said:   7 years ago
The prototype is not defined how will program work is it possible?

Priya said:   7 years ago
Can anyone explain this program?

Please.

Adarsh Saxena said:   8 years ago
Actually when we write return (a, b); then this values first go into the stack so a will go at 0th position and b will go at the 1th position. And stack follows the LIFO criteria that's why the last element will be returned i.e. b.

Subham said:   8 years ago
In return(kk,LL).

The value of LL will be return only commo operation rule.

Bhanu said:   8 years ago
Step 1: int i=3, j=4, k, l; The variables i, j, k, l are declared as an integer type and variable i, j are initialized to 3, 4 respectively.

The function addmult(i, j); accept 2 integer parameters.

Step 2: k = addmult(i, j); becomes k = addmult(3, 4)

In the function addmult(). The variable kk, ll are declared as an integer type int kk, ll;

kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.

ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.

return (kk, ll); It returns the value of variable ll only.

The value 12 is stored in variable 'k'.

Step 3: l = addmult(i, j); becomes l = addmult(3, 4)
kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.
ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.
return (kk, ll); It returns the value of variable ll only.
The value 12 is stored in variable 'l'.

Step 4: printf("%d, %d\n", k, l); It prints the value of k and l

Hence the output is "12, 12".

Ketaki said:   8 years ago
The answer is 12.

Abhishek said:   8 years ago
Here return (kk, ll); it will return only kk as RETURN only return one value i.e first one.

Rohan someshetty said:   8 years ago
Thanks for the explanation @Sundar.

Kamrul Hasan said:   9 years ago
Comma (,) Operator has LEFT to RIGHT associativity rules, so if we assign/return a VARIABLE a list of values with comma then it takes the RIGHT most value.

Ashish ranjan said:   9 years ago
In this program, there are two errors.

1. We cannot return two value at a function.

2. Declaration of the function is not available.

Because of this, it is compile time error.


Post your comments here:

Your comments will be displayed after verification.