C Programming - Functions - Discussion
Discussion Forum : Functions - Find Output of Program (Q.No. 9)
9.
What will be the output of the program?
#include<stdio.h>
int check (int, int);
int main()
{
int c;
c = check(10, 20);
printf("c=%d\n", c);
return 0;
}
int check(int i, int j)
{
int *p, *q;
p=&i;
q=&j;
i>=45 ? return(*p): return(*q);
}
Answer: Option
Explanation:
There is an error in this line i>=45 ? return(*p): return(*q);. We cannot use return keyword in the terenary operators.
Discussion:
19 comments Page 1 of 2.
Himanshu said:
1 decade ago
Why we cannot use return keyword in the terenary operators. If there is any logic behind this then please let me know.
Wikiok said:
1 decade ago
"return i>=45 ? *p: *q;" is good.
From another forum: "That won't work; 'return' is a statement and the ternary operator ?: wants expressions. It just won't compile."
From another forum: "That won't work; 'return' is a statement and the ternary operator ?: wants expressions. It just won't compile."
Manishsoni said:
1 decade ago
Wikiok is right, it works properly.
Shubhm said:
1 decade ago
Wikiok is rite.
Akki said:
1 decade ago
If the statement were like return "i>=45 ? *p: *q;" then what would be the output?
Chikki said:
1 decade ago
In this, the i value doesnot clear because we doesnot know where the i is stored?so if we use printf ("i=%d", i) ;then we will understand the output (without presence of return statement).
Kalyan said:
1 decade ago
What would be the correct structure of program. And for that what would be the output. Please tel me.
Priyabrat said:
1 decade ago
If we use,
return i>=45 ? (*p): (*q);
den O/p will be 20.
return i>=45 ? (*p): (*q);
den O/p will be 20.
Yogendrasinghpepe@gmail.com said:
1 decade ago
We don't need to return because values are updated during pointer operation. I think this is correct reason.
Aditi said:
1 decade ago
What kind of values return can return, please explain me detail?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers