Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
What are the Rules for Using an Underscore in a C++ Identifier?
Next article icon

What are the Rules for Using an Underscore in a C++ Identifier?

Last Updated : 29 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, an identifier is a unique name that is used to define a variable, function, class or object, and etc in a program. It can consist of numbers, letters, and underscores and must start with an underscore or letter (can be uppercase or lowercase). No special character is allowed in identifiers. In this article, we will learn the rule about using an underscore in a C++ identifier.

Rules for Using Underscore (_) in C++ Identifier

The following are the rules that we should handle while using an underscore in a C++ identifier:

1. Leading Underscore Followed by a Lowercase Letter

 In C++, an identifier can start with an underscore followed by a lowercase letter but such identifiers are reserved for implementations in the global namespace.

Example:

 int _variable;   // Incorrect usage in global namespace

2. Trailing Underscore

Identifiers ending with a single underscore are not specifically reserved, but it's a common convention to use them for private member variables in classes. It’s a good practice to avoid ending identifier names with an underscore for general purposes.

Example:

int variable_;   // Valid, commonly used for private members

3. Double Underscore Anywhere

Identifiers containing adjacent double underscores are reserved for the compiler and its libraries in all scopes. Using these can interfere with compiler operations and library functionality.

Example:

int __variable;  // Invalid: Reserved for implementation

4. Leading Underscore Followed by an Uppercase Letter

Identifiers starting with an underscore followed by an uppercase letter are reserved for use in all scopes. This is done to avoid conflicts with macros and other identifiers in the standard library.

Example:

int _Variable;   // Invalid: Reserved for implementation

5. Single Underscore

Identifiers consisting solely of a single underscore are allowed, especially in local scopes, but their use is discouraged. They are typically used as placeholders or in very short code segments. However, they can be unclear in their purpose and can potentially conflict with compiler extensions.

Example:

 int _;       // Discouraged: Potentially unclear and error-prone

Conclusion

All of the above rules and conventions regarding the use of the underscores in C++ identifiers are based on the guidelines established by the C++ standard to ensure that the identifiers you use are portable, understandable, and free of conflicts with compiler or system-level names. It's always best practice to follow these rules strictly, especially when writing library code or code that needs to be highly portable.


Next Article
What are the Rules for Using an Underscore in a C++ Identifier?

M

maha123
Improve
Article Tags :
  • C++ Programs
  • C++
  • CPP-Basics
  • CPP Examples
  • misc-cpp
Practice Tags :
  • CPP

Similar Reads

    What Are the Basic Rules and Idioms for Operator Overloading in C++?
    In C++, operator overloading is a form of compile-time polymorphism where an operator is redefined to provide a special meaning beyond its typical operation. It allows developers to use traditional operators with user-defined types (or classes). In this article, we will learn the basic rules and idi
    3 min read
    Why it is important to write "using namespace std" in C++ program?
    In this article, we will discuss the use of "using namespace std" in the C++ program.Need of Namespace in C++As the same name can't be given to multiple variables, functions, classes, etc. in the same scope. So, to overcome this situation, namespace is introduced.ExampleBelow is the C++ program illu
    4 min read
    Why Const is Used at the End of Function Declaration in C++?
    In C++, the const keyword is used to define the constant values that cannot be changed during the execution of the program. Once a value or a function is declared as constant then its value becomes fixed and cannot be changed and if we try to change it, an error is thrown. In this article, we will l
    2 min read
    Overloads of the Different References in C++
    This article focuses on function/method overloads by references, as well as the types of arguments that can be passed. Prerequisites: l-value references.r-value references.Move semantics - std::move(). Overview:l-value refers to a memory location that identifies an object. r-value refers to the data
    12 min read
    Where and Why Do I Have to Put the 'template' and 'typename' Keywords?
    In C++, we have a template keyword to define template classes or functions and allow them to operate with generic types. On the other hand, we have typename keyword which is used to specify that the identifier that follows is a type, particularly in template code to clarify the ambiguity but confusi
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences