C + + Tokens :
The group of characters that logically belong together is called Token.Tokens are of following 4 types:-
1. Identifiers
2.Keywords
3.Constants
4.Operators
1. Identifiers:
A program can use various data items with symbolic names in C + +. Identifiers refer to the names of functions, arrays, variables, classes etc. created by a programmer. There are certain rules for naming these identifiers in each languages. An Identifier, generally contains symbolic name. These symbolic names are used in various C + + programs. The rule of naming Identifiers in C and C + + are similar.
Rules for naming identifiers:
1. Uppercase letters and lowercase letters are distinct.
There is a difference between Ravi and RAVI.
2.An Identifiers name is to be made up of alphabets and digits. No other special characters are allowed.
3.The Underscore ( _ ) is the only special characters that is allowed to form the name of an identifier.
4.The underscore is treated as an alphabet.
5.The keywords are not allowed to be used as identifier names.
Following are some valid identifier names
a abc abc123 max_sal_law_weight
Following are some invalid identifier names with the reasons of invalidity
break - the keyword is not allowed as identifier name.
4XYZ - the identifier name must not begin with digit.
max + sal - the character ‘+’ is not allowed.
hello welcome - the blank space is not allowed.
2. Keywords:-
The keywords are nothing but the words which has got predefined meaning in that language.As a programmer we can not change meaning of these keywords. These keywords must be used for their predefined purpose. The keywords are also called as reserved words.
auto do inline short typedef
break double int signed union
case else long sizeof unsigned
catch enum new static virtual
char extern operator struct vopd
class float private switch volatile
const for protected template while
continue friend public this throw
3.Constants:-
The items whose values are not changed during the execution of the program are called constants. If an attempt is made to change their values, the program will not successfully execute and generate errors.
Each constant is determined by its value and form C + + has threetypes of constants:
1)Numeric constant
2)Character Constant
3)String Constant.
4. OPERATORS:-
An operator is a symbol or letter which causes the complier to take an action and yield a value.An operator acts on different data items entities called Operands.
For example,
X + Y is an expression in which sign + is an operator which specifies the operation of addition.
Types Of Operators:
1.Arithmetical Operators
2.Increment Decrement Operators
3.Binary Operators
4.Logical Operators
5.Conditional Operators
1.Arithmetical Operators:
Arithmetical Operators are used for various mathematical calculations. The result of an arithmetical expression is a numerical value. An arithmetical expression may be a variable or a constant or a combination of both. These variables or functions are connected with arithmetical operators.
Arithmetical operators are of following types.
i) Unary Arithmetic Operator
ii) Binary Arithmetic Operator
2. Increment and Decrement Operator:
Increment operator:- (+ +) The increment operator (+ +) is used to increment the value of a variable by 1.
The increment operator is either used as pre-increment operator or post-increment operator.
Consider the following example
Pre–increment Post – increment
x = 3 x = 3
+ + x ; x + + ;
Due to pre – increment Due to post –increment operator
operator the value of x the value of x will be incremented
will be incremented by 1 by 1 and it will become 4.
and it will become 4.
Decrement Operator (--)
The decrement operator (--) is used to decrement the value of a variable by 1.
The decrement operator is either used as pre-increment operator or post-decrement operator.
Consider the following example.
Pre-decrement Post-decrement
x = 3 x = 3;
- - x; x - -;
Due to pre-decrement Due to post-decrement
operator the value of operator the value of x
x will be decremented by will be decremented by 1
1 and it will become 2. and it will become 2.
3. Relational Operators:
Relational operators are used to determine the relationship between different operands. These are used in the work of comparison also . There are two mathematical expression and these are connected by a relational operator. The relational expression returns O if the relation is false and returns 1 if the relation is true. The six relational operators are shown in the following table.
Symbol Meaning
> Greater then
< Less then
>= Greater than , equal to
< = Less than, equal to
= = Equal to
! Not equal to
4. Logical Operators :
Logical operators combine the results of two or more than two expressions. The mode of connecting relationship in these expressions refers as logic and the expressions are called as logical expressions.
The logical expression returns 1 if the result is true and it returns 0 if the result is false.
C + + provides three logical operators which are listed below.
Symbol Meaning
! Logical negation
II Logical OR
i Logical Operator (!)
5. CONDITIONAL OPERATOR:
Conditional operator is also called ternary operator. It is represented by (? :). This is the only operator in C + + which takes three terms (Operands) and returns 0 value. The value of an expression using this operator is the value of either tits second operand or third operand, depending upon the value of first operand. However, ternary operator is some times o confused control structure.
(expression 1) ? (expression 2) : (expression3);
If expression 1 is true, the result of the operator is the value of expression 2; otherwise it results in the value of expression 3.
Simply speaking, the first operand is a test condition, the second and the third operands represent the final value of the expression and out of these values, one is selected depending upon the value of first operand.