C++ Tokens – Keywords, Constants, Identifiers, Strings, Operators & Special Symbols

Here you will learn about C++ tokens, keywords, constants, identifiers, strings, operators and special symbols.

What are Tokens?

In simple words, we can say that tokens are the smallest component pertaining to any program that make sense to the compiler, i.e. compiler can very easily understand that. We can classify the tokens into six different sub-categories as follows:

  • Keywords
  • Constants
  • Strings
  • Identifiers
  • Operators
  • Special Symbols

Now we are going to illustrate these one by one and will see what exactly they mean.

Keyword

The keywords are the reserved terms in any programming language. Every keyword in the language is expected to provide an intended functionality to the program. We cannot use the keywords  as variable names because this practice tries to assign a new meaning to the keyword which is not appreciated nor allowed in any programming language. However, the C/C++ pre-processor directives (so-called header files) can be used to specify text to be exchanged for keywords before compilation. C language preferably supports for 32 keywords that we have mentioned in the tabular form below:

do for float continue
void else if switch
struct enum register char
typedef static return default
const int short volatile
while double break signed
union long sizeof unsigned
extern goto auto case

Apart from supporting these 32 keywords, C++ has 31 more keywords which are:

typeid false catch new
this delete try explicit
export typename using inline
asm namespace class throw
private protected public true
static_cast template const_cast operator
reinterpret_cast dynamic_cast mutable bool
friend virtual wchar_t

Constants

Constants are normally the variables. The only thing that differentiates Constants from Variables is the fact that it is not allowed to modify the value of a constant by the program after the constants have already been defined.

Constants refer to fixed values.

Constants are also sometimes referred as Literals.

They may belong to any of the data types.

Constant types

  • Integer constants: For example: 0, 5, 957, 12376 etc.
  • Floating Point / Real constants: For example: 0.7, 8.65, 4587.05 etc.
  • Octal and Hexadecimal constants: For example:
    • Octal: (15)8  = (13)10
    • Hexadecimal: (015)16 = (21)10
  • Character constants: For example: ‘a’, ‘A’, ‘x’, ‘Z’ etc.
  • String constants:  For example: “TheCrazyProgrammer”

Strings

Strings are regarded as an array of characters, ending with a null character “\0”. The null character is used to point the ending of the specified string.

Strings, in practical use are expected to be enclosed in double quotes (“ ”), whereas the character within single quotes (‘ ’).

C++ Tokens 1

The above declarations can be illustrated as:

  1. When char is declared as “flag[25]”, that means 25 bytes of space in the memory is provided for accommodating the value of the string.
  2. But when we’ve declared char as “flag[]”, the space in the memory will be provided by the CPU strictly according to the requirement at runtime.

Identifiers

Identifiers are saviors. We can use them as the general terminology for the naming purpose of variables, arrays, and functions. Identifiers are the user-defined names which may consist of a long sequence of letters and digits with either a letter or the underscore (_) as a first character.

Keywords can’t be used as identifiers, as they are reserved for special purpose. To use the identifiers in program statements we need to declare it in the program itself.

Rules for naming C++ identifiers

  • Identifiers must begin with a letter or underscore( _ ).
  • No special characters are allowed in identifier naming, only letters, digits or underscores can be used.
  • A keyword can’t be an identifier.
  • White space is not allowed within identifier
  • It should only be up to 31 characters long, as only first 31 characters are significant.

C identifiers examples

C++ Tokens 2

Operators

The operators are the symbols that are commonly used to trigger some action when applied to variables or other objects. The operator needs some data items to act upon, those data items are referred to as Operands. For example in (a + b), ‘+’ sign is the operator whereas ‘a’ & ‘b’ are the operands.

Types of Operators

  • Unary Operators: The operators that work upon a single operand only are called Unary Operators. Example:- Increment & decrement operators.
  • Binary Operators: As clear from its name itself, Binary Operators are those which require two different operands to work upon. They can be classified into:
    1. Arithmetic Operators
    2. Relational Operators
    3. Logical Operators
    4. Assignment Operators
    5. Conditional Operators
    6. Bitwise Operators
  • Ternary Operators: The operators that require three different operands to work upon are known as Ternary Operators. Conditional Operator (?:) is an example of ternary operator.

Special Symbols

The symbols that are used in C/C++ with some special meaning and for some specific function are called as Special Symbols.

The special symbols being used with context to programming language are illustrated below as:

  • Brackets []: These opening and closing brackets are used as array element reference. These are used to indicate single & multidimensional subscripts.
  • Braces {}: Opening and closing curly braces are used to mark start and end of a block of code containing more than one statement.
  • Comma ( , ): To separate more than one statement, Comma is used for example in for loop comma separates initialization, condition & increment.
  • Semicolon ( ; ): Used at the end of statements for termination.
  • Parenthesis () : Are used to indicate function parameters & function calls.
  • Asterick ( * ): This special symbol is used to create a pointer variable.
  • Assignment Operator ( = ): For assigning values, this special symbol is used.
  • Preprocessor ( # ): This you must have seen attached with the header files. This is automatically used by the compiler to transform your program before actual compilation.

 Reference: http://www.geeksforgeeks.org/cc-tokens/

Comment below if you have any queries related to above C++ tokens tutorial.

5 thoughts on “C++ Tokens – Keywords, Constants, Identifiers, Strings, Operators & Special Symbols”

  1. Token is a smallest individual unit. These are six in no.. These are: keywords, operaters, strings, identifiers, constants, special symbols.

  2. hey man, how can i assign the value of token[2] to a new vaiable ??
    example: new = token[2]; <– this does not work for me ..

    #include
    #include
    int main()
    {
    const char *token[] = {“ErSh”,”TriP”,”pOw”,”ElL”};
    printf(“concaternate all tokens : %s%s%s%s\n”, token[0], token[1], token[2], token[3]);
    }

  3. Your article easily explains clearly about the C++ tokens, the keywords, operator and its classification, etc and it is very useful for computer science students. Thanks for sharing.

Leave a Comment

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