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.

Sonit said:   3 years ago
In ternary, we use only statements not the operations.

Nakul said:   5 years ago
A function can have only one return keyword.

Muthu said:   8 years ago
We can use only one return key in ternary operator.
(1)

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.

Mohan said:   8 years ago
There is a problem of type casting b/w pointer and integer @Vikram.

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

Sherine said:   10 years ago
I am asking the same question of @Vikram.

Why the out put is 10 NOT 20 as expected?

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.

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?

Aditi said:   1 decade ago
What kind of values return can return, please explain me detail?


Post your comments here:

Your comments will be displayed after verification.