C++ Keywords
In C++, keywords are reserved words that have special meaning to the compiler. They are part of the language syntax and cannot be used as variable names, function names, or identifiers.
Example of Keywords
Some commonly used C++ keywords are:
- int – used for integer variables
- float – used for decimal numbers
- double – used for more precise decimal numbers
- char – used for single characters
- if – used for conditional statements
- else – used with if for alternative conditions
- for – used for loops
- while – used for loops
- return – used to return a value from a function
- const – used to declare constants
- void – indicates no value is returned from a function
- switch – used for multiple condition checks
- case – used inside switch statements
- break – exits loops or switch statements
- continue – skips the current iteration in loops
- class – used to define a class
- public, private, protected – access specifiers in classes
Example Program Using Keywords
#include <iostream>
using namespace std;
int main() {
const int age = 25; // const is a keyword
int number = 10; // int is a keyword
if(age > 18) { // if is a keyword
cout << "Adult";
} else { // else is a keyword
cout << "Minor";
}
return 0; // return is a keyword
}
Important Notes
- Keywords are case-sensitive. For example, int is valid, but Int is not.
- You cannot use keywords as variable names or identifiers.
- C++ has around 95 keywords depending on the compiler and C++ standard.
Next Topic
Next, learn about C++ Operators.