GATE2005-32

5.5k views

Consider the following C program:

double foo (double);		/* Line 1 */
int main() {
double da, db;
//input da
db = foo(da);
}
double foo (double a) {
return a;
}

The above code compiled without any error or warning. If Line $1$ is deleted, the above code will show:

1. no compile warning or error

2. some compiler-warnings not leading to unintended results

3. some compiler-warnings due to type-mismatch eventually leading to unintended results

4. compiler errors

edited
4

In C99 or C11, standard C requires a function declaration in scope before you call any function. Many compilers do not enforce this restriction in practice unless you force them to do so.

0

https://stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c/2575186#2575186

According to this link, if prototype is not declared before using a function. compiler guesses it based on type of parameters passed to it.

In this case parameters passed are double so it will create the prototype accordingly and process it. Moreover if it finds one more declaration of same function and prototype guessed does not match with given definition, it gives error.

My thought process : as it is

db = foo(da)

da being double it will make a prototype taking double as parameter. But by default return type of user defined function is considered to be int((http://www.lessons2all.com/c_user_defined_functions.php). So compiler will guess it like

int foo(double) which does not match with definition given below main. Hence the error.

2

But by default return type of user defined function is considered to be int

recent days it is removed, i mean there is no default case

before calling a function user should be declared it , otherwise it will create compilation error

0
That means because foo will not be declared before use, it will give error in main right? but the answer selected here is dealing with type mismatch which implies compiler assume prototype.
0

From C99 on ward, implicit declaration of functions is not even allowed.

this line will take care about it !

0

It still allows. There is backward compatibility.

0
is it supporting C99 standard ? or earlier ?
0
The point you said is correct that function has to be declared before calling(good practice) it but it also support implicit declaration which leads to conflict.
0
brother, in earlier to C99

there is a default return type is present for functions ( we know that is int )

but after C99 onwards, there is no default return type is present for functions
0

The only reason the standard allows you to not use them is for backward compatibility with very old code.

0
0

@Shaik Masthan

check the error in the above pic 'conflicting types for foo'. It assumes return type of foo to be int and two functions cannot have different prototype like here both are foo, both taking argument as double but return type is different.

When a function is called without being defined, $C$ compiler assumes it to return "$int$" but here foo is returning "$double$" and hence the compiler will throw type mis-match error.

From $C99$ on ward, implicit declaration of functions is not even allowed.

edited
0
how ?
0
if it had been a function that returns int,then no compiler error or warning,RIGHT?
3
From C99 on ward, implicit declaration of functions is not even allowed. MEANING??

if the function is returning integer and we dont declare its prototype call it before defining, then it wont give any error??
0

But what if it is a void type ? Whether it will give any compilation error or not ? If not why ? If yes why ?

0
then it will show warning but runs successfuly.
1
If line 1 would not have been there then it will be a kind of scenario where we are using a function without declaring then it should throw an error all the time?

Is this true in respect to this question?
3
@ reena_kandari  it will not run and there will be compilation error.

@ saxena0612 it will work fine if the function foo() is returning a integer type value,in this case if you would not have given the function declaration there would be no problem.

see this :https://ideone.com/fCmNbT
5

The function prototype is not needed if the user-defined function is defined before the main() function.

https://www.programiz.com/c-programming/c-user-defined-functions

0
but here, function is defined after main, so prototype error should be there.
0

I found first statement consuffing:-

When a function is called without being defined,

It should be.when function is not declared before call and it is defined later.

4
In ANSI C (meaning C89 or C90), you do not have to declare a function prototype; however, it is a best practice to use them. The only reason the standard allows you to not use them is for backward compatibility with very old code.

If you do not have a prototype, and you call a function, the compiler will infer a prototype from the parameters you pass to the function. If you declare the function later in the same compilation unit, you'll get a compile error if the function's signature is different from what the compiler guessed.

Worse, if the function is in another compilation unit, there's no way to get a compilation error, since without a a prototype there's no way to check. In that case, if the compiler gets it wrong, you could get undefined behavior if the function call pushes different types on the stack than the function expects.

Convention is to always declare a prototype in a header file that has the same name as the source file containing the function.
2
Please edit the answer "The function is called without the declaration, it is already defined after main".
double foo(double); <-- This is called Declaration.

double foo(double a){ ......} <-- This is called definition  of the function.

And the answer would be D.
0
I think
" C compiler assumes it to return "int" but here foo is returning "double" and hence the compiler will throw type mis-match error. "
Where type mismatch has occurred?
0

When I run that same code using void , it gives many warnings but it runs successfully. I don't understand the "void" case.

3
for this question we can say there is type mismtch error but for void it executes successfully.
0

From C99 on ward, implicit declaration of functions is not even allowed.

@Arjun sir is this statement correct?

according to this C still supports implicit declaration for backward compatibility and it can be seen from above snippet.

0

@ so ans must be compiler error due to type mismatch due to backward compatibility(implicit declaration ,that return type of function is int) in this ques ?? and warning when void is used ,am i correct??

0

compiler doesn't care anything about the parameters,it only deals with return type which it will guess implicitly as an integer.

https://onlinegdb.com/rkVaQfwd4

0

@srestha

@Digvijay Pandey

by default c assume return type =int but is there any rule for default parameter type in c or it will acept any?

like int or double or none?

0

@jlimbasiya by default means ?

1
0
By default means if nothing is given then only.

I executed the code in code:blocks

I execute the following code:

//No prototype declaration for the function foo()
int main()
{
int da, db; //int instead of double
da = 20; //initialization
db = foo(da); //implicit declaration of function foo()
printf("%d", db);
}

int foo(int a)
{
return a;
}

Above code will execute successfully even without warnings. The reason might be compiler implicitly assumes that the function foo has a return type of int and also at the end when it sees the function as what he assumes there will be no problem and therefore code executes correctly.

What I also observe is you have to make code:blocks to follow c99 standard. and when I set it and again execute code:

Compiler produces warning: implicit declaration of function 'foo'

but the code executes successfully and prints 20 as output.

but in the question, without a prototype as compiler assumes that foo() return int but as it goes it sees at the end function returns double type. therefore returns an error. therefore option (D) is correct and option (c) is not correct as it says some compiler-warning and not error.

Please correct me if what I am assuming is wrong.

0
that comment dont hav any upvote .....
1
@digvijay  Not possible

Answer must be D only .
1 vote
D since foo is defined after main so it should be declared before main.
0
Absolutely No.

Nowadays, there is no significance of writing functions before or after the main function.

You can write it anywhere.
0
so what should be the answer??
0
@sushmita

Answer is correct , its D only but the logic behind the solution is wrong as given by @_xor_ .

Plz read the next line what I have written .
1

Let me give more insight :--

In c programming the function definition can be done anywhere , after the main or before the main . I think there is no problem.(Plz check the code in gcc compilers)

CODE 1

double foo (double);        /* Line 1 */
int main() {

double da, db;
//input da

db = foo(da);

}

double foo (double a) {
return a;
}

CODE 2

double foo (double);        /* Line 1 */
double foo (double a) {
return a;
}

int main() {

double da, db;
//input da

db = foo(da);

}

You can see the difference as in CODE 1 I have written the function definition just above the main and in CODE 2 I have written function definition just after the main function . There is no problem with these codes.

Coming to function prototype ------

Positioning of function prototype -

1. If function definition is written after main then and then only we write prototype declaration in global declaration section
2. If function definition is written above the main function then ,no need to write prototype declaration

Hope u understand . Let me know If I am wrong . @Arjun Sir

http://www.c4learn.com/c-programming/c-function-prototype-declaration/

0

THAT I KNOW. You are right. I was confused about the point that if we write the function definition after calling the function, without even declaring the function or its prototype will it give compiler error or not???

Because i think C compiler will assume the datatypes of parameters from the call itself but the return type need to be specified.

3

Plz note :   I am doing all the coding in codeblock compiler that is a gcc compiler , could be run in windows environment And I have the latest version .

double foo (double a) {
return a;
}
int main() {
double da, db;
db = foo(da);
}

I have not written the prototype of this function however this code is 100 % correct . No compilation error.

So there are two rules -

1. If ur function is after the main function then u need to define the prototype unless its return type is default return type that is int.
2. If ur function is above the main function then there is no need to define the prototype.

Hope  it helps.

In c  if a function is called before its declaration, the compiler assumes return type of the function as int so deleting line 1 cause error.

In C, a function shouldn't be called before it's declaration. If it happens, the compiler assumes the return type to be int.

Given function foo() returns a, which is a double. Compiler expects an int. This type-mismatch results in an error, and not just a warning.

With Line 1 intact, foo() is declared before being called. It won't throw errors as the compiler would know the return type is double.

Option D

However if the return type of the function is such that it can't mismatch with compiler's default assumption, ie, int we'll just get a warning, not an error.

Notice void is the return type here, not double.

When a function is called before its declaration then it leads to compiler error.

Related questions

1
6.3k views
What does the following C-statement declare? int (*f) (int * ); A function that takes an integer pointer as argument and returns an integer A function that takes an integer as argument and returns an integer pointer A pointer to a function that takes an integer pointer as argument and returns an integer A function that takes an integer pointer as argument and returns a function pointer
The grammar $A \rightarrow AA \mid (A) \mid \epsilon$ is not suitable for predictive-parsing because the grammar is: ambiguous left-recursive right-recursive an operator-grammar
double foo(int n) { int i; double sum; if(n == 0) { return 1.0; } else { sum = 0.0; for(i = 0; i < n; i++) { sum += foo(i); } return sum; } } Suppose we modify the above function $foo()$ ... modification the time complexity for function $foo()$ is significantly reduced. The space complexity of the modified function would be: $O(1)$ $O(n)$ $O(n^2)$ $n!$
double foo(int n) { int i; double sum; if(n == 0) { return 1.0; } else { sum = 0.0; for(i = 0; i < n; i++) { sum += foo(i); } return sum; } } The space complexity of the above code is? $O(1)$ $O(n)$ $O(n!)$ $n^n$