user3395194
user3395194

Reputation: 89

What is the meaning of "package" keyword in C?

I recently came across the C function with return type as void and package keyword added in front of it.

Could anyone please tell me what the meaning of package in the below mentioned C function prototype represents?

package void function (void) 

The macro definition for the package:

/* function and variable scope modifiers */ 

#define package //accessible within package only,used for functions only

Upvotes: 0

Views: 6427

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263357

Sometimes the information you want to communicate to the reader in a declaration differs from the information that C lets you communicate.

For example, C's use of the static keyword can be confusing. On an object declaration at block scope, it affects the storage duration of the object; on an object declaration at file scope (outside any function body), it affects the linkage.

Programmers will sometimes try to extend the language, by defining macros that more clearly express the intent.

For example, I might write:

#define PUBLIC extern
#define PRIVATE static

PUBLIC void func1(void);
PRIVATE void func2(void);

As far as the compiler is concerned, this is exactly equivalent to:

extern void func1(void);
static void func2(void);

but it might be clearer to a human reader who happens to know, or can guess, what the PUBLIC and PRIVATE macros are supposed to mean.

In your case, you have:

#define package //accessible within package only,used for functions only

so package is replaced by nothing. (IMHO it would have been better to define PACKAGE, so it's obvious to a reader that this is a macro), followed by:

package void function (void);

The word package is ignored by the compiler. It's intended to convey to the human reader that the function function is intended to be "accessible within package only" -- whatever that means.

The C language does not have the concept of a "package". The largest unit of organization for a C program is the translation unit, consisting of a source file along with anything it #includes, directly or indirectly.

The author of the code presumably had something in mind as the definition of a "package". Perhaps it's some well-defined collection of translation units -- in which case there is no C syntax to express the restriction that the author intends.

One problem with this kind of thing is that although package looks like it could be a keyword, it's not enforced by the compiler. Perhaps there's another tool that checks the source files for "correct" use of package -- or perhaps it's entirely up to the maintainers of the code to use it correctly.

Without knowing what a "package" is, it's impossible to be sure what the package pseudo-keyword is supposed to mean. I hope that the term is precisely documented somewhere.

Upvotes: 3

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

It can be a way for the author (programmer) to give the reader (maintenance programmer) some extra information about intentions and structure. From the comment "accessible within package only,used for functions only" it could also be defined as:

#define package static

Now all functions with "package" will be declared as static and have scope only within the "package" (source module/file). I see many programmers do things like this. Of course there is no standard for it so every time I have to find it out again "what's he doing?"

Upvotes: 2

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137448

package is not a keyword in any C standard.

Perhaps it's being #defined by some header file you're not showing us.

Edit: package is being defined to nothing, so it has exactly zero effect in your program. Maybe if you told us what this header file was, and what this other code is you're trying to use, we might understand why it exists.

Upvotes: 4

Related Questions