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 9 of 10.
Yash said:
1 decade ago
@ Preethi
We can write multiple return statements in a function but it is not useful without conditional statements because at first return statement it returns next to where it is called. So in your program return(kk) always executes then it return the value of kk, the return(ll) statement never execute. So the output is 7, 7.
We can write multiple return statements in a function but it is not useful without conditional statements because at first return statement it returns next to where it is called. So in your program return(kk) always executes then it return the value of kk, the return(ll) statement never execute. So the output is 7, 7.
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
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.
SAMIR said:
1 decade ago
Thanks sundar.
Jains said:
1 decade ago
Very nice explanation! sundar.
Have a nice day.
Have a nice day.
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.
Sachin vishvkrma said:
1 decade ago
Coma (,) operater has right to left associativity, not to left to right.
Harish said:
1 decade ago
Only return (kk) is being executed each time.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers