Keywords in C++ Language

Keywords are those who has a fixed meaning already defined by Compiler and used to perform certain internal operation. There are 84 keywords in C++ Language including 32 keywords of C language. These reserved words not available for re-definition or overloading and cannot be used as an identifier.

C++ Keywords:

//C++98 keywords
and            continue        goto          public             try
and_eq         default         if            register           typedef
asm            delete          inline        reinterpret_cast   typeid
auto           do              int           return             typename
bitand         double          long          short              union
bitor          dynamic_cast    mutable       signed             unsigned
bool           else            namespace     sizeof             using
break          enum            new           static             virtual
case           explicit        not           static_cast        void
catch          export          not_eq        struct             volatile
char           extern          operator      switch             wchar_t
class          false           or            template           while
compl          float           or_eq         this               xor
const          for             private       throw              xor_eq
const_cast     friend          protected     true

//C++11 added keywords

alignas        constexpr       static_assert
alignof        decltype        thread_local  
char16_t       noexcept         
char32_t       nullptr          

C++ also have two words final & override which act like keywords and can also used as identifiers.

Note-1: This guide only contains a brief explanation of all keywords. And complex keywords will be explained in later guide for better understanding.

Note-2: All keywords are grouped and explain in relative manners not in alphabetical order

Keywords Brief Explanation with examples

int: Declare an integer variable.

char: Declare a character variable which can take values up to 256.

float: Declare a floating-point variable.

double: Declare a double precision floating-point variable.

long, short, signed, unsigned: used as prefix for above data type to defines their size and ability to hold data.

wchar_t: wide characters can take on 65536 values which almost similar to UNICODE values.

void: void means no value or without value and is not a data type. Void mostly used as return type function which does not return any value.

return: used to return control back to calling function.

sizeof: used to return the size of a data type or variable.

sizeof(int)  // return  4 

auto: Used to declare a local variable. And the use of auto keyword is optional as all variable declared locally are auto by default.

auto int a=1; 
int b=2;
 //Both are same   

true & false: constants representing boolean true and false value

bool: This one is a data type used to declare a variable which can only hold two boolean values (1/0) aka (true or false).

int main()
bool a = 1;    // you can replace true with 0 or 1 or false

                      //true is equal to "1"
                     //false equals to "0"
       cout << "a is equal to 1 or true\n";

       cout << "a is equal to 0 or false\n";

return 0;


a is equal to 0 or false

If: used to test the expression.

else: follow if block with alternate execution to if statement (look above example for if-else explanation).

for: for keyword is used as special case for a pre-conditional loop to iterate for certain number of times over expression depending upon initializer and step-expression.

while: while keyword also used for loop except it depends on conditional expression.

int main()
int a[10]={1,2,3,4,5,6,7,8,9,10};
int i=0;

for(i=0;i<10;i++){           // FOR loop
    cout<<a[i]<<" ";
while(i<10){              // WHILE loop
    cout<<a[i]<<" ";
return 0;


1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10

do: do used as support to while loop to execute loop once irrespective of while condition.

int main()
int a[10]={1,2,3,4,5,6,7,8,9,10};
int i=0;

while(i<0){    //This loop doesn't get executed bcoz i==0
    cout<<a[i]<<" ";

do{     // get executed once irrespective what while condition is
    cout<<a[i]<<" ";

return 0;



switch: switch statements are alternate of long if-else statements that compare a variable to several integral case values.

case: a block of code in a switch statement.

default: default case in switch statement.

break: used to break out of a loop.

continue: To bypass and continue to iterate over the loop.

//example for “switch”, ”case”, ”default”, ”break”, “continue”

int main()
    int n, div = 0;

    cout<<"Enter the value of n:"<<endl;

    div = n % 2;   //Replace n%2 with any number except 0 and 1 
                   //to see default block effect

 case 0:
    cout<<"n is EVEN number"<<endl;
    break;     //replace break with ‘continue’ to see its result
case 1:
    cout<<"n is ODD number"<<endl;
        cout<<"default block"<<endl;

    return 0;


Enter the value of n:
n is EVEN number
// break, continue example 

int main()
   int n=10;

   for(int i=0;i<n;i++ ){

           cout<<i<<" ";

           break;   // replace with "continue"


   cout<<"outside for loop \n";

  return 0;

Output with 'break'

outside for loop

Output with 'Continue'

0 1 2 3 4
outside for loop

goto: Keyword used to jump to anywhere in program with label also referred as unconditional jump statement.

int main()
  cout<<"1  ";
goto jump;
  cout<<"2  ";
  cout<<"3  ";
  cout<<"4  ";
  cout<<"5  ";

return 0;


1 5

struct: used to create user defined data type that can group items of different types into a single name or type.

struct person
char name[20];
int day;
char month[10];
int year;
float salary;

enum: This one also a user defined data type used to assign names to integral constants.

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
   int i;
   for (i=Mon; i<=Sun; i++)
      cout< <i<<" ";

   return 0;


0 1 2 3 4 5

try: Keyword try used for a block of code where an exception /error may occur which will activate certain exception block based on message thrown by it.

throw: throws an exception when error shows up in try block.

catch: catch block defines how to handle the error caught, when an exception occurs.

//Example of ‘try’ ‘throw’ ‘catch’ 

int main()
    int a=5;
   int x = -1;  // Change 'x' to positive integer value

   try {
      cout << "Dividing a and x \n";
      if (x < 0)
         throw x;
          cout << "This line will not get executed \n";
      cout << a/x<<endl;
   catch (int x ) {
      cout << "x cannot be -ve \n";

   cout << "Program ends \n";
   return 0;


Dividing a and x
x cannot be -ve
Program ends

class: Keyword class used for user defined data type, which holds its own data members and functions, accessed and used by creating an instance of that class.

Public: used to declare data members and member functions as public inside class which can be accessed outside the class using the direct member access operator.

Private: used to declare data members and member functions as private inside class which cannot be accessed outside the class directly and only the member functions or the friend functions of that class are allowed to access the private members.

Protected: This keyword almost similar to private except these data members participate in inheritance.

friend: used to grant non-member function access to private data.

class Class_name{
         int a,b;
         char ch;
         void get();
         int sets();
        int x,y;

friend int I_am_friend();

new: used dynamic memory allocation to a pointer variable.

delete: used to free-up or clear memory dynamically allocated.

int *p = new int; // allocating only memory
int *q = new int(25); // allocating only memory as well as data

delete p; // freeing memory
delete q;

const: used to define variable constant which cannot be change once declared and defined.

mutable: used to change data members of a constant object.

dynamic_cast: used for type casting i.e for converting base-class pointers into derived-class pointers during runtime.

static_cast: used for implicit type conversation.

float b = 6.6
int a = static_cast<int>(b); 

//Now a = 6

const_cast: used to remove the constant-ness from references and pointers that refer to something that is not const.

int a = 0;   // if ‘a’ is already constant then it’s a undefined behavior
const int* ptr = &i;

*const_cast<int*>(ptr) = 1;

reinterpret_cast: used to simply cast one type bitwise to another.

int* p = new int(97); 
char* ch = reinterpret_cast<char*>(p); 
cout << *p <<” ”; 
cout << *ch << endl; 

//Output: 97  a

explicit: used to prevents the compiler from using constructor for implicit conversions.

export: Used to identify template definition as exported, which allows the same template to be declared, but not defined.

extern: used to declare a variable or function defined somewhere else and specifies that it has external linkage.

inline: keyword inline used to request the compiler that a particular function to insert the complete body of the function in every context for optimized speed.

operator: used to overload operators like +, - etc.

register: requesting compiler to store variable in register for fast access.

static: used to create permanent storage for a variable.

template: used to create generic functions which can be called with any data type.

this: this refers to the current instance/object of the class.

typedef: used to define alias name to data types.

typedef int integer;
integer a=10; // similar to int a =10 

typeid: used to determine the class of an object at runtime.


typename: used as an alternative to class to declare type template parameters or a dependent name is a type.

template <typename T> 

namespace: partition the global namespace by defining a scope.

using: import name from another namespace into the current scope.

using namespace std;

virtual: keyword virtual used as prefix to functions that can be overridden in a derived class.

class Base_class 
    virtual void print() { 
        cout<<" I am Base class \n";
class Derived_class: public Base_class
    void print() { 
        cout<<"I am derived class \n";

volatile: used to prevent compiler from optimizing the object or variables that can be modified unexpectedly.

alignas: used to specifies the storage for the variable that should be aligned by specific given amount.

alignof: return alignment requirements of a type.

char16_t& char32_t: declare char of 16 bit and 32 bit.

constexpr: specifies that the value of an variable or function can be evaluated at compile time and can be used in other constant expressions.

decltype: extract the type from the variable.

noexcept: used to performs a compile-time check and specify whether a function will throw exceptions.

nullptr: true pointer type assigned to empty declared pointers.

static_assert: used to Performs compile-time assertion checking.

thread_local: variable declared as thread_local then each thread has its own copy.


Leave a Reply

Your email address will not be published. Required fields are marked *