Keywords And Identifiers In C++

Share Your Love

Before discussing keywords and identifiers, we should actually know what makes a C++ program. So, the basic building blocks of a program are called Tokens. 

Tokens:

Tokens are the smallest entity or individual units of the C++ language. A C++ program is made up of a combination of small tokens just like cells of our body or bricks of a building. Tokens are different by nature and purpose, that is why they are classified into 6 types:

  1. Keywords 
  2. Identifiers
  3. Constants
  4. Special symbols
  5. Operators
  6. Strings
Token
Tokens

Keywords:

The keyword is a reserved word whose meaning or definition is already known or predefined (as you can say) to the compiler. It is reserved because it needs to serve a specific purpose, it is made or developed to do that task only. Keywords have some rules which should be followed while using them. So, the rules to remember while inserting keywords in a program are:

  1. As it is reserved so, it’s meaning cannot be changed to use for other purposes which it is not meant to do.
  2. Keywords cannot be used for giving names to use defined things in the program like class, object etc.
  3. Keywords are always written in lower cases.

Here is a list of all C++ keywords. (as of C++17)

alignasdecltypenamespacestruct
alignofdefaultnewswitch
anddeletenoexcepttemplate
and_eqdonotthis
asmdoublenot_eqthread_local
autodynamic_castnullptrthrow
bitandelseoperatortrue
bitorenumortry
boolexplicitor_eqtypedef
breakexportprivatetypeid
caseexternprotectedtypename
catchfalsepublicunion
charfloatregisterunsigned
char16_tforreinterpret_castusing
char32_tfriendreturnvirtual
classgotoshortvoid
complifsignedvolatile
constinlinesizeofwchar_t
constexprintstaticwhile
const_castlongstatic_assertxor
continuemutablestatic_castxor_eq
C++ 17

Identifiers:

Identifiers are used to give names to user-defined (defined by users, whose meaning is not known to the compiler) things like class, object, variables, constants, array, functions. Identifiers also have rules to apply just like keywords have but the rules are different. 

So, rules to use identifiers in a program are:

  1. They consist of alphabets[(A-Z) and(a-z)], digits (0 to 9), _(underscore).
  2. Identifiers should not start with digits. For example: ‘5_web’ cannot be used as an identifier.
  3. Identifiers are case sensitive which means upper case letters are treated differently from lower case letters. For example, min and MIN are two different identifiers.
  4. No special symbols other than underscore can be used in identifiers and there should not be any blank spaces either.
  5. Keywords cannot be used as identifiers. For example, we can’t use while as an identifier like ‘int while’.

Constants:

The Constant refers to fixed values that cannot be changed during the execution of the program, unlike variables that can be changed.

Special Symbols:

These are the symbols that are used for specific purposes and cannot be used for other tasks. These symbols are brackets [], parentheses (), braces {}, comma (,), colon (:), asterisk (*), semi-colon (;), pre-processor (#) and assignment operator (=).

Spaces and Syntax:

These are blank spaces that are used in a program. 

Quick Recap: We discussed tokens, types of tokens, Keywords, rules for Keywords, identifiers. Rules for identifiers and some other tokens. We didn’t discuss briefly operators, special symbols, strings and constants as it’s not the topic of the article but we will know about them in another article briefly. Thank you.

Difference between Keyword and Identifier:

SR. NO.KEYWORDIDENTIFIER
1Keywords are reserved words and can’t be changed. In C/C++, keywords are special meaning and used in programming.Identifiers are used to define inside programming languages like a declaration of variables, data types and mostly alphabetic characters.
2Keywords are a specific type or kind of entity.An identifier is the identity of a particular entity.
3Keywords are always started with lower case letters.According to Identifier first character can be uppercase, lowercase or underscore.
4Always keep remembered keyword should be in lower case.An identifier in variable declaration accepts underscore or upper case or lower case.
5Keywords always contain alphabetic characters.An identifier in data types always in lower case.
6They help to build a specific instruction that exists within the computer programming language.The identifier can be any unique name that can be used to identify the variable.
7In Keywords, no special symbols and punctuations are used.No punctuation or special symbol except ‘underscore’ is used.
8Keywords examples are int, char, if, while, do, class etc.type identifier: Here type means is the keyword that represents the data type, for example, intfloat, boolean, string etc.
Difference Between Identifiers and Keywords

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Shiva Patra
Shiva Patra

Hello, this is G. Shiva. Patra, your content writer for this website, is currently pursuing BCA for a graduation degree. I am here to write content based on C++ and similar kind of stuff.
I like the tech world as it grows really fast and you have great things to learn every day which keep you away from boredom. So, I have some knowledge on very different topics related to tech like the dark web, deep web, onion rooting, IoT, Cybersecurity, and I always keep track of new devices and software used by them as every tech enthusiast does.

Articles: 9

Newsletter Updates

Enter your email address below to subscribe to our newsletter