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 1 of 10.
Preethi said:
1 decade ago
#include<stdio.h>
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;
}
int addmult(int ii, int jj)
{
int kk, ll;
kk = ii + jj;
ll = ii * jj;
return(kk);
return(ll);
}
If the program is rewritten as shown above the output is 7 7 how is it possible?
Can any one explain me please?
Akash said:
1 decade ago
Comma (,) operator has left to right associativity, so while returning the rightmost operator will be returned and no error because of this.
Am12cs said:
1 decade ago
int i=3, j=4, k, l;
k = addmult(i, j);
l = addmult(i, j);
A function cannot return two values at a time, k and l.
Hence it an compiler error.
k = addmult(i, j);
l = addmult(i, j);
A function cannot return two values at a time, k and l.
Hence it an compiler error.
Harish said:
1 decade ago
Only return (kk) is being executed each time.
Sachin vishvkrma said:
1 decade ago
Coma (,) operater has right to left associativity, not to left to right.
Sundar said:
1 decade ago
The Comma Operator (,):
The comma operator, written as a comma (,) takes two operands. It evaluates its left operand and discards the value. It then evaluates its right operand, and returns that value as the result of the expression.
7 + 3, 6 * 2 // Gives 12
3,14 // Gives 14 (and not pi)
The comma operator is seldom necessary, except in function-like preprocessor macros, which we advise against anyway.
The commas used to separate the arguments in a call to a method, or the elements in an array, are not comma operators.
The comma operator, written as a comma (,) takes two operands. It evaluates its left operand and discards the value. It then evaluates its right operand, and returns that value as the result of the expression.
7 + 3, 6 * 2 // Gives 12
3,14 // Gives 14 (and not pi)
The comma operator is seldom necessary, except in function-like preprocessor macros, which we advise against anyway.
The commas used to separate the arguments in a call to a method, or the elements in an array, are not comma operators.
Jains said:
1 decade ago
Very nice explanation! sundar.
Have a nice day.
Have a nice day.
SAMIR said:
1 decade ago
Thanks sundar.
Verma said:
1 decade ago
Answer is right, here it will return last return value.
If function is returning return(i,j); then value of j will return.
If function is returning return(i,j); then value of j will return.
Pratik said:
1 decade ago
@ Preethi,,,,,we can return only one value in fuctions if we are not using pointer,,,,,,so when k==addmui(i,j); executes functions is called and and kk is returned fromreturn(kk),and recieved by k,now we cant return two statement so curser goes to main();
again l=admull(i,j);
again kk is retun boy time
hence you are feting to 7; ; ;ie7,7 or 77
again l=admull(i,j);
again kk is retun boy time
hence you are feting to 7; ; ;ie7,7 or 77
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers