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

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for What are the rules about using an underscore in a C++ identifier?

๐Ÿ‘‹ Hey there, fellow tech enthusiasts! ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

In today's blog post, we're going to address an interesting and commonly asked question in the C++ programming language: what are the rules about using an underscore in a C++ identifier? ๐Ÿ˜ƒ๐Ÿ’ป

๐Ÿค” So, let's dive right in, shall we?

When it comes to naming member variables in C++, it's not uncommon to use some sort of prefix to differentiate them from local variables or parameters. For folks familiar with the MFC framework, the convention of using "m_" as a prefix (e.g., "m_foo") is quite popular. On the other hand, some developers prefer using something like "myFoo" to indicate a member variable.

But, here's where it gets interesting! If you come from a C# (or possibly just .NET) background, you might have seen the convention of using just an underscore for member variable names, like "_foo". The question is, does this convention hold up in C++ according to the standard? ๐Ÿค”๐Ÿ“š

๐Ÿ“œ The C++ Standard

According to the C++ standard, using an underscore as the first character of an identifier is allowed. However, there are a few rules we need to keep in mind:

1๏ธโƒฃ Rule #1: Names that begin with an underscore followed by an uppercase letter are reserved for use by the implementation. So, avoid using identifiers like "_Foo" or any variations thereof, starting with an underscore followed by a capital letter.

2๏ธโƒฃ Rule #2: Names that begin with an underscore are also reserved at global or namespace scope. This means it's best to avoid using identifiers like "_bar" for global variables or namespace-level entities.

3๏ธโƒฃ Rule #3: Names that begin with an underscore followed by another underscore anywhere after the first character are also reserved. So, steer clear of identifiers like "__baz".

โœจ Easy Solutions and Best Practices

Now that we know the rules, you might be wondering how to adopt a naming convention that works well in C++. Here are a couple of easy solutions and best practices to guide you:

1๏ธโƒฃ Solution #1: Use a consistent prefix convention, such as "m_" or "my" (e.g., "m_foo" or "myFoo"). This ensures clarity and avoids any possible conflicts with reserved names mentioned earlier.

2๏ธโƒฃ Solution #2: If you find the C# convention appealing, you can use an underscore as a prefix for your member variables (e.g., "_foo"). However, it's crucial to adhere to the rules stated in the standard and avoid any potential conflicts.

๐Ÿ”” Calling All Code Warriors!

We've now demystified the rules and provided you with practical solutions for using underscores in C++ identifiers. ๐Ÿ’กโœจ

Do you have any additional tips or tricks for naming conventions in C++? Share them with us in the comments section below! Let's create a vibrant and engaging discussion on this topic. ๐Ÿ˜๐Ÿ’ฌ

Remember, in the ever-evolving world of programming, it's crucial to stay informed and be aware of language specifications and best practices. Stay curious, keep learning, and happy coding! ๐Ÿ˜Š๐Ÿ’ป

P.S. Don't forget to subscribe to our newsletter for more exciting tech tips, tricks, and tutorials! ๐Ÿ’Œ๐Ÿ˜‰

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

๐Ÿ”ฅ ๐Ÿ’ป ๐Ÿ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! ๐Ÿš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings ๐Ÿ’ฅโœ‚๏ธ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide ๐Ÿš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? ๐Ÿค” Well, my