The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+14 votes

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

asked in Programming by Veteran (69k points)
edited by | 2k views

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.


4 Answers

+30 votes
Best answer

ans is (D)

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.

answered by Boss (5.1k points)
edited by
how ?
if it had been a function that returns int,then no compiler error or warning,RIGHT?
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??

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

then it will show warning but runs successfuly.
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?
@ 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 :

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

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

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.



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.
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.
+7 votes

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.

answered by (147 points)
+1 vote
answered by Veteran (53.6k points)
that comment dont hav any upvote .....
@digvijay  Not possible

Answer must be D only .
+1 vote
D since foo is defined after main so it should be declared before main.
answered by Junior (585 points)
Absolutely No.

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

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

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 .

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)


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

    double da, db;
    //input da

    db = foo(da);


double foo (double a) {
    return a;



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


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.


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.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

33,646 questions
40,193 answers
38,665 users