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 4 of 10.

Sohom Chowdhury said:   5 years ago
In the given question, the comma(,) acts as an operator & the comma operator returns the rightmost operand in the expression and simply evaluates the rest of the operands and finally rejects them.
(2)

Dimple said:   10 years ago
The output will be 12 12.

I checked in Dev CPP. It takes the right most one in the return statement because according to the comma operator has left to right priority. I hope it will helps for you.

Ajay said:   1 decade ago
Answer is soo simple guys. Whenever we try to return two values through return the last value will only gets result tats why both the time ll value returns and get stored k an l. Simple as that.

PRASHANT o_0 said:   1 decade ago
return (kk,ll);
here "," has left to right
so returning steps are
STEP 1:
return kk //here after getting a comma
STEP 2:
//discard kk
return ll
.
.
so finaly ll will be returned

Robert said:   9 years ago
Hi, guys.

This will provide and prove the needed answer.

#include <stdio.h>

int func()
{
return (3,14);
}

int main()
{
printf("%d\n", func());
return 0;
}

Sir.alid said:   9 years ago
We can return only one value at the time and in the program given return (ll, kk); Hence the compile take only one value of return (ll, kk); left to right and value gives us of kk = 12.

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.

Victor said:   9 years ago
It is because of the brackets in the return statement. And brackets have to l precedence, that the reason why only multiplicative part is returned back to the main function.

Pradeep said:   1 decade ago
If function return more then one value to only one L value then right argument will be taken,

Ex:
a=func();

And,
func{
-----------
-------
return (c,b);
}

Then a==b.

Ashish mishra said:   6 years ago
Since ', ' comma operator is right-left associative right most value will be return at the returning time. This is the simplest explanation of this problem.

Thank you.


Post your comments here:

Your comments will be displayed after verification.