Hey guys, what is up. I hope so you all are fine and are doing quite well. In this particular post, we will be talking about what are tokens in C++. Its types with examples.

What are tokens in C++?

TOKENS IN C++
PICTURE FROM PINTEREST.COM

Tokens are the smallest single parts in a program. These words are identified by the compiler.

C++ has the following tokens:

  • Keywords
  • Identifiers
  • Literals
  • Punctuators
  • Operators

KEYWORDS:

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

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 program. It is used to name a variable, constant, functions, classes, subjects, etc. To name a variable 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 consist of a sequence 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 enclosed in single quotes. Ex: ‘a’, ‘*’. A character constant is stored by its ASCII value. 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 sequence of characters enclosed in double-quotes and these characters gets print as it is. Ex: “1234”, “New Delhi”. It has no extra space for a null character or a string is terminated by 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 type of token 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: +, -, *, /, %
  2. RELATIONAL OPERATORS: <>, <=, >=, ==, !=

In the upcoming post, you will be getting information related to the latest technology and some amazing facts about technology. 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:

0 Comments

Leave a Reply

Avatar placeholder

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