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 1 of 2.

Vikram said:   1 decade ago
#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 ? *p:*q;
}


In this case, why is the output 10 instead of 20?

R@jesh said:   8 years ago
All having thea same doubt in this statement------ return i>=45 ? (*p): (*q);

Actually, I want how it will work it properly from left to right or from right to left in internally?

Please, someone explain it.

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

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

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.

Anusha said:   10 years ago
20 will be the output as the value of i is 10 and 10 is not greater than 45. So *q will be the output which is 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.

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

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

Suchi said:   9 years ago
If we can use printf statement in place of return statement then what will be the output?


Post your comments here:

Your comments will be displayed after verification.