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 3 of 10.
Prasanth said:
2 years ago
Actually it returns a value like that k=(8,12).
if we give multiple values for integers with brackets, the compiler only considers the last value 12.
Note :
if we declare a variable it considers a last value,
int a=(1,2,3) // a=2.
else we initialize a variable that considers the first value,
int a;
a=(1,2,3)
// a=1.
if we give multiple values for integers with brackets, the compiler only considers the last value 12.
Note :
if we declare a variable it considers a last value,
int a=(1,2,3) // a=2.
else we initialize a variable that considers the first value,
int a;
a=(1,2,3)
// a=1.
K.brahmateja said:
1 decade ago
In the return statement if there is more than one argument, it will the rightmost argument value will be sent to the calling function.
In this program we have return(kk,ll) the rightmost argument here is ll, in the definition ll is assigned to multiplication of the two variables only i.e " 3*4 =12".
In this program we have return(kk,ll) the rightmost argument here is ll, in the definition ll is assigned to multiplication of the two variables only i.e " 3*4 =12".
Shlok said:
1 decade ago
Had there been only "kk" being returned 'addmult()' would return only '7'
but since two different computations are being returned the greater value (i.e. 12)is overriding the smaller value(i.e. 7) and so the result of the both the computations stored in k and l respectively are turning out be 12.
but since two different computations are being returned the greater value (i.e. 12)is overriding the smaller value(i.e. 7) and so the result of the both the computations stored in k and l respectively are turning out be 12.
Sunil kumar said:
1 decade ago
In this prg we can get the value
k=(7,12) and l=(7,12).
In here the comma operator and brackets performs the right to lift.
So that way only it prints 12 and 12.
We can assign k = 7,12 and l = 7,12.
That time we can got 7 and 7 is the output in here it will working in left to right.
k=(7,12) and l=(7,12).
In here the comma operator and brackets performs the right to lift.
So that way only it prints 12 and 12.
We can assign k = 7,12 and l = 7,12.
That time we can got 7 and 7 is the output in here it will working in left to right.
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.
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.
Pravu said:
1 decade ago
@Prashanthi.
It depend on whether you are changing the value in called function or not if you are catching value of I and j through pointer or address and if you are doing any operation in called function then sure the original value of I and j get changed.
It depend on whether you are changing the value in called function or not if you are catching value of I and j through pointer or address and if you are doing any operation in called function then sure the original value of I and j get changed.
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.
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).
For both k and l it happens therefore the o/p is 1212. (just an assumption referring previous questions).
Thi said:
1 decade ago
@preethi:
After encountering a return statement the control of the function should be transferred to the next of the statement where it is called. So it ll always returns the value of the first return statement.
After encountering a return statement the control of the function should be transferred to the next of the statement where it is called. So it ll always returns the value of the first return statement.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers