Tzafrir
Tzafrir

Reputation: 1841

In C, what does "public" mean when put before a global variable?

I'm going through the source code of the "less" unix tool by Mark Nudelman, and the beginning of main.c has many of the following:

public int  logfile = -1;
public int  force_logfile = FALSE;
public char *   namelogfile = NULL;

etc. in the global scope, before the definition of main(),

What does public mean in this context? And more important, where can I find this information by myself? I searched using countless query combinations, and could not find this information, or any thorough C reference.

Upvotes: 6

Views: 20324

Answers (4)

William Pursell
William Pursell

Reputation: 212298

The definition of public as an empty pre-processor macro has been addressed in other answers. To find the definition, you probably want to use a tool like ctags/etags or cscope. (There are many tools to scan a source tree to generate this information.) For example, you can find the definition of public at line 55 of less.h by invoking:

$ ctags -dtw *.c *.h
$ vi -t public

Or, simply run ctags before you start editing anything. When you see a definition you don't understand, put the cursor on it and type ^] (that's control-right square bracket, and will work in vi-like editors.)

Upvotes: 2

Dan Gøran Lunde
Dan Gøran Lunde

Reputation: 5343

This has nothing to do with C as such. If you look in the include file less.h you will see that the author has defined a number of preprocessor instructions. Some of them like 'public' is most likely for readability. E.g.:

 /*
 * Language details.
 */
#if HAVE_VOID
#define VOID_POINTER    void *
#else
#define VOID_POINTER    char *
#define void  int
#endif
#if HAVE_CONST
#define constant    const
#else
#define constant
#endif

#define public      /* PUBLIC FUNCTION */

See how public is defined. It's translated to nothing and as you have already figured out it's in the global scope. However it's more readable and more obious that it's in the global scope. Also, one could argue that if the source is written consistently like this and a new version of C emerges that does have a public keyword, it's a matter of redefining the the header file and recompile to actually use it.

Preprocessing tricks like this can even be used in clever ways to have one source compile in different languages (like C++ and Java). This is not something you should be doing, but it's possible to it.

The options like HAVE_VOID you see in the example from less.h above are usually specified as compiler (actually preprocessor) options on compile time. So if you have a compiler and a version of C that supports the void keyword you would compile your source with:

g++ -g -DHAVE_VOID -Wall myprog.C -o myprog

Everywhere the author uses VOID_POINTER in the source would then actually be considered by the compiler as:

void * 

If you didn't specify HAVE_VOID the compiler would instead use

char * 

which is a reasonable substitue.

TIP: Check your compiler's options to see if you have an option to just preprocess your sources. That way you can look at the actual source that gets sent to the compiler.

Upvotes: 4

Timbo
Timbo

Reputation: 28050

In the file less.h is your answer:

#define public      /* PUBLIC FUNCTION */

It seems like public is only used as a marker for public/global functions and variables. When compiled, it is expanded to nothing.

How to find this information?

  1. Search the .c file from top to the location of the identifier you want more information about
  2. If you do not find any declaration, look for #include directives
  3. Open any included file and look for the declaration of what you are looking for
  4. Repeat from step two for every included file

In this case, that was pretty simple.

Upvotes: 23

sepp2k
sepp2k

Reputation: 370212

C doesn't have a keyword "public", so it's probably a macro defined in the less source code somewhere.

Upvotes: 2

Related Questions