C Programming - Functions - Discussion
Discussion Forum : Functions - Point Out Errors (Q.No. 2)
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;
}
Answer: Option
Explanation:
In a ternary operator, we cannot use the return statement. The ternary operator requires expressions but not code.
Discussion:
9 comments Page 1 of 1.
Vivid said:
1 decade ago
int f(int a)
{
int c;
c=a>20?10:20;
return c;
}
That's rite?
{
int c;
c=a>20?10:20;
return c;
}
That's rite?
(1)
Khushboo said:
1 decade ago
In above code there is function definition before function prototype declaration? Is it correct?.
Subham said:
1 decade ago
Please explain how the prototype of a function is redeclared in main again.
Ganesh said:
1 decade ago
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.
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:
10 years ago
@Ganesh.
You are wrong. Because program execution always starts with main. So it considered first the declaration.
You are wrong. Because program execution always starts with main. So it considered first the declaration.
Saran said:
9 years ago
Please anyone explain the flow of execution.
Mayuu said:
7 years ago
@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.
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:
4 years ago
The ternary operator requires expressions but not code.
Anonymous said:
3 years ago
@Vivid.
Thanks for your answer.
Thanks for your answer.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers