edited by
16,042 views
51 votes
51 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

edited by

8 Answers

Best answer
84 votes
84 votes

Answer 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$ onwards implicit declaration of functions is not even allowed.

edited by
19 votes
19 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.

4 votes
4 votes

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.

3 votes
3 votes
Answer:

Related questions

37 votes
37 votes
4 answers
2
Kathleen asked Sep 22, 2014
23,300 views
The grammar $A \rightarrow AA \mid (A) \mid \epsilon$ is not suitable for predictive-parsing because the grammar is:ambiguousleft-recursiveright-recursivean operator-gram...
13 votes
13 votes
3 answers
4
Kathleen asked Sep 22, 2014
12,351 views
A common property of logic programming languages and functional languages is:both are procedural languages both are based on $\lambda$-calculusboth are declarativeboth us...