The Wayback Machine - https://web.archive.org/web/20240828110659/https://www.geeksforgeeks.org/cpp-keywords/
Open In App

C++ Keywords

Last Updated : 29 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

C++ is a powerful language. In C++, we can write structured programs and object-oriented programs also. C++ is a superset of C and therefore most constructs of C are legal in C++ with their meaning unchanged. However, there are some exceptions and additions.

Token

When the compiler is processing the source code of a C++ program, each group of characters separated by white space is called a token. Tokens are the smallest individual units in a program. A C++ program is written using tokens. It has the following tokens:

C++ Tokens

Keywords

Keywords(also known as reserved words)  have special meanings to the C++ compiler and are always written or typed in short(lower) cases. Keywords are words that the language uses for a special purpose, such as void, int, public, etc. It can’t be used for a variable name or function name or any other identifiers. The total count of reserved keywords is 95. Below is the table for some commonly used C++ keywords.

C++ Keyword

asm double new switch
auto else operator template
break enum private this
case extern protected throw
catch float public try
char for register typedef
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete int static volatile 
do long struct while 

Note: The keywords not found in ANSI C are shown here in boldface.

  • asm: To declare that a block of code is to be passed to the assembler.
  • auto: A storage class specifier that is used to define objects in a block.
  • break: Terminates a switch statement or a loop.
  • case: Used specifically within a switch statement to specify a match for the statement’s expression.
  • catch: Specifies actions taken when an exception occurs.
  • char: Fundamental data type that defines character objects.
  • class: To declare a user-defined type that encapsulates data members and operations or member functions.
  • const: To define objects whose value will not alter throughout the lifetime of program execution.
  • continue:- Transfers control to the start of a loop.
  • default:- Handles expression values in a switch statement that are not handled by case.
  • delete: Memory deallocation operator.
  • do: indicate the start of a do-while statement in which the sub-statement is executed repeatedly until the value of the expression is logical-false.
  • double:  Fundamental data type used to define a floating-point number.
  • else: Used specifically in an if-else statement.
  • enum: To declare a user-defined enumeration data type.
  • extern: An identifier specified as an extern has an external linkage to the block.
  • float:- Fundamental data type used to define a floating-point number.
  • for: Indicates the start of a statement to achieve repetitive control.
  • friend: A class or operation whose implementation can access the private data members of a class.
  • goto: Transfer control to a specified label.
  • if: Indicate the start of an if statement to achieve selective control.
  • inline: A function specifier that indicates to the compiler that inline substitution of the function body is to be preferred to the usual function call implementation.
  • int: Fundamental data type used to define integer objects.
  • long: A data type modifier that defines a 32-bit int or an extended double.
  • new: Memory allocation operator.
  • operator: Overloads a c++ operator with a new declaration.
  • private: Declares class members which are not visible outside the class.
  • protected: Declares class members which are private except to derived classes
  • public: Declares class members who are visible outside the class.
  • register: A storage class specifier that is an auto specifier, but which also indicates to the compiler that an object will be frequently used and should therefore be kept in a register.
  • return: Returns an object to a function’s caller.
  • short: A data type modifier that defines a 16-bit int number.
  • signed: A data type modifier that indicates an object’s sign is to be stored in the high-order bit.
  • sizeof: Returns the size of an object in bytes.
  • static: The lifetime of an object-defined static exists throughout the lifetime of program execution.
  • struct: To declare new types that encapsulate both data and member functions.
  • switch: This keyword is used in the “Switch statement”.
  • template: parameterized or generic type.
  • this:  A class pointer points to an object or instance of the class.
  • throw: Generate an exception.
  • try: Indicates the start of a block of exception handlers.
  • typedef: Synonym for another integral or user-defined type.
  • union: Similar to a structure, struct, in that it can hold different types of data, but a union can hold only one of its members at a given time.
  • unsigned: A data type modifier that indicates the high-order bit is to be used for an object.
  • virtual: A function specifier that declares a member function of a class that will be redefined by a derived class.
  • void: Absent of a type or function parameter list.
  • volatile: Define an object which may vary in value in a way that is undetectable to the compiler.
  • while: Start of a while statement and end of a do-while statement.

What is the identifier?

Identifiers refer to the name of variables, functions, arrays, classes, etc. created by the programmer. They are the fundamental requirement of any language.

Rules for naming identifiers:

  • Identifier names can not start with a digit or any special character.
  • A keyword cannot be used as an identifier name.
  • Only alphabetic characters, digits, and underscores are permitted.
  • The upper case and lower case letters are distinct. i.e., A and a are different in C++.
  • The valid identifiers are GFG, gfg, and geeks_for_geeks.

Examples of good and bad identifiers

Invalid Identifier  Bad Identifier  Good Identifier
Cash prize C_prize cashprize
catch catch_1 catch1
1list list_1 list1

Example:

C++




// C++ program to illustrate the use
// of identifiers
 
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Use of Underscore (_) symbol
    // in variable declaration
    int geeks_for_geeks = 1;
 
    cout << "Identifier result is: "
         << geeks_for_geeks;
 
    return 0;
}


Output

Identifier result is: 1

How keywords are different from identifiers?

So there are some main properties of keywords that distinguish keywords from identifiers:

Keywords  Identifiers
Keywords are predefined/reserved words  identifiers are the values used to define different programming items like a variable, integers, structures, and unions.
Keywords always start in lowercase identifiers can start with an uppercase letter as well as a lowercase letter.
It defines the type of entity. It classifies the name of the entity.
A keyword contains only alphabetical characters,  an identifier can consist of alphabetical characters, digits, and underscores.
It should be lowercase. It can be both upper and lowercase.
No special symbols or punctuations are used in keywords and identifiers.  No special symbols or punctuations are used in keywords and identifiers.  The only underscore can be used in an identifier.
Keywords: int, char, while, do. Identifiers: Geeks_for_Geeks, GFG, Gfg1.

Example: Below is the program for how to use different keywords in the program:

C++




// C++ Program to demonstrate keywords
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Variable declaration and
    // initialization
    int n = 2;
 
    // Switch Case Statement
    switch (n) {
    case 1:
        cout << "Computer Network"
             << endl;
        break;
    case 2:
        cout << "C++" << endl;
        break;
    case 3:
        cout << "DBMS" << endl;
        break;
    case 4:
        cout << "Data Structure"
             << endl;
        break;
    case 5:
        cout << "Operating System"
             << endl;
        break;
    default:
        cout << "Enter Valid number"
             << endl;
    }
 
    // Return keyword returns an object
    // to a function's caller
    return 0;
}


Output

C++



Previous Article
Next Article

Similar Reads

Where and Why Do I Have to Put the 'template' and 'typename' Keywords?
In C++, we have a template keyword to define template classes or functions and allow them to operate with generic types. On the other hand, we have typename keyword which is used to specify that the identifier that follows is a type, particularly in template code to clarify the ambiguity but confusion arises as to where exactly we need to put these
3 min read
C++ Programming Language
C++ is the most used and most popular programming language developed by Bjarne Stroustrup. C++ is a high-level and object-oriented programming language. This language allows developers to write clean and efficient code for large applications and software development, game development, and operating system programming. It is an expansion of the C pr
9 min read
7 ways to Initialize Vector in C++
Vectors in C++, like Arrays, are one of the most extensively used entities and to initialize vectors in C++ is one of the most common issues that users face. One of the most commonly used methods for vector initialization is the Array style. But C++, along with this, provides several different methods to initialize a vector. In this post, we have l
6 min read
30 OOPs Interview Questions and Answers (2024)
Object-Oriented Programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is widely used in many popular languages like Java,
15+ min read
Top 50 C++ Project Ideas For Beginners & Advanced
C++ is one of the most popular programming languages widely used in the software industry for projects in different domains like games, operating systems, web browsers, DBMS, etc due to its fast speed, versatility, lower-level memory access, and many more. Many top companies like Microsoft, Google, Apple, Netflix, etc use C++ to develop their produ
15+ min read
C++ Programming Examples
Writing C++ programs yourself is the best way to learn the C++ language. C++ programs are also asked in the interviews. This article covers the top practice problems for basic C++ programs on topics like control flow, patterns, and functions to complex ones like pointers, arrays, and strings. C++ Tutorial C++ Recent Articles Topics: Basic Programs
9 min read
C++ Interview Questions and Answers (2024)
C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent in C++. It is utilized by top IT companies such as
15+ min read
Left Shift and Right Shift Operators in C/C++
In C/C++, left shift (&lt;&lt;) and right shift (&gt;&gt;) operators are binary bitwise operators that are used to shift the bits either left or right of the first operand by the number of positions specified by the second operand allowing efficient data manipulation. In this article, we will learn about the left shift and right shift operators. Le
6 min read
vector erase() and clear() in C++
Prerequisite: Vector in C++ Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::clear() The clear() function is used to remove all the elements of the vector container, thus making it size 0. Syntax
6 min read
Substring in C++
The substring function is used for handling string operations like strcat(), append(), etc. It generates a new string with its value initialized to a copy of a sub-string of this object. In C++, the header file which is required for std::substr(), string functions is &lt;string&gt;. The substring function takes two values pos and len as an argument
8 min read
Virtual Function in C++
A virtual function (also known as virtual methods) is a member function that is declared within a base class and is re-defined (overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the meth
6 min read
Inheritance in C++
The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the information about how it affects different properties of the
15+ min read
C++ Polymorphism
The word "polymorphism" means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. A real-life example of polymorphism is a person who at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. So the same person ex
7 min read
C++ Classes and Objects
In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. In this article, we will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program. What is a Class in C++?A class is a user-defined data type, which holds its own data members and member functions, w
9 min read
C++ Data Types
All variables use data type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data they can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared. Every data type
10 min read
Convert String to int in C++
Converting a string to int is one of the most frequently encountered tasks in C++. As both string and int are not in the same object hierarchy, we cannot perform implicit or explicit type casting as we can do in case of double to int or float to int conversion. Conversion is mostly done so that we can convert numbers that are stored as strings. Exa
8 min read
std::sort() in C++ STL
We have discussed qsort() in C. C++ STL provides a similar function sort that sorts a vector or array (items with random access) It generally takes two parameters, the first one being the point of the array/vector from where the sorting needs to begin and the second parameter being the length up to which we want the array/vector to get sorted. The
6 min read
unordered_map in C++ STL
unordered_map is an associated container that stores elements formed by the combination of a key value and a mapped value. The key value is used to uniquely identify the element and the mapped value is the content associated with the key. Both key and value can be of any type predefined or user-defined. In simple terms, an unordered_map is like a d
7 min read
Priority Queue in C++ Standard Template Library (STL)
A C++ priority queue is a type of container adapter, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue, and elements are in non-increasing or non-decreasing order (hence we can see that each element of the queue has a priority {fixed order}). In C++ STL, the top elemen
11 min read
Stack in C++ STL
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. Stack uses an encapsulated object of either vector or deque (by default) or list (sequential container class) as its underlying container, providing a specific set of membe
5 min read
Vector in C++ STL
Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end. Inse
10 min read
Set in C++ Standard Template Library (STL)
Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific sorted order i.e. either ascending or descending. The std::set class is the part of C++ Standard Template Library (STL) and it is defined inside the &lt;set&gt; header file. Syntax: std:
7 min read
Map in C++ Standard Template Library (STL)
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values. std::map is the class template for map containers and it is defined inside the &lt;map&gt; header file. Basic std::map Member FunctionsSome basic functions associated with std::
8 min read
The C++ Standard Template Library (STL)
The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized. Working knowledge of template classes is a prerequ
5 min read
Object Oriented Programming in C++
Object-oriented programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data
9 min read
Operator Overloading in C++
in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning. In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot overload in C++. C++ Operator OverloadingC++ has
8 min read
Templates in C++ with Examples
A template is a simple yet very powerful tool in C++. The simple idea is to pass the data type as a parameter so that we don't need to write the same code for different data types. For example, a software company may need to sort() for different data types. Rather than writing and maintaining multiple codes, we can write one sort() and pass the dat
10 min read
Friend Class and Function in C++
A friend class can access private and protected members of other classes in which it is declared as a friend. It is sometimes useful to allow a particular class to access private and protected members of other classes. For example, a LinkedList class may be allowed to access private members of Node. We can declare a friend class in C++ by using the
6 min read
Constructors in C++
Constructor in C++ is a special method that is invoked automatically at the time an object of a class is created. It is used to initialize the data members of new objects generally. The constructor in C++ has the same name as the class or structure. It constructs the values i.e. provides data for the object which is why it is known as a constructor
7 min read
Bitwise Operators in C
In C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C. The &amp; (bitwise AND) in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.  The | (bitwise OR) in C takes two n
7 min read
Article Tags :
Practice Tags :