Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Back to C++ Programming Fundamentals
Lesson 10 of 15
Beginner
40 minutes
Keywords and naming identifiers
Learn C++ keywords and best practices for naming variables and identifiers.
Prerequisites
Uninitialized variables and undefined behavior
Not completed
Keywords and Naming Identifiers
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;
int myVariable;
// Invalid
int 2count; // Cannot start with number
int #value; // Cannot start with symbol
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
const double PI = 3.14159;
const int MAX_STUDENTS = 30;
const string DEFAULT_NAME = "Unknown";
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
Keywords and naming identifiers - Quiz
Test your understanding of the lesson.
10 questions
10 minutes
60% to pass
Lesson Discussion
Share your thoughts and questions