codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

6 / 87 topics
6Keywords and Identifiers7Variables, Literals, Constants & Storage Classes8Data Types & Type Modifiers9Type Conversion & Casting Operators10Operators11Booleans12Math & Numbers13Date and Time
Tutorials/C++ Programming/Keywords and Identifiers
⚡C++ Programming

Keywords and Identifiers

Updated 2026-05-12
30 min read

Keywords and Identifiers

In the realm of programming, keywords and identifiers are fundamental building blocks that help structure your code. Keywords are reserved words with specific meanings in C++, while identifiers are names used to identify variables, functions, classes, etc. Understanding these concepts is crucial for writing clear, error-free, and maintainable C++ programs.

Introduction

Keywords are predefined words in the C++ language that have special meaning and cannot be used as identifiers (names for variables, functions, etc.). Identifiers, on the other hand, are user-defined names that help you identify different parts of your program. Knowing the rules for using keywords and how to name identifiers properly will make your code more readable and less prone to errors.

In this tutorial, we'll explore:

  1. The list of reserved keywords in C++.
  2. The rules for naming identifiers.
  3. Examples of valid and invalid identifiers.
  4. Best practices for naming conventions.

Reserved Keywords

C++ reserves a set of keywords that have specific meanings within the language. These keywords cannot be used as identifiers (variable names, function names, etc.). Here is a list of some common C++ keywords:

KeywordDescription
autoUsed for automatic type deduction.
breakExits a loop or switch statement.
caseUsed in switch statements to specify cases.
charRepresents character data.
classDefines a class.
constDeclares constants.
continueSkips the current iteration of a loop and continues with the next one.
defaultUsed in switch statements to specify a default case.
deleteDeletes an object or array.
doStarts a do-while loop.
doubleRepresents double-precision floating-point numbers.
elseUsed with if statements to handle alternative cases.
enumDefines enumerations.
explicitPrevents implicit type conversions.
exportUsed in template definitions (deprecated).
externDeclares a variable or function that is defined elsewhere.
falseRepresents the boolean value false.
floatRepresents single-precision floating-point numbers.
forStarts a for loop.
friendGrants special access rights to classes and functions.
gotoJumps to a labeled statement (generally discouraged).
ifStarts an if statement.
inlineSuggests that the function should be inlined by the compiler.
intRepresents integer data.
longRepresents long integers.
mutableAllows modification of a member variable even if it's declared const.
namespaceDefines a namespace for organizing code.
newAllocates memory for an object or array.
nullptrRepresents a null pointer (C++11 and later).
operatorOverloads operators.
privateRestricts access to class members.
protectedAllows access within the class and derived classes.
publicAllows access from anywhere.
registerSuggests that a variable should be stored in a register (deprecated).
returnReturns a value from a function.
shortRepresents short integers.
signedIndicates signed integer data.
sizeofReturns the size of a type or object.
staticDeclares static variables, functions, and members.
structDefines a structure.
switchStarts a switch statement.
templateUsed for template metaprogramming.
thisPoints to the current object.
throwThrows an exception.
trueRepresents the boolean value true.
tryStarts a try block.
typedefCreates type definitions.
typeidReturns the runtime type information of an object.
typenameUsed in template declarations.
unionDefines a union.
unsignedIndicates unsigned integer data.
usingAlias for namespaces and templates (C++11).
virtualDeclares virtual functions.
voidRepresents no type or void return value.
volatileIndicates that a variable can be changed by external factors.
whileStarts a while loop.

Warning

Always avoid using these keywords as identifiers to prevent syntax errors and confusion.

Naming Rules for Identifiers

Identifiers in C++ must follow certain rules:

  1. Start with a Letter or Underscore: The first character of an identifier can be a letter (a-z, A-Z) or an underscore (_). It cannot start with a digit.

  2. Followed by Letters, Digits, or Underscores: Subsequent characters can include letters, digits (0-9), and underscores.

  3. Case Sensitivity: Identifiers are case-sensitive. myVariable and MyVariable are considered different identifiers.

  4. Reserved Keywords: Identifiers cannot be the same as any C++ reserved keyword.

  5. No Spaces or Special Characters: Identifiers cannot contain spaces or special characters like @, #, $, etc.

  6. Maximum Length: While there is no strict limit, identifiers should be reasonably short for readability and maintainability.

Here are some valid and invalid identifier examples:

Valid Identifiers

valid_identifiers.cpp
1#include <iostream>
2
3int main() {
4 int myVariable = 10;
5 double _pi = 3.14;
6 bool is_valid = true;
7 std::cout << "Valid identifiers work!" << std::endl;
8 return 0;
9}
Output
Valid identifiers work!

Invalid Identifiers

invalid_identifiers.cpp
1#include <iostream>
2
3int main() {
4 int 1variable = 10; // Invalid: starts with a digit
5 double my-variable = 3.14; // Invalid: contains a hyphen
6 bool @isValid = true; // Invalid: starts with an '@'
7 std::cout << "Invalid identifiers cause errors!" << std::endl;
8 return 0;
9}
Terminal
$ g++ invalid_identifiers.cpp -o invalid_identifiers
invalid_identifiers.cpp: In function ‘int main()’:
invalid_identifiers.cpp:4:7: error: expected identifier before numeric constant
int 1variable = 10; // Invalid: starts with a digit
^
invalid_identifiers.cpp:5:13: error: expected unqualified-id before '-' token
double my-variable = 3.14; // Invalid: contains a hyphen
^
invalid_identifiers.cpp:6:12: error: expected identifier before '@' token
bool @isValid = true; // Invalid: starts with an '@'
^

Best Practices for Naming Conventions

While C++ allows a wide range of identifiers, following certain naming conventions can make your code more readable and maintainable:

  1. Use Descriptive Names: Choose names that clearly describe the purpose or function of the identifier.

  2. Camel Case for Variables and Functions: Start with a lowercase letter and capitalize the first letter of each subsequent word.

    • Example: calculateTotalPrice
  3. Pascal Case for Classes and Structs: Capitalize the first letter of each word, including the first one.

    • Example: CustomerOrder
  4. Use Uppercase Letters for Macros and Constants: This distinguishes them from regular variables.

    • Example: MAX_VALUE
  5. Avoid Abbreviations: Use full words unless they are widely understood abbreviations (e.g., ID for Identifier).

  6. Consistent Naming Style: Stick to a consistent naming style throughout your codebase.

Here's an example demonstrating these conventions:

naming_conventions.cpp
1#include <iostream>
2
3class CustomerOrder {
4public:
5 void calculateTotalPrice() {
6 int itemCount = 5;
7 double itemPrice = 2.99;
8 double totalPrice = itemCount * itemPrice;
9 std::cout << "Total Price: $" << totalPrice << std::endl;
10 }
11};
12
13int main() {
14 CustomerOrder order;
15 order.calculateTotalPrice();
16 return 0;
17}
Output
Total Price: $14.95

Tip

Following these conventions will make your code more understandable and easier to maintain, especially in collaborative projects.

Practical Example

Let's create a simple program that calculates the area of a rectangle using descriptive variable names and follows naming conventions:

rectangle_area.cpp
1#include <iostream>
2
3class Rectangle {
4public:
5 void calculateArea() {
6 double length = 10.5;
7 double width = 7.2;
8 double area = length * width;
9 std::cout << "The area of the rectangle is: " << area << " square units." << std::endl;
10 }
11};
12
13int main() {
14 Rectangle rect;
15 rect.calculateArea();
16 return 0;
17}
Output
The area of the rectangle is: 75.6 square units.

Summary

  • Keywords are reserved words in C++ with specific meanings and cannot be used as identifiers.
  • Identifiers must start with a letter or underscore, followed by letters, digits, or underscores. They are case-sensitive and should not contain spaces or special characters.
  • Following best practices for naming conventions (descriptive names, camel case for variables/functions, pascal case for classes/structs, uppercase for macros/constants) enhances code readability and maintainability.

What's Next?

Now that you have a solid understanding of keywords and identifiers, the next step is to learn about Variables, Literals, Constants & Storage Classes. These concepts will help you declare and manipulate data in your C++ programs. Stay tuned!

Note

Continue practicing by writing small programs that use different identifiers and explore how they behave under various conditions.

PreviousBasic Input / OutputNext Variables, Literals, Constants & Storage Classes

Recommended Gear

Basic Input / OutputVariables, Literals, Constants & Storage Classes