Keywords in C++

Keywords in C++ are reserved words that have a predefined meaning in the language. These words are used to perform specific tasks, so they cannot be used as variable names, function names, or class names.

For example, words like int, if, while, and return are C++ keywords because the compiler already knows their purpose.

Example

int age = 20;

if (age >= 18) {
    cout << "Eligible";
}

In this example:

  • int is a keyword used to declare an integer variable.
  • if is a keyword used for decision-making.

C++ Keywords List

Below are some of the most commonly used C++ keywords.

KeywordPurpose
intDeclares an integer variable
floatDeclares a floating-point variable
charDeclares a character variable
boolDeclares a boolean variable
ifExecutes code based on a condition
elseExecutes when the if condition is false
switchSelects one block from multiple options
caseDefines a case inside a switch statement
forCreates a loop
whileRepeats code while a condition is true
doExecutes a loop at least once
breakExits a loop or switch statement
continueSkips the current loop iteration
returnReturns a value from a function
voidIndicates no return value
classDefines a class
publicPublic access specifier
privatePrivate access specifier
protectedProtected access specifier
constDeclares a constant value

Note: Modern versions of C++ contain more than 90 reserved keywords. You don’t need to memorize all of them. As you learn C++, you’ll naturally become familiar with the most commonly used ones.

Can You Use Keywords as Variable Names?

No. Since keywords already have a predefined meaning, they cannot be used as identifiers.

Invalid Example

int return = 10;

The above code will generate a compilation error because return is a reserved keyword.

Valid Example

int marks = 90;

Here, marks is a valid identifier because it is not a keyword.

Key Points

  • Keywords are reserved words in C++.
  • Every keyword has a predefined meaning.
  • Keywords cannot be used as variable or function names.
  • Modern C++ provides more than 90 reserved keywords.
  • You only need to remember the commonly used keywords while learning.

Frequently Asked Questions

Q1. What are keywords in C++?

Keywords in C++ are reserved words with predefined meanings. They are part of the language syntax and cannot be used as identifiers such as variable or function names.

Q2. How many keywords are there in C++?

Modern C++ includes more than 90 keywords, with the exact number depending on the C++ standard version. Common examples include int, if, while, return, and class.

Q3. Can I use C++ keywords as variable names?

No. Since keywords are reserved by the compiler, they cannot be used as variable names, function names, or class names in a C++ program.

Q4. What is the difference between keywords and identifiers in C++?

Keywords have predefined meanings in the C++ language, whereas identifiers are user-defined names given to variables, functions, classes, and other program elements.

Q5. Why are C++ keywords important?

C++ keywords define the structure and behavior of a program. Understanding them helps you write correct syntax, avoid compilation errors, and build a strong foundation in C++ programming.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top