Ready to practice?
Sign up to access interactive coding exercises and track your progress.
C++ Naming Conventions
Learn C++ keywords and best practices for naming variables and identifiers.
Prerequisites
C++ Keywords
Keywords are reserved words that have special meaning in C++. You cannot use them as variable names, function names, or other identifiers.
Common Keywords
int double char bool string
if else while for do
class public private return void
const static true false nullptr
Examples of Invalid Uses
int int = 5; // Error: 'int' is a keyword
double class = 3.14; // Error: 'class' is a keyword
bool return = true; // Error: 'return' is a keyword
Identifier Naming Rules
Identifiers are names you give to variables, functions, etc. They must follow these rules:
Rule 1: Start with Letter or Underscore
// Valid
int age;
int _count; // Valid but see warning below
int myVariable;
// Invalid
int 2count; // Cannot start with number
int #value; // Cannot start with symbol
While underscores are technically allowed, avoid these patterns in modern C++:
_Count
- Underscore + uppercase (reserved by compiler)__variable
- Double underscore (reserved by compiler)_global
- Single underscore in global scope (reserved by compiler)
Rule 2: Only Letters, Numbers, Underscores
// Valid
int user_name;
int count2;
int totalAmount;
// Invalid
int user-name; // Hyphen not allowed
int my variable; // Space not allowed
int cost$; // $ symbol not allowed
Rule 3: Case Sensitive
int age;
int Age; // Different variable!
int AGE; // Also different!
Rule 4: Cannot Be Keywords
// Invalid
int class;
int return;
int if;
Naming Conventions
camelCase (Recommended)
int studentAge;
double totalPrice;
bool isComplete;
string firstName;
snake_case
int student_age;
double total_price;
bool is_complete;
string first_name;
Avoid These Styles
// Hungarian notation (outdated)
int iAge;
double dPrice;
// ALL_CAPS (use for constants only)
int STUDENT_AGE; // Don't do this for variables
Best Practices
Use Descriptive Names
// Bad - unclear
int x;
double p;
bool f;
// Good - descriptive
int studentCount;
double itemPrice;
bool isFileOpen;
Avoid Abbreviations
// Bad - confusing abbreviations
int stCnt;
double prc;
bool flg;
// Good - clear names
int studentCount;
double price;
bool isValid;
Constants Use ALL_CAPS
Constants are variables whose values cannot be changed after they're created. Use the const
keyword to make a variable constant:
const double PI = 3.14159; // Value of PI never changes
const int MAX_STUDENTS = 30; // Maximum class size
const string DEFAULT_NAME = "Unknown"; // Default value
// Once created, you cannot change them:
// PI = 3.14; // Error! Cannot modify a const variable
We'll learn more about constants and why they're useful in later lessons. For now, just remember that constants use ALL_CAPS naming convention.
Be Consistent
Pick one naming convention and stick with it throughout your program.
Common Mistakes
// Mistake 1: Starting with number
int 1stPlace; // Error!
int place1st; // Correct
// Mistake 2: Using keywords
int new; // Error!
int newValue; // Correct
// Mistake 3: Special characters
int my-var; // Error!
int my_var; // Correct
Summary
Keywords and naming identifiers are fundamental to writing C++ code. Key points:
Keywords:
- Reserved words with special meaning in C++ (like
int
,if
,class
) - Cannot be used as variable names or identifiers
- Examples:
int
,double
,bool
,if
,else
,return
,void
Identifier Rules:
- Must start with a letter or underscore
- Can only contain letters, numbers, and underscores
- Are case-sensitive (
age
,Age
, andAGE
are different) - Cannot be C++ keywords
Naming Conventions:
- camelCase:
studentAge
,totalPrice
(recommended for beginners) - snake_case:
student_age
,total_price
(alternative style) - ALL_CAPS: Reserved for constants only (
const int MAX_SIZE = 100
)
Best Practices:
• Avoid single letters and unclear abbreviations
• Be consistent with your chosen naming style
• Make your code readable for others and your future self
Good naming makes your code self-documenting and easier to understand and maintain.
C++ Naming Conventions - Quiz
Test your understanding of the lesson.
Lesson Discussion
Share your thoughts and questions