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

R.K said:   9 years ago
Well said @Sundar. Thankyou.

Nilesh said:   9 years ago
In return statement can we return two values at a time?

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.

Rrrr said:   10 years ago
But here the return case we not suppose to do like that it is not possible so it will get error.

Nandkishor said:   10 years ago
Note: In C ', ' act as an operator which returns the last value.

E.g: int i = (1, 2); this expression will store the value 2 in I.

Same is in return statement. The '(kk, ll)' acts as an expression which returns ll.

Value of ll = 12.

Hence the output: 12 12.

KARTHIK said:   10 years ago
RETURN always return the rightmost value.

Rohan said:   1 decade ago
Let a = 15 and b = 16.

Return (b = a+b, b);

Value return will be 31.

This may clear the left to right associativity of operator.

Jain said:   1 decade ago
return (kk, ll) is not possible as 2 values cannot be returned. So, the accumulator value is taken which is 12.

For both k and l it happens therefore the o/p is 1212. (just an assumption referring previous questions).

Pradeepa said:   1 decade ago
12 12 how it is possible please help me?

Abhi said:   1 decade ago
@Ravindra.

A sufficient one Ravindra :-).


Post your comments here:

Your comments will be displayed after verification.