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

I am looking at the source code of the β€œless” unix tool from Mark Nudelman, and the beginning of main.c has many of the following features:

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

etc .. globally, before defining main (),

What does the public mean in this context? And more importantly, where can I find this information on my own? I searched for the use of countless query combinations and could not find this information or any detailed link to C.

+4
source share
4 answers

The less.h file has your answer:

 #define public /* PUBLIC FUNCTION */ 

It seems that public is only used as a marker for public / global functions and variables. When it is compiled, it expands to zero.

How to find this information?

  • Locate the .c file at the top with the address of the identifier for which you want more information
  • If you didn’t find any declaration, look for the #include directives
  • Open any included file and find the declaration of what you are looking for.
  • Repeat step 2 for each included file.

In this case, it was pretty simple.

+21
source

This has nothing to do with C as such. If you look in the include less.h file, you will see that the author has defined a number of preprocessor instructions. Some of them, as "public", are most likely readable. For instance:

  /* * 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 publication is defined. This has not changed anything and, as you have already disassembled it on a global scale. However, it is more readable and more suspicious that it is on a global scale. In addition, it can be argued that if the source is written sequentially like this, and a new version of C appears that has a public keyword, it is a matter of overriding the header file and recompiling for its actual use.

Pre-processed tricks like this can even be used in clever ways so that the same source compiles in different languages ​​(e.g. C ++ and Java). This is not what you should do, but it is possible.

Parameters such as HAVE_VOID, which you see in the example with less.h above, are usually specified as compiler (actually preprocessor) parameters at compile time. Therefore, if you have a compiler and a version of C that supports the void keyword, you must compile your source with:

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

Everywhere, when the author uses VOID_POINTER in the source, it will actually be considered by the compiler as:

 void * 

If you did not specify HAVE_VOID, the compiler will use instead

 char * 

which is a reasonable replacement.

TIP: Check the compiler options to see if you can just pre-process your sources. So you can look at the actual source that is sent to the compiler.

+4
source

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

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

Or just run ctags before you start editing anything. When you see a definition that you do not understand, hover over it and type ^] (this control is the right square bracket and will work in editors similar to vi).

+2
source

C does not have the keyword "public", so it is probably a macro defined in the smaller source code.

+1
source

All Articles