C++ Tokens ( Keywords, Identifiers, Literals, Punctuators and Operators)

C++ tokens are the smallest individual unit in a program, C++ tokens are Keywords, Identifiers, Literals, Punctuators and Operators

C++ Tokens ( Keywords, Identifiers, Literals, Punctuators and Operators)

In this tutorial, we will learn about C++ Tokens with the help of examples and if you are following our full C++ course then you probably know that we have given a small introduction about C++ Tokens but in this tutorial, we will dive deeper to know more about types of C++ Tokens.

The smallest individual unit of a C++ program is known as Tokens.

Types of C++ Tokens:

Keywords

These are the words that have special meaning associated with them, they pass special meaning to the language compiler.

Some frequently used C++ keywords are listed below:

bool break case char class const  continue
default delete double do else enum false
float for goto if inline int long
namespace new private protected public return short
signed sizeof static struct switch this true
try typedef typename unsigned using void while

If you want to know more C++ keywords please refer to this Complete C++ Keyword List.

Identifiers

It is the name given by the user to the variable, function, class, method, or any other user-defined data types. It is basically the name given by the user for a unit of the program.

C++ is case sensitive as it treats upper and lower case characters differently. So we need to take care of the lower and upper cases.

There are some rules which need to be followed while defining any identifier. Rules which need to be followed are as follows:

  1. It can not contain special characters. (other than A-Z, a-z, 0-9)
  2. It can not start with the digit.
  3. It can not be a reserved keyword.
  4. It can not contain spaces in it. (Use underscore ' _ ' in place of spaces).

Examples of some valid and invalid identifiers.

  • My-fILE (INVALID)
  • My_fiLE (VALID)
  • 26hELLO (INVALID)
  • Hello26 or H26ello (VALID)
  • break, for or void (any keyword in INVALID)
  • Hello_World (VALID)
  • Hello World (INVALID)

Literals

It also called constant, these are data items that never change its value during the program run.

C++ allows  several types of literals :

  1.  integer-constant
  2. character-constant
  3. floating-constant
  4. string-constant
  5. boolean-constant
  6. defining-constant

We will discuss each type of literals deeply in the next tutorial, for now, we should move forward to punctuators and operators.

Punctuators

The characters that are used to enhance the readability and give proper meaning to statements, expressions, beautify the program, etc. according to the syntax.

The following characters are used as punctuators and are also as separators in C++:

[ ] ( ) { } , ; : * ... = #

Operators

These are tokens that trigger some computation or action when applied to variables and other objects in an expression.

There are different types of operators in C++:

  1. Input/Output Operators (<< and >>).
  2. Arithmetic Operators ( +, -, *, /, % ).
  3. Increment/Decreament Operators ( ++, -- ).
  4. Relational Operators ( ==, <, >, <=, >=, != )
  5. Logical Operators ( && , ||, ! )
  6. Conditional Operator ( ?: )

Let's stop here in this tutorial we will move forward in the next tutorial.

What's Your Reaction?

like
1
dislike
0
love
1
funny
0
angry
0
sad
0
wow
1