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"
if(a)
       cout << "a is equal to 1 or true\n";

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

return 0;
}

Output:

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]<<" ";
}
cout<<"\n";
i=0;
while(i<10){              // WHILE loop
    cout<<a[i]<<" ";
    i++;
}
return 0;
}

Output:

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]<<" ";
    i++;
}
cout<<"\n";

do{     // get executed once irrespective what while condition is
    cout<<a[i]<<" ";
    i++;
}while(i<0);

return 0;
}

Output:

1

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;
    cin>>n;

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

    switch(div)
    {
 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;
    break;
default:
        cout<<"default block"<<endl;
        break;
    }

    return 0;
}

Output:

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

int main()
{
   int n=10;

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

    if(i<5){
          
           cout<<i<<" ";

           break;   // replace with "continue"

    }  

   }
   cout<<"\n";
   cout<<"outside for loop \n";

  return 0;
}

Output with 'break'

0
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  ";
jump:
  cout<<"5  ";

return 0;
}

Output:

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;
}

Output:

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;
}

Output:

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{
   private:
         int a,b;
         char ch;
   public:
         void get();
         int sets();
   protected:
        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.

typeid(obj);

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 
{ 
public: 
    virtual void print() { 
        cout<<" I am Base class \n";
         } 
}; 
   
class Derived_class: public Base_class
{ 
public: 
    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.

Reference: https://en.cppreference.com/w/cpp/keyword

10 Comments

  1. I blog frequently and I seriously thank you
    for your information. This article has really peaked my interest.
    I am going to book mark your blog and keep checking
    for new details about once a week. I subscribed to your Feed too.

  2. Everything is very open with a really clear description of the challenges.

    It was really informative. Your site is useful. Thanks for sharing!

  3. Awesome! Its really amazing piece of writing, I have got much clear idea regarding from this article.

  4. Good ? I should certainly pronounce, impressed with your
    site. I had no trouble navigating through all tabs and related
    info ended up being truly simple to do to access.

    I recently found what I hoped for before you know it
    in the least. Quite unusual. Is likely to appreciate it for those who add forums or something, website theme .
    a tones way for your client to communicate. Nice task.

  5. Its like you read my mind! You appear to know so much about
    this, like you wrote the book in it or something. I think that
    you could do with some pics to drive the message home
    a little bit, but instead of that, this is excellent blog.
    A great read. I’ll certainly be back.

  6. I have been surfing online more than 3 hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. In my opinion, if all web
    owners and bloggers made good content as you did, the internet will be a
    lot more useful than ever before.|
    I could not refrain from commenting. Very well written!|
    I will immediately grasp your rss feed as I can not
    to find your email subscription link or newsletter service.

    Do you have any? Kindly permit me recognize so that I may just subscribe.
    Thanks.|
    It’s perfect time to make some plans for the future and it’s time to be happy.
    I’ve read this post and if I could I want
    to suggest you few interesting things or advice. Perhaps you could
    write next articles referring to this article. I wish to read even more things about it!|
    It’s the best time to make a few plans for the long
    run and it’s time to be happy. I have read this publish and if I
    may I want to recommend you some attention-grabbing things or advice.
    Perhaps you could write subsequent articles referring to this article.
    I desire to learn more things approximately it!|
    I’ve been browsing online more than 3 hours nowadays,
    yet I by no means found any interesting article
    like yours. It’s lovely value sufficient for me. In my opinion, if
    all web owners and bloggers made just right content as you
    probably did, the net will likely be much more helpful than ever before.|
    Ahaa, its fastidious dialogue concerning this piece of writing here at this webpage, I have read all that, so at
    this time me also commenting here.|
    I am sure this piece of writing has touched all the internet visitors, its really really fastidious post on building up new web
    site.|
    Wow, this paragraph is good, my younger sister is analyzing these
    kinds of things, so I am going to let know her.|
    bookmarked!!, I like your site!|
    Way cool! Some very valid points! I appreciate
    you penning this article plus the rest of the website is also really good.|
    Hi, I do think this is an excellent site.

    I stumbledupon it 😉 I may revisit once again since
    i have book-marked it. Money and freedom is the best way to change, may you be rich
    and continue to guide other people.|
    Woah! I’m really loving the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness and visual appearance.
    I must say you have done a fantastic job with this.
    In addition, the blog loads very fast for
    me on Chrome. Excellent Blog!|
    These are actually wonderful ideas in concerning blogging.

    You have touched some good factors here. Any way keep up
    wrinting.|
    I really like what you guys tend to be up too. Such clever work and reporting!
    Keep up the fantastic works guys I’ve added you guys to my personal blogroll.|
    Howdy! Someone in my Myspace group shared this website with
    us so I came to give it a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my
    followers! Outstanding blog and great style and design.|
    I really like what you guys are usually up too. This sort of clever work
    and coverage! Keep up the fantastic works guys I’ve incorporated you guys to my blogroll.|
    Hey there would you mind sharing which blog platform you’re using?
    I’m going to start my own blog soon but I’m having a tough time
    making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique.
    P.S Sorry for being off-topic but I had to ask!|
    Hey there would you mind letting me know which webhost you’re working
    with? I’ve loaded your blog in 3 different web browsers and I must say this
    blog loads a lot faster then most. Can you recommend a good internet hosting provider at a
    fair price? Many thanks, I appreciate it!|
    I like it when individuals come together and
    share views. Great blog, keep it up!|
    Thank you for the auspicious writeup. It in fact was a amusement account it.
    Look advanced to more added agreeable from you! By the way, how could we communicate?|
    Hello just wanted to give you a quick heads up. The words in your article seem
    to be running off the screen in Internet explorer.
    I’m not sure if this is a format issue or something to do with web browser
    compatibility but I thought I’d post to let you know. The design and style look great though!
    Hope you get the issue solved soon. Many thanks|
    This is a topic that’s near to my heart… Best wishes!
    Where are your contact details though?|
    It’s very effortless to find out any matter on web as compared to
    textbooks, as I found this paragraph at this site.|
    Does your site have a contact page? I’m having problems locating it but, I’d like to shoot
    you an e-mail. I’ve got some creative ideas for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it expand over time.|
    Hello! I’ve been reading your site for a long time now and finally got the courage to go ahead and
    give you a shout out from New Caney Tx! Just wanted to say keep
    up the excellent work!|
    Greetings from California! I’m bored to death at work so I decided to check out your site on my iphone during
    lunch break. I enjoy the knowledge you provide here and can’t
    wait to take a look when I get home. I’m shocked at how fast your blog loaded
    on my mobile .. I’m not even using WIFI, just 3G ..
    Anyways, awesome site!|
    Its like you learn my mind! You appear to understand a lot
    about this, such as you wrote the ebook in it or something.
    I believe that you could do with some % to drive the message home a
    little bit, however instead of that, that is great blog. An excellent read.
    I’ll certainly be back.|
    I visited various websites except the audio feature for audio songs present at this website is really
    fabulous.|
    Hi, i read your blog from time to time and i own a
    similar one and i was just wondering if you get a lot of spam comments?
    If so how do you protect against it, any plugin or anything you can suggest?

    I get so much lately it’s driving me crazy so any support is very much appreciated.|
    Greetings! Very useful advice within this post!

    It is the little changes which will make the greatest changes.
    Thanks a lot for sharing!|
    I really love your site.. Great colors & theme. Did you
    create this web site yourself? Please reply back as I’m wanting to create my very own website and want to
    learn where you got this from or what the theme is called.

    Appreciate it!|
    Howdy! This blog post couldn’t be written any
    better! Looking through this post reminds me of my previous roommate!
    He continually kept talking about this. I will forward
    this information to him. Fairly certain he will have a good read.
    I appreciate you for sharing!|
    Amazing! This blog looks just like my old one! It’s on a totally different
    topic but it has pretty much the same layout and design. Great choice of
    colors!|
    There’s definately a lot to know about this topic. I love all the points you’ve made.|
    You made some really good points there. I checked on the internet
    to learn more about the issue and found most individuals will go along with your views on this website.|
    What’s up, I check your new stuff like every week.
    Your story-telling style is witty, keep doing what
    you’re doing!|
    I just could not go away your website before suggesting
    that I extremely enjoyed the usual information an individual supply to your guests?
    Is going to be again continuously in order to investigate cross-check new
    posts|
    I need to to thank you for this fantastic read!!
    I certainly enjoyed every little bit of it. I have got you
    saved as a favorite to check out new things you post…|
    Hello, just wanted to tell you, I enjoyed this blog post.
    It was practical. Keep on posting!|
    Hello, I enjoy reading all of your post. I like to write a little comment to
    support you.|
    I always spent my half an hour to read this website’s posts daily along with a cup of
    coffee.|
    I constantly emailed this website post page to all my contacts, since if like to read it afterward my contacts will too.|
    My coder is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using Movable-type on several websites for about a year and
    am worried about switching to another platform.
    I have heard great things about blogengine.net.
    Is there a way I can transfer all my wordpress content
    into it? Any kind of help would be greatly appreciated!|
    Good day! I could have sworn I’ve visited this site before
    but after looking at many of the articles I realized it’s new
    to me. Regardless, I’m certainly pleased I stumbled upon it and I’ll be bookmarking it and checking back regularly!|
    Great article! That is the kind of information that should be shared around the
    internet. Shame on the search engines for no longer positioning this submit upper!

    Come on over and discuss with my site . Thank you =)|
    Heya i am for the first time here. I found this board and I find It really useful
    & it helped me out much. I hope to give something
    back and help others like you aided me.|
    Hi, I believe your web site could possibly be having internet browser compatibility issues.
    When I look at your website in Safari, it looks fine but when opening in I.E., it has some overlapping issues.
    I just wanted to give you a quick heads up! Other than that, fantastic blog!|
    Somebody essentially assist to make seriously posts I
    might state. This is the first time I frequented your website page and so far?
    I surprised with the research you made to create this particular publish incredible.
    Wonderful activity!|
    Heya i’m for the first time here. I came across this board and I in finding It truly useful & it
    helped me out much. I am hoping to give one thing again and help others like you aided me.|
    Hey there! I simply want to give you a huge thumbs up for your great information you
    have got right here on this post. I’ll be coming back to your
    blog for more soon.|
    I always used to study post in news papers but now
    as I am a user of internet therefore from now I am using net for articles or reviews, thanks to web.|
    Your way of telling all in this post is in fact fastidious,
    every one be able to simply know it, Thanks a lot.|
    Hi there, I discovered your website by the use of Google at the same
    time as looking for a related subject, your site got here up,
    it looks good. I have bookmarked it in my google bookmarks.

    Hi there, just turned into aware of your weblog through Google, and located that it’s really informative.
    I am going to watch out for brussels. I’ll be grateful if you
    proceed this in future. Many other folks might be
    benefited out of your writing. Cheers!|
    I’m curious to find out what blog platform you happen to be working with?
    I’m experiencing some small security problems with my latest website and I
    would like to find something more safe. Do you have any solutions?|
    I am extremely impressed with your writing skills and also with the layout on your blog.
    Is this a paid theme or did you modify it yourself?
    Either way keep up the nice quality writing, it’s rare to
    see a great blog like this one today.|
    I am extremely impressed together with your writing talents as smartly as with the structure on your weblog.
    Is this a paid theme or did you customize it your self?
    Anyway stay up the excellent quality writing, it
    is rare to look a great blog like this one these days..|
    Hi, Neat post. There’s an issue together
    with your website in web explorer, might test this? IE still is the market chief and a huge part of folks will omit
    your excellent writing because of this problem.|
    I’m not sure where you are getting your info, but great topic.
    I needs to spend some time learning more or understanding more.
    Thanks for wonderful info I was looking for this information for my mission.|
    Hello, i think that i saw you visited my web site thus i came to “return the favor”.I
    am trying to find things to enhance my site!I
    suppose its ok to use some of

    1. Thank you for your valuable feedback.
      Web host I use Namecheap and for theme learn some HTML, CSS and Javascript a little bit of three will help you in customizing theme for your need

  7. Hello There. I discovered your weblog the use of msn. That is an extremely well
    written article. I’ll make sure to bookmark it and come back
    to learn extra of your useful info. Thank you for the post.
    I’ll certainly return.

Leave a Reply

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

%d bloggers like this: