C Programming - Functions - Discussion

2. 

Point out the error in the program

#include<stdio.h>
int f(int a)
{
  a > 20? return(10): return(20);
}
int main()
{
    int f(int);
    int b;
    b = f(20);
    printf("%d\n", b);
    return 0;
}

[A]. Error: Prototype declaration
[B]. No error
[C]. Error: return statement cannot be used with conditional operators
[D]. None of above

Answer: Option C

Explanation:

In a ternary operator, we cannot use the return statement. The ternary operator requires expressions but not code.


Vivid said: (Nov 11, 2011)  
int f(int a)
{
int c;
c=a>20?10:20;
return c;
}

That's rite?

Khushboo said: (Apr 9, 2012)  
In above code there is function definition before function prototype declaration? Is it correct?.

Subham said: (Jun 1, 2012)  
Please explain how the prototype of a function is redeclared in main again.

Ganesh said: (Jun 6, 2012)  
1. Actually function is declared only once inside the main().
2. Before the main() here we defined the function.
3. An function can be declared anywhere in the program with one condition that it is declared (i.e. Give prototype) before calling.
Here function is declared inside the main but following the above condition that declaration is before calling.
We can't declare an function after the calling which will give you error as function should have a prototype.

Amit said: (Jan 16, 2016)  
@Ganesh.

You are wrong. Because program execution always starts with main. So it considered first the declaration.

Saran said: (Jul 21, 2016)  
Please anyone explain the flow of execution.

Mayuu said: (Oct 16, 2018)  
@All.

Actually, the program is correct, if normal if statements are used.

First starts the main () then checks the func declaration. Then proceeds and passes the arg. And do such things and terminates.

Yogi said: (Nov 21, 2021)  
The ternary operator requires expressions but not code.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.