[comp.lang.c] Variable Name Conventions

gordon@hymir.cs.cornell.edu (Jeffrey Adam Gordon) (01/19/90)

I have seen C source with at least three different types of variable
names.  Some are just 'var' while others are either '_var' or '__var.'
What I'd like to know is:  is there any convention for using
underline (and for that matter, CAPS) in C variable/function/structure
names.  Does anyone have a neat system they like to use when, say,
defining typedefs (such as "all typedefs will end in '_t') or
pointers (like '_p').

I think you get the picture.  Thanks to all in advance.

- jag -

henry@utzoo.uucp (Henry Spencer) (01/20/90)

In article <36190@cornell.UUCP> gordon@cs.cornell.edu (Jeffrey  Adam Gordon) writes:
>I have seen C source with at least three different types of variable
>names.  Some are just 'var' while others are either '_var' or '__var.'
>What I'd like to know is:  is there any convention for using
>underline (and for that matter, CAPS) in C variable/function/structure
>names...

Ignoring some fine points, names with leading underscores are basically 
reserved for use by the implementation.  You use such names for your own
purposes at your peril.  The implementation may make some such names
available to you for specific purposes:  for example, __STDC__ as a
preprocessor macro to test whether you've got an ANSI-standard compiler.

There are various naming schemes for identifiers.  Probably the most
widely used is a vague standard common in Unix sources and among long-time
C programmers:  type names end in _t, macro names are all caps,
ordinary variable names are all lowercase, and there are no particular
stylistic conventions depending on variable type.  There is also the
convention of starting structure member names with xx_, where xx is
an abbreviated version of the structure name, but that's largely an
artifact of now-obsolete namespace structure in early compilers.
-- 
1972: Saturn V #15 flight-ready|     Henry Spencer at U of Toronto Zoology
1990: birds nesting in engines | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

karl@haddock.ima.isc.com (Karl Heuer) (01/20/90)

In article <36190@cornell.UUCP> gordon@cs.cornell.edu (Jeffrey  Adam Gordon) writes:
>I have seen C source with at least three different types of variable
>names.  Some are just 'var' while others are either '_var' or '__var.'
>What I'd like to know is:  is there any convention for using
>underline (and for that matter, CAPS) in C variable/function/structure
>names.  Does anyone have a neat system they like to use when, say,
>defining typedefs (such as "all typedefs will end in '_t') or
>pointers (like '_p').

If you're a user (rather than the implementor of the C runtime system) then
you should avoid creating any names beginning with underscore, as most of
these are reserved by ANSI C.

Likewise, you should probably not use _t for your own typedefs, because POSIX
reserves this entire class for use in <sys/types.h>.

Beyond this, it's pretty much personal convention, which should probably be
discussed off-line to keep the S/N ratio from deteriorating too badly.

Karl W. Z. Heuer (karl@haddock.isc.com or ima!haddock!karl), The Walking Lint

donp@na.excelan.com (don provan) (01/20/90)

>There is also the
>convention of starting structure member names with xx_, where xx is
>an abbreviated version of the structure name, but that's largely an
>artifact of now-obsolete namespace structure in early compilers.

Henry, you've said this several times recently.  It just isn't true.
There are many advantages to this convention.  The most obvious is
that, while it's easy to find all occurrences of "xx_len" in a large
body of code, it's difficult and meaningless to find all occurrences
of "len".

Personally, i think it would be more accurate to say the convention of
NOT having structure ID in field names is largely an artifact of
now-obsolete programming practices which emphasized brevity at the
expense of maintainability.

						don provan
						donp@excelan.com

dah@cup.portal.com (Dave Albert Harrison) (01/21/90)

I understand that Microsoft and many Microsoft Windows programmers
favour the "Hungarian Notation" named after a Hungarian at Microsoft.

One prefixes all variable names with a type indicator (e.g., l_count
indicates long and sz_name indicates a null-terminated string).  This
allows one to look at the body of the code and instantly recognize
variable types without having to look for their definitions.

The BBN (Bolt, Beranek & Newman) convention asks you to capitalize
separate words within a variable name (e.g., StringLength).

Many programmers prefix procedure names with a subsystem identifier.
This avoids naming conflicts and helps you search for procedure
definitions.  One doesn't usually need to do this with variable
names because one tries to avoid cross-subsystem variable references.

randall@uvaarpa.virginia.edu (Randall Atkinson) (01/22/90)

There is still the matter of subsystems composed of several modules.
In such cases, the practice of prefixing all identifiers external to
a module with an abbreviated module identifier really pays off.

More than just avoiding namespace collisions, it aids developers
in finding out where the procedure lives and what its parameters are
and what service it provides.  This is especially helpful in environments
where the external header file for a module contains meaningful comments
about public identifiers.  

All of these comments (and for that matter those in the articles I'm
following up to) really aren't C specific so I've redirected followups
elsewhere...