Search code examples
csyntaxlanguage-lawyerreserved-words

What are the rules about using an underscore in a C identifier?


It's common in C (and other languages) to use prefixes and suffixes for names of variables and functions. Particularly, one occasionally sees the use of underscores, before or after a "proper" identifier, e.g. _x and _y variables, or _print etc. But then, there's also the common wisdom of avoiding names starting with underscore, so as to not clash with the C standard library implementation.

So, where and where is it ok to use underscores?


Solution

  • Good-enough rule of thumb

    Don't start your identifier with an underscore.

    That's it. You might still have a conflict with some file-specific definitions (see below), but those will just get you an error message which you can take care of.

    Safe, slightly restrictive, rule of thumb

    Don't start your identifier with:

    • An underscore.
    • Any 1-3 letter prefix, followed by an underscore, which isn't a proper word (e.g. a_, st_)
    • memory_ or atomic_.

    and don't end your identifier with either _MIN or _MAX.

    These rules forbid a bit more than what is actually reserved, but are relatively easy to remember.

    More detailed rules

    This is based on the C2x standard draft (and thus covers previous standards' reservations) and the glibc documentation.

    Don't use:

    • The prefix __ (two underscores).
    • A prefix of one underscore followed by a capital letter (e.g. _D).
    • For identifiers visible at file scope - the prefix _.
    • The following prefixes with underscores, when followed by a lowercase letter: atomic_, memory_, memory_order_, cnd_, mtx_, thrd_, tss_
    • The following prefixes with underscores, when followed by an uppercase letter : LC_, SIG_, ATOMIC, TIME_
    • The suffix _t (that's a POSIX restriction; for C proper, you can use this suffix unless your identifier begins with int or uint)

    Additional restrictions are per-library-header-file rather than universal (some of these are POSIX restrictions):

    If you use header file... You can't use identifiers with ...
    dirent.h Prefix d_
    fcntl.h Prefixes l_, F_, O_, and S_
    grp.h Prefix gr_
    limits.h Suffix _MAX (also probably _MIN)
    pwd.h Prefix pw_
    signal.h Prefixes sa_ and SA_
    sys/stat.h Prefixes st_ and S_
    sys/times.h Prefix tms_
    termios.h Prefix c_

    And there are additional restrictions not involving underscores of course.