C++ Tokens
A token is the smallest element of a
program that is meaningful to the compiler. Tokens can be classified as
follows:
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Special Symbols
6. Operators
Fig: c++ token |
1. Keyword:
Keywords are pre-defined or reserved words in a programming
language. Each keyword is meant to perform a specific function in a program.
Fig: Keyword in c++ |
2. Identifiers:
Identifiers are used as the general terminology for naming of variables, functions and arrays. These are user defined names consisting of arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first character. Identifier names must differ in spelling and case from any keywords. You cannot use keywords as identifiers.
Fig: identifier in c++ |
There are certain rules that should be followed while naming c++ identifiers:
·
They must begin with a letter or
underscore(_).
·
They must consist of only letters,
digits, or underscore. No other special character is allowed.
·
It should not be a keyword.
·
It must not contain white space.
·
It should be up to 31 characters
long as only first 31 characters are significant.
3. Constants:
Constants are also like normal variables. But, only
difference is, their values can not be modified by the program once they are
defined. Constants refer to fixed values. They are also called as literals.
Syntax:
const data_type variable_name;
const data_type *variable_name;
Types of Constants:
1.
Integer constants – Example: 0, 1, 1218, 12482
2.
Real or Floating point constants
– Example: 0.0, 1203.03,
30486.184
3.
Octal & Hexadecimal constants
– Example: octal: (013 )8 = (11)10, Hexadecimal:
(013)16 = (19)10
4.
Character constants -Example: ‘a’, ‘A’, ‘z’
5.
String constants -Example: “Pramod Dwivedi”
4. Strings:
Strings are nothing but an array of
characters ended with a null character (‘\0’).This null character indicates the
end of the string. Strings are always enclosed in double quotes. Whereas, a
character is enclosed in single quotes in C and C++.
Declarations for String:
·
char string[20] = {‘p’, ’r’, ‘a’,
‘o’, ‘d’, ‘d’, ‘w’, ‘i’, ‘v’, ’e’, ‘d’, ‘i’, ‘\0’};
·
char string[20] = “pramoddwivedi”;
·
char string [] = “pramoddwivedi”;
Fig: String in c++ |
5. Special Symbols:
The following special symbols are used in C
having some special meaning and thus, cannot be used for some other purpose.[]
() {}, ; * = #
·
Brackets[]: Opening and closing brackets are used as array element
reference. These indicate single and multidimensional subscripts.
·
Parentheses(): These special symbols are used to indicate function calls
and function parameters.
·
Braces{}: These opening and ending curly braces marks the start
and end of a block of code containing more than one executable statement.
·
comma (,
): It is used to separate more than one
statements like for separating parameters in function calls.
·
semi
colon : It is an operator that
essentially invokes something called an initialization list.
·
asterick
(*): It is used to create pointer
variable.
· assignment operator: It is used to assign values.
·
pre
processor(#): The preprocessor is a macro
processor that is used automatically by the compiler to transform your program
before actual compilation.
6. Operators:
Operators are symbols that triggers an action when applied to
C variables and other objects. The data items on which operators act upon are
called operands.
There are three type of operators in C++
·
Unary Operators:
Those
operators that require only single operand to act upon are known as unary
operators.
For Example
increment and
decrement operators
·
Binary Operators:
Those
operators that require two operands to act upon are called binary operators. Binary operators are classified into :
1.
Arithmetic operators
2.
Relational Operators
3.
Logical Operators
4.
Assignment Operators
5.
Conditional Operators
6.
Bitwise Operators
· Ternary Operators:
These operators requires three
operands to act upon.
For Example
Conditional operator(?:).
Fig: Operator In C++ |
No comments:
Post a Comment