Hey guys, what is up. In this particular post, we will be talking about tokens in C++, its types with examples.

(Updated on 24/9/2021)

What are tokens in C++?

TOKENS IN C++
PICTURE FROM PINTEREST.COM

Tokens in C++ are the smallest single parts that are used in a program. These words are identified by the compiler and they have a special role in the coding.

Common used Tokens in C++:

  • Keywords
  • Identifiers
  • Literals
  • Punctuators
  • Operators

KEYWORDS:

A keyword has a special meaning in the coding/programming language. It is also known as a reserved or restrained word. As the name depicts it is reserved for some special purpose in the coding.

The original C++ contains the following keywords:

ContinueSignedclassnewvoid
FloatUnsignedstructsize ofdelete
Intifenumstaticdefault
Charelseconstswitchoperator
Doubleforbreakprivatereturn
Longwhileautopublicinline
shortdogotoprotectedExtern

IDENTIFIERS:

An identifier is the basic building block of a code/program. It is used to name a constant, variable, classes, functions, subjects, etc. To name a variable in a program there are some rules.

RULES FOR NAMING IDENTIFIERS:

  1. Digits and numbers are acceptable.
  2. Special characters except for underscore ( _ ) are not allowed.
  3. An identifier must begin with a letter or an underscore but cannot begin with a number.
  4. C++ is case sensitive therefore, capital and small letters are treated differently.
  5. Keywords cannot be used as identifiers.
  6. Identifiers should consist of a letter or an underscore.

LITERALS:

These are the data elements/items that never alter their value during the program run. Literals are of several types:-

  1. Integers
  2. Float
  3. Characters
  4. String
  5. Bool Constant

INTEGERS:

These are whole numbers without any fractional part i.e., they must contain a decimal constant. A (+) or () sign can proceed with the digit. However, no sign indicates a positive number. No commas (,) cannot appear in an integer constant. 

These are of 3 types:

  • Decimal ( base 10 )
  • Octal ( base 8 )
  • Hexadecimal ( base 16 )

Decimal integer constants comprises of a segment of digits. It cannot begin with a digit zero. Ex: -90, 78, 0.

Octal integer is a sequence of digits starting with zero. Ex: 0237 = 2×82 + 3×81 + 7×80 = 159

Hexadecimal integer constant is the sequence of digits preceeding by 0x or 0X. It is taken to be as hexadecimal integer constant. Ex: 0x3ac = 3×162 + 10×161 + 12×160 = 940

CHARACTER CONSTANT:

A character constant is only a single character that is enclosed in single quotes whenever used. Ex: ‘a’, ‘*’. A character constant is stored using its ASCII value in the compiler. Ex: ‘A’=65. C++ allows certain non-graphic elements in character constant. 

FLOATING POINT CONSTANT: 

Floating-point constants are also real constant as they are numbers having a fractional part.

They are written in two forms:

  1. FRACTIONAL FORM
  2. EXPONENT FORM
  • FRACTIONAL FORM: A real constant in fractional form consists of digits that can be a sign or unsign. Also a decimal between the digits. At least one digit before the decimal and at least one after the decimal. Ex: -67.0, 3.97, +12.78.
  • EXPONENT FORM: A real constant in exponent form consists of two-part a mantissa and exponent. The mantissa must be either an integer or a proper real constant in fractional form. The exponent must be an integer. The mantissa is followed by the letter e or E that represents the exponent. Ex: 45.67e4, -6.89E-3, 0.45e+5 

STRING LITERAL CONSTANT:

A string literal is a pattern of characters that are enclosed in double-quotes and these characters gets print as it is. Ex: “1234”, “New Delhi”. It has no additional space for a null character or a string, that is terminated using a null character. 

BOOL LITERAL CONSTANT: 

There are two boolean characters 0,1. Where 0 stands for false and 1 stands for truth and in C++ everything except 0 is true.

PUNCTUATORS:

These are special characters used as separators.

USES OF PUNCTUATORS ARE:-

{ } – These are curly brackets

[ ] – In arrays

( ) – Parenthesis are used to change the preference of operators in an expression

, – A separator in a list of text

; – A a terminator

: – It indicates a labeled statement

* – Pointer declaration

= – As an assignment operator

# – As pre-processor directive

OPERATORS:

An operator is a special and an important type of token in coding that initiates some operations when applied to constants and variables used in expressions to have a particular output. These are of 2 types:

  1. ARITHEMATIC OPERATORS IN C++: *, -, +, %, /
  2. RELATIONAL OPERATORS: <>, <=, >=, ==, !=

In the upcoming post, you will be getting information related to the latest technology, some amazing facts about technology, and Header files in C++. So stay tuned for upcoming post. If you haven’t read about DATA TYPES AND TYPE MODIFIERS IN C++ then go and read this.

Categories: CODING

0 Comments

Leave a Reply

Avatar placeholder

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