Aseem Bansal
Aseem Bansal

Reputation: 6972

How to use a function from a header file without including the header in C?

Suppose I have a header file abc.h which defines a function foo(). How can I use the function foo() in the header file abc.h without using #include<abc.h> in my program?

I could come up with the following solutions

So how can I use a function in a header file(not necessarily from the standard library) without including the header file?

This was an interview question so it is essentially a trick question. Not real use.

Upvotes: 1

Views: 1633

Answers (2)

Dave Rager
Dave Rager

Reputation: 8160

I believe the key word here is "defined" in the header file, not just declared. The header would have to be compiled to an object. To use it, you'd "declare" the function in your source and link with the object file.

That said, I hate interview questions like this since you're never really sure if that's what they are looking for because no one in their right mind would do this in practice.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798944

But this would fail for most of the cases where the header has been pre-compiled into object files

This sentence makes no sense. A header can be used to compile an object file, but they never get compiled into object files; they are always used externally.

Upvotes: 0

Related Questions