Home > Backend Development > C++ > Can I Use Underscores in C Identifiers Without Breaking the Standard?

Can I Use Underscores in C Identifiers Without Breaking the Standard?

Linda Hamilton
Release: 2024-12-28 21:25:12
Original
571 people have browsed it

Can I Use Underscores in C   Identifiers Without Breaking the Standard?

Is It Permissible to Use an Underscore in a C Identifier?

C employs certain conventions for naming member variables, such as prefixing them with a character like 'm_' to differentiate them from local variables and parameters. However, the use of underscores as a prefix, as seen in other frameworks like .NET, raises questions about its validity in the C standard.

The Rules Governing Underscore Usage

According to the C standard, the following rules apply to the use of underscores in identifiers:

Reserved Scopes

  • Names beginning with an underscore followed by an uppercase letter or containing adjacent underscores are reserved for implementation use in any scope.

Global Namespace

  • Identifiers starting with an underscore are reserved for the global namespace.

std Namespace

  • Everything in the std namespace is reserved, including specializations of templates.

C99 Restrictions

While not directly applicable to C , the C99 standard further restricts the use of underscores:

  • Identifiers beginning with an underscore followed by an uppercase letter are reserved for any use.
  • Identifiers beginning with an underscore are reserved as file scope identifiers.

Other Restrictions

  • Starting identifiers with underscores is generally discouraged.
  • Double underscores (__) should be avoided altogether.
  • Ending identifiers with _t is reserved by the POSIX standard for additional type names.

POSIX Standard Reserved Identifiers

The POSIX standard also reserves numerous identifiers that may conflict with normal code usage, including those starting with prefixes like "E," "is," "to," and "LC_."

Conclusion

While using underscores in C identifiers is technically allowed, adhering to the rules outlined above is recommended to avoid potential conflicts and confusion.

The above is the detailed content of Can I Use Underscores in C Identifiers Without Breaking the Standard?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template