[comp.lang.c] Motivation behind a particular piec

billc@prism.UUCP (11/25/87)

>    I do not understand the motivation behind a following declaration
>    found in the sources of cpp.
> 
>      #define STATIC
> 
>      Then there are various declartions of the sort
>      STATIC char ch.
> 
>     If STATIC is defined to be null, why would one use it in declarations ?
	
	I admit, this does look unusual.  What you usually see is the
	following:

	#ifdef DEBUG
	#define STATIC
	#else
	#define STATIC static
	#endif

	That way, the run version of the program has the variables
	declared static, while the debug version has them global, which
	on many debuggers makes their values easier to obtain.

	I realize I answered a different question than the one you
	asked, but I suspect the two are related.  It seems that you may
	have a set of code which was half-heartedly trying to do this,
	but was never fully implemented, or perhaps it was found that the
	code needed to have global rather than static declarations for the
	STATIC-ly defined variables.  Either way, the code is a bit sloppy,
	in my opinion.  (Pleas flame me by mail rather than here if I'm
	wrong! :-)


Bill C.		billc@mirror.TMC.COM
		{mit-eddie, ihnp4, wjh12, cca, cbosgd, seismo}!mirror!billc

henry@utzoo.UUCP (Henry Spencer) (12/02/87)

>	That way, the run version of the program has the variables
>	declared static, while the debug version has them global, which
>	on many debuggers makes their values easier to obtain.

Another reason for doing this sort of thing -- the reason why STATIC is
found in my code -- is an incompatibility between some current compilers
and X3J11.  How do you declare the return value of a static function in
advance?  X3J11 (unless it's changed lately, I haven't had a chance to
read the latest draft yet) says that if you want the thing to be static,
the word "static" had better appear in the first mention of it.  So the
forward declaration would be "static char *foo();" or words to that effect.
My current compiler croaks on that, however.  So to cope with my current
environment while getting the code right for the future, I instead write
"STATIC char *foo();", put "#define STATIC static" inside "#ifndef STATIC"
up near the front, and use "-DSTATIC=" to convince my compiler to like it.
-- 
Those who do not understand Unix are |  Henry Spencer @ U of Toronto Zoology
condemned to reinvent it, poorly.    | {allegra,ihnp4,decvax,utai}!utzoo!henry