Site icon DataFlair

Tokens in C++ (Keywords, Identifiers, Constants, Strings, Operators, Special Symbols )

Free C++ course with real-time projects Start Now!!

Tokens and keywords are undoubtedly notable features of C++. 

Tokens act as building blocks of a program. Just like a living cell is the smallest possible unit of life, tokens in C++ are referred to as the smallest individual units in a programKeywords in C++ help the user in framing statements and commands in a language. Each keyword conveys a unique connotation to the compiler to perform a specific task. Just like the combination of words helps us in framing sentences, the combination of keywords helps us in framing statements to perform logical operations in a programming language. Simply combining keywords wouldn’t help to serve the purpose.

As we need to use proper grammar to form a meaningful sentence, we need to be well-acquainted with the syntax of C++ to instruct the compiler what to do. If these statements are not formed in a logical manner, they would sound gibberish and you would get a compilation error. Let’s discuss the concept of Tokens with Character set in C++ in detail.

1. C++ Character Set

Before we begin with C++ tokens, let us understand what Character set has to offer.

C++ Character set is basically a set of valid characters that convey a specific connotation to the compiler. We use characters to represent letters, digits, special symbols, white spaces, and other characters.

The C++ character set consists of 3 main elements. They are:

  1. Letters: These are alphabets ranging from A-Z and a-z (both uppercase and lowercase characters convey different meanings)
  2. Digits: All the digits from 0 – 9 are valid in C++.
  3. Special symbols: There are a variety of special symbols available in C++ like mathematical, logical and relational operators like +,-, *, /, \, ^, %, !, @, #, ^, &, (, ), [, ], ; and many more.

To understand the concept in the better way, learn Data Types in C/C++ 

2. Tokens in C++

As discussed earlier, tokens in C++ are the smallest individual unit of a program.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

The following tokens are available in C++ which are similar to that seen in C with the addition of certain exclusive keywords, strings, and operators:

3. C++ Keywords

Keywords in C++ refer to the pre-existing, reserved words, each holding its own position and power and has a specific function associated with it.

It is important to note that we cannot use C++ keywords for assigning variable names as it would suggest a totally different meaning entirely and would be incorrect.

Get the Samurai Technique to Learn Arrays in C and C++

Here is a list of keywords available in C++ according to the latest standards:

alignas alignof asm auto bool break
case catch char char16_t char32_t class
const constexpr const_cast continue decltype default
delete double do dynamic_cast else enum
explicit export extern FALSE float for
friend goto if inline int long
mutable namespace new noexcept nullptr operator
private protected public register reinterpret_cast return
short signed sizeof static static_assert static_cast
struct switch template this thread_local throw
TRUE try typedef typeid typename union
unsigned using virtual void volatile wchar_t
while

4. C++ Identifiers

C++ allows the programmer to assign names of his own choice to variables, arrays, functions, structures, classes, and various other data structures called identifiers. The programmer may use the mixture of different types of character sets available in C++ to name an identifier.

Rules for C++ Identifiers

There are certain rules to be followed by the user while naming identifiers, otherwise, you would get a compilation error. These rules are:

  1. First character: The first character of the identifier in C++ should positively begin with either an alphabet or an underscore. It means that it strictly cannot begin with a number.
  2. No special characters: C++ does not encourage the use of special characters while naming an identifier. It is evident that we cannot use special characters like the exclamatory mark or the “@” symbol.
  3. No keywords: Using keywords as identifiers in C++ is strictly forbidden, as they are reserved words that hold a special meaning to the C++ compiler. If used purposely, you would get a compilation error.
  4. No white spaces: Leaving a gap between identifiers is discouraged. White spaces incorporate blank spaces, newline, carriage return, and horizontal tab.
  5. Word limit: The use of an arbitrarily long sequence of identifier names is restrained. The name of the identifier must not exceed 31 characters, otherwise, it would be insignificant.
  6. Case sensitive: In C++, uppercase and lowercase characters connote different meanings.

Before we move ahead, you should know how to declare and define variables in C/C++

Here is a table which illustrates the valid use of Identifiers:

Identifier Name
Valid or Invalid Correction or alternative, if invalid 
Elucidation if invalid 
5th_element Invalid element_5
It violates Rule 1 as it begins with a digit
_delete Valid
school.fee Invalid school_fee
It violates Rule 2 as it contains a special character ‘.’
register[5] Invalid Register[5]
It violates Rule 3 as it contains a keyword
Student[10] Valid
employee name
Invalid employee _name
It violates Rule 4 as it contains a blank space
perimeter() Valid

5. C++ Constants

Before we begin our discussion on constants in C++, it is important to note that we can use the terms “constants” and “literals” interchangeably.

As the name itself suggests, constants are referred to as fixed values that cannot change their value during the entire program run as soon as we define them.

Syntax:

const data_type variable_name = value;

Types of Constants in C++

The different types of constants are:

For instance:

const int data = 5;

For instance:

const float e = 2.71;

Refer to Constants and Literals in C++ for a detailed description.

For instance:

const char answer = ‘y’;

For instance:

const char title[] = ‘‘DataFlair’’;

const int oct = 034;

It is the octal equivalent of the digit 28 in the decimal number system.

const int hex = 0x40;

It is the hexadecimal equivalent of the digit 64 in the decimal number system.

6. C++ Strings

Just like characters, strings in C++ are used to store letters and digits. Strings can be referred to as an array of characters as well as an individual data type.

It is enclosed within double quotes, unlike characters which are stored within single quotes. The termination of a string in C++ is represented by the null character, that is, ‘\0’. The size of a string is the number of individual characters it has.

In C++, a string can be declared in the following ways:

char name[30] = ‘’Hello!”; // The compiler reserves 30 bytes of memory for the string.

char name[] = “Hello!”; // The compiler reserves the required amount of memory for the string.

char name[30] = { ‘H’ , ’e’ , ’l’ , ’l’ , ’o’};; // This is how a string is represented as a set of characters.

string name = “Hello” // The compiler reserves 32 bytes of memory.

7. Special Symbols

Apart from letters and digits, there are some special characters in C++ which help you manipulate or perform data operations. Each special symbol has a specific meaning to the C++ compiler.

Here is a table which illustrates some of the special characters in C:

Special Character Trivial Name Function
[ ] Square brackets
The opening and closing brackets of an array symbolize single and multidimensional subscripts.
() Simple brackets
The opening and closing brackets represent function declaration and calls, used in print statements.
{ } Curly braces
The opening and closing curly brackets to denote the start and end of a particular fragment of code which may be functions, loops or conditional statements
, Comma
We use commas to separate more than one statements, like in the declaration of different variable names
# Hash / Pound / Preprocessor
The hash symbol represents a preprocessor directive used for denoting the use of a header file
* Asterisk
We use the asterisk symbol in various respects such as to declare pointers, used as an operand for multiplication
~ Tilde
We use the tilde symbol as a destructor to free memory
. Period / dot
The use the dot operator to access a member of a structure

8. C++ Operators

Operators are tools or symbols which are used to perform a specific operation on data. Operations are performed on operands. Operators can be classified into three broad categories according to the number of operands used.

Unary: It involves the use of one a single operand. For instance, ’!’ is a unary operator which operates on a single variable, say ‘c’ as !c which denotes its negation or complement.

Binary: It involves the use of 2 operands. They are further classified as:

Ternary: It involves the use of 3 operands. For instance, ?: is used to in place of if-else conditions.

Enhance Your Fundamental Skills with Operators in C/C++

9. Quiz on Tokens in C++

Time limit: 0

Quiz Summary

0 of 15 Questions completed

Questions:

Information

You have already completed the quiz before. Hence you can not start it again.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    #include <iostream>

     

    using namespace std;

     

    int main()

    {

        int x=2, y=5;

        auto z;

        z=x+y;

        cout<<z;

     

        return 0;

    }

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    #include <iostream>

     

    using namespace std;

     

    int main()

    {

        int x=2, y=5;

        auto z=x+y;

        cout<<z;

     

        return 0;

    }

    Correct
    Incorrect
  3. Question 3 of 15
    3. Question

    #include <iostream>

     

    using namespace std;

     

    int main()

    {

        int x=10, y=20;

        const int z=1;

        z=x+y;

        cout<<z;

     

        return 0;

    }

    Correct
    Incorrect
  4. Question 4 of 15
    4. Question

    #include <iostream>

     

    using namespace std;

     

    int main()

    {

        int x=3, y=4;

        static int z=1;

        z=x+y;

        cout<<z;

     

        return 0;

    }

    Correct
    Incorrect
  5. Question 5 of 15
    5. Question

    #include <iostream>

     

    using namespace std;

     

    int main()

    {

        int y;

        int x=y=7;

        if(x==y)

        {

            x++;

            cout<<x;

            goto here;

            x++;

            cout<<x;

        }

        here: return 0;

    }

     

    Correct
    Incorrect
  6. Question 6 of 15
    6. Question

    #include <iostream>

     

    using namespace std;

     

    int main()

    {

        int y=7;

        int x=1;

        for(int i=0; i<x;i++)

        {

            x++;

            if(x==y)

            {

               break;

            }

        }

        cout<<x;

        return 0;

    }

     

    Correct
    Incorrect
  7. Question 7 of 15
    7. Question

    #include <iostream>

     

    using namespace std;

     

    int main()

    {

        int x=2;

        int y=1;

        cout<<sizeof(x+y);

        return 0;

    }

     

    Correct
    Incorrect
  8. Question 8 of 15
    8. Question

    #include <iostream>

     

    using namespace std;

    class Data{

        public:

        int x=10;

    };

    int main()

    {

        Data d;

        cout<<d.x;

        return 0;

    }

    Correct
    Incorrect
  9. Question 9 of 15
    9. Question

    #include <iostream>

     

    using namespace std;

    class Data{

        public:

        int y;

    };

    int main()

    {

        Data d;

        d.y=9;

        cout<<d.y;

        return 0;

    }

     

    Correct
    Incorrect
  10. Question 10 of 15
    10. Question

    #include <iostream>

     

    using namespace std;

    class Data{

        int x;

        public:

        int y;

        Data()

        {

            x=1;y=2;

            cout<<y<<x;

        }

    };

    int main()

    {

        Data d;

        return 0;

    }

    Correct
    Incorrect
  11. Question 11 of 15
    11. Question

    Which among the following is not a keyword?

    Correct
    Incorrect
  12. Question 12 of 15
    12. Question

     What is the use of * (asterisk) symbol in c++?

    Correct
    Incorrect
  13. Question 13 of 15
    13. Question

    )Which among the following is not a bitwise operator

    Correct
    Incorrect
  14. Question 14 of 15
    14. Question

     Which among the following is not a relational operator?

    Correct
    Incorrect
  15. Question 15 of 15
    15. Question

    Which among the following is not a part of token classification?

    Correct
    Incorrect

10. Summary

Now, you know why tokens in C++ are called as building blocks of a program. All the sub-parts of tokens are: keywords, identifiers, constants, strings, special symbols, and operators equally important and play a vital role in framing the programs in C++.  Every part is discussed well, refer all the links above and get a detailed description of each part.

Feedback and suggestions are welcomed in the comment section!

Exit mobile version