0
    #include <stdio.h>
    void bar();

    int main()

    {

        void foo();

        printf("1 ");

        foo();
        bar();

    }

    void foo()

    {

        printf("2 ");

    }
    void bar()
    {
        foo();
    }

Output:

1 2 2

Shouldn't the function foo() be visible only inside main() ?
But this is not the case.
Are function declaration in C always to every functions?

3
  • 2
    Move the definition of bar() above foo() and try again. Commented Jan 30, 2014 at 17:20
  • @SLaks Does it mean that once functions are defined it is visible to all other functions defined later? Commented Jan 30, 2014 at 17:23
  • @PankajMahato I replied to your question on the comment on my answer. Commented Jan 30, 2014 at 17:32

2 Answers 2

3

let me explain commenting the original code you pasted :)

#include <stdio.h>
void bar(); //so, we declared bar here, thus it will be called successfully later

int main()

{

    void foo(); //we declared foo here, thus the foo two statements below also can be called

    printf("1 ");

    foo(); //this works because the void foo(); some lines above
    bar(); //this works because of bar() outside the main()

}

void foo() //here foo is defined

{

    printf("2 ");

}
void bar() //here bar is defined
{
    foo(); //this works because foo was declared previously
}

Indeed, you are both right, and wrong, right in the sense that the first declaration of foo() goes out of scope after main ends. Wrong in the sense that the linker would confuse stuff, the linker, that decides what function call calls what, can find the foo outside the main and figure that is the same foo declared inside the main.

That said, you should not declare functions inside other functions...

Also here is a better proof of what you want to know:

I tried to compile this:

#include <stdio.h>
void bar();

void bar()
{
    void foo();
    foo();
}

int main()

{



    printf("1 ");

    foo(); //line 18
    bar();

}

void foo()

{

    printf("2 ");

}

It gave me this error: prog.c:18:9: error: implicit declaration of function ‘foo’ [-Werror=implicit-function-declaration]

This is because the foo() inside bar() is valid, but the foo() on main is not, because although foo() is declared before main, it is still valid only inside bar()

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

3 Comments

@PankajMahato this should not even work in first place, the C standard (at least up to c99, if someone can tell me if c11 consider this valid, you are welcome), forbids DEFINING (instead of declaring) functions inside another function.
So, why it compiled the way it did in your compiler, then only your compiler can tell you, because it is custom compiler behaviour (not a behaviour from the standard)
Try using Ideone on c99 strict mode (instead of normal C mode, or a nonstrict mode) and it won't work.
1

foo() is visible in bar() because it has been defined before. If you implement foo() after bar() there will be a compiler error

#include <stdio.h>
void bar();

int main()

{

    void foo();

    printf("1 ");

    foo();
    bar();

}


void bar()
{
    foo();
}
void foo()

{

    printf("2 ");

}

brings:

'foo': identifier not found

Comments

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.