in Programming in C retagged by
2,086 views
5 votes
5 votes

What error would the following function give on compilation? 

f(int a, int b) 
{ 
int a; 
a=20; 
return a; 
}
  1. Missing parenthesis is $\textit{return}$ statement.
  2. Function should be defined as $\text{int f(int a, int b)}$
  3. Redeclaration of $a$.
  4. None of these.
in Programming in C retagged by
by
2.1k views

4 Comments

Function should be defined as int f(int a, int b)
1
1
What about C?
3
3
yes sir, It is also needed to consider this option too.

I just missed it that there was redeclaration of a too.

So both A and C is correct one.

kindly suggest if otherwise!!
0
0
Yes both has to be correct but it should be noted that if the return type is not mentioned then by default it takes return type as int.

So more appropriate its redeclaration i think.
1
1

6 Answers

2 votes
2 votes
A) Missing parenthesis in return statement.-> This is wrong because return statement does not require parenthesis.

B) Function should be defined as int f(int a, int b) -> When a function is not defines with any data type in C, it automatically assumes it to return an integer. The same would not be true if the return value would be float or any other data type. So this option is wrong.

C) Redeclaration of variable a. -> This is the correct option because int a is already declared in the argument of the function and inside the function body 'a' is declared once again which will cause error.
1 vote
1 vote
Correct answer is C: Redeclaration of variable "a",which is not allowed and this is the expected error
1 vote
1 vote
Compiler error for multiple definition
1 vote
1 vote
The answer would be 'Redeclaration of a' as a is already a parameter here.

'Function should be defined as int f(int a, int b)' won't be a compilation error, it would give unexpected results in the output.
Answer:

Related questions