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;
}
Discussion:
91 comments Page 5 of 10.
Ciri said:
7 years ago
The comma operator evaluates a series of expressions. The value of the comma group is the value of the last element in the list.
int multi_return_args(void)
{
return (44,66);
}
In the example, you show the leading constant expression 44 has no effect, but if the expression had a side effect, it would occur.
For example:
return printf( "we're done" ), 66;
In this case, the program would print "we're done" and then return 66.
so, final answer is based on the last argument that is ll.
int multi_return_args(void)
{
return (44,66);
}
In the example, you show the leading constant expression 44 has no effect, but if the expression had a side effect, it would occur.
For example:
return printf( "we're done" ), 66;
In this case, the program would print "we're done" and then return 66.
so, final answer is based on the last argument that is ll.
Aman kumar mawandia said:
7 years ago
As when admult function is called it is gone to admult definition i.e
k=admult(3,4),in calling function.
Now in function def.
int addmult(3,4)
i.e kk=3+4;
ll=3*4;
return(kk,ll)i.e
return(7,12)i.e
return(12).
AS because return statement only one value i.e 12 from right side.
Same is also done when other function is call .i.e return 12.
k=admult(3,4),in calling function.
Now in function def.
int addmult(3,4)
i.e kk=3+4;
ll=3*4;
return(kk,ll)i.e
return(7,12)i.e
return(12).
AS because return statement only one value i.e 12 from right side.
Same is also done when other function is call .i.e return 12.
Kutrapali said:
7 years ago
Why does it return value? I thought the variable "kk" is not equals to variable "k".
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.
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.
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
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
Ankush said:
1 decade ago
@viren it also depends upon the type of compiler you are using, in gcc compiler it is running of < code blocks > as comma operator is working in it.
Nidhinpradeep said:
1 decade ago
Function arguments are entered into a stack from left to right but they are popped out from right to left. So here return order is (12, 7). Since only one variable to receive 12 is received and 7 is discarded.
Hari said:
1 decade ago
In above programme last return which value taken that value prints two times
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers