carl.hiass
carl.hiass

Reputation: 1774

Should static declarations go in a header file

For public functions I'll put those in a header file such as:

// tree.h
bool TreeIsEmpty(const Tree *ptree);
bool TreeIsFull(const Tree *ptree);
size_t TreeItemCount(const Tree *ptree);

However, what about internal implementation items? Do those only go in my tree.c file or do I also put those into my tree.h file? For example, let's say for TreeItemCount I have a prototype such as:

// tree.c
static bool TreeItemCountInternal(const Tree *ptree);

Do I do anything in the header file with that?

Upvotes: 0

Views: 60

Answers (2)

Z4-tier
Z4-tier

Reputation: 7978

If you were to declare TreeItemCountInternal without designating it as static:

bool TreeItemCountInternal(const Tree *ptree);

then you could put the forward declaration in the header file, and #include that header in you .c files. You might consider doing that if your project spans multiple .c files and you need to share a function across them. The static declaration, when applied to a function, limits it's scope to the translation unit in which it was declared (see this answer for an excellent explanation of what that means from a practical perspective).

Remember that one of the first steps your compiler takes when building a program is to take all of your #include directives, find the file in quesetion, and paste it's contents in place of the #include statement.

There is no dogmatic rule that says you should always put the declaration in the header (although it's often good practice), and the act of doing so-- in and of itself-- does not confer any particular benefit or status (but it could make a lot of difference, depending on how the rest of the program is structured).

Upvotes: 0

Bill Lynch
Bill Lynch

Reputation: 81926

static bool TreeItemCountInternal(...); has internal linkage, so you wouldn't put anything in the header file.

Upvotes: 2

Related Questions