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);
}
Print 10
Print 20
Print 1
Compile error
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 2 of 2.

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.

Priyabrat said:   1 decade ago
If we use,
return i>=45 ? (*p): (*q);
den O/p will be 20.

Kalyan said:   1 decade ago
What would be the correct structure of program. And for that what would be the output. Please tel me.

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).

Akki said:   1 decade ago
If the statement were like return "i>=45 ? *p: *q;" then what would be the output?

Shubhm said:   1 decade ago
Wikiok is rite.

Manishsoni said:   1 decade ago
Wikiok is right, it works properly.

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."

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.


Post your comments here:

Your comments will be displayed after verification.