2

i have read in various places that functions which are declared in main() cannot be called outside main. But in below program fun3() is declared inside main() and called outside main() in other functions, and IT WORKS, giving output 64.here's link http://code.geeksforgeeks.org/7EZZxQ .however,if i change fun3() return type int to void ,it fails to compile,whats reason for this behaviour?

#include <stdio.h>
 #include <stdlib.h>


int main()
{
    void fun1(int);
    void fun2(int);
    int fun3(int); 

    int num = 5;
    fun1(num);
    fun2(num);
}

void fun1(int no)
{
    no++;
    fun3(no);
}

void fun2(int no)
{
    no--;
    fun3(no);

}

int fun3(int n)
{
    printf("%d",n);
}

there is fail in compilation when declaration of fun3() is changed.

 #include <stdio.h>
 #include <stdlib.h>


int main()
{
    void fun1(int);
    void fun2(int);
    void fun3(int);     //fun3 return type changed to void error in compilation

    int num = 5;
    fun1(num);
    fun2(num);
}

void fun1(int no)
{
    no++;
    fun3(no);
}

void fun2(int no)
{
    no--;
    fun3(no);

}

void fun3(int n)   //fun3 return type changed to void error in compilation
{
    printf("%d",n);
}
7
  • While my answer is an educated guess, it would be helpful if you told us what compiler you are using, what version of it, and what error you get when you change the argument (should probably show us the changed code as well). Commented Jun 28, 2015 at 12:31
  • 1
    Compile with all warnings on (-Wall -Wextra -pedantic for gcc), read up on what the compiler tells you and (may be) get enlightened. Commented Jun 28, 2015 at 12:31
  • @JoachimPileborg i have included the link of online compiler in the question... i will update the code with the one where its getting wrong :) Commented Jun 28, 2015 at 12:34
  • Doesn't compile with C99: ideone.com/we74Jf You didn't specify a C Standard, so we can assume it is C99 ( or C11 ). Commented Jun 28, 2015 at 12:36
  • @this aha...!! i think this answered the question..thanks:) Commented Jun 28, 2015 at 12:38

2 Answers 2

4

Prior to the C99 standard, if the compiler saw a function call without a function declaration in the same scope, it assumed that the function returned int.

Neither fun1 nor fun2 declare fun3 before they call it, nor do they see the declaration in main; the declaration of fun3 in main is limited to the body of main. Under C89 and earlier, the compiler will assume that the call to fun3 returns an int. In the first example, the definition of fun3 returns an int so the code will compile under a C89 compiler. In the second example, the definition of fun3 returns void, which doesn't match the assumed int type, so the code fails to compile.

Under C99 and later standards, neither snippet will compile; the compiler no longer assumes an implicit int declaration for a function call. All functions must be explicitly declared or defined before they are called.

Sign up to request clarification or add additional context in comments.

Comments

1

Declarations are only valid in the scope they are declared in. However, in older C standards, the compiler was allowed to guess function declarations if none existed at the time of the call, which is probably what happens in your case: The compiler sees the call of fun3 in fun1, fun2 and fun3, and deduces (guesses) the argument types based on the argument you pass in the call.

3 Comments

so according to you probably they are implementing older standards..?and what about void...why it fails with void fun3(int)? @joachim pilebrog
@codeluv I really can't say, because I don't know exactly what changes you made or what errors you get.
@codeluv The compiler, when it guesses the function declaration, defaults to guess the function returns int, if the actual function definition is different from what the compiler deduces, then you will get an error because the declaration and definition doesn't match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.