[go: up one dir, main page]

100% found this document useful (1 vote)
887 views22 pages

C++ Tokens

The document discusses the different types of tokens in C++ - identifiers, keywords, constants, and operators. Identifiers are names given to variables, functions, classes etc. and have rules for naming. Keywords are reserved words that cannot be used as identifiers. Constants represent fixed values that do not change. Common operators include arithmetic, relational, and logical operators.

Uploaded by

utkaljyoti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
887 views22 pages

C++ Tokens

The document discusses the different types of tokens in C++ - identifiers, keywords, constants, and operators. Identifiers are names given to variables, functions, classes etc. and have rules for naming. Keywords are reserved words that cannot be used as identifiers. Constants represent fixed values that do not change. Common operators include arithmetic, relational, and logical operators.

Uploaded by

utkaljyoti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

TOKENS OF C++

The group of characters that forms an Basic Building block is called as Token.

The types of tokens are:-

1. Identifiers
2. Keywords
3. Constants
4. Operators

Identifiers

Various data items with symbolic names in C++ is called as Identifiers. Following
data items are called as Identifier in C++ –

1. Names of functions

2. Names of arrays

3. Names of variables
4. Names of classes

The rules of naming identifiers in C++:

1. C++ is case-sensitive so that Uppercase Letters and Lower Case letters are

different

2. The name of identifier cannot begin with a digit. However, Underscore can be

used as first character while declaring the identifier.

3. Only alphabetic characters, digits and underscore (_) are permitted in C++

language for declaring identifier.

4. Other special characters are not allowed for naming a variable / identifier
5. Keywords cannot be used as Identifier.

Some Examples of Identifier in C++ :

Identifier Validity Reason

1digit Invalid Digit at first location is not allowed


Identifier Validity Reason

Special characters other than underscore is not


digit-1 Invalid
allowed

num 1 Invalid Space not allowed

Program

#include <iostream.h>

using namespace std;

void main()

int a;

int A;

cout<<"Enter the values of 'a' and 'A'";

cin>>a;

cin>>A;

cout<<"\nThe values that you have entered are : "<<a<<" , "<<A;

In the above code, we declare two variables 'a' and 'A'. Both the letters are same but
they will behave as different identifiers. As we know that the identifiers are the case-
sensitive so both the identifiers will have different memory locations.

Note:- Identifiers cannot be used as the keywords. It may not conflict with the
keywords, but it is highly recommended that the keywords should not be used as the
identifier name. You should always use a consistent way to name the identifiers so
that your code will be more readable and maintainable.
Keywords

In C++, keywords are reserved identifiers which cannot be used as names for the
variables in a program.

C++ Keyword List :

asm else operator template

auto enum private this

break extern protected throw

case float public try

catch for register typedef

char friend return union

class goto short unsigned

const if signed virtual

continue inline sizeof void

default int static volatile

delete long struct while

double new switch -

Keywords cannot be used for the –

1. Declaring Variable Name

2. Declaring Class Name


3. Declaring Function Name

4. Declaring Object Name

Or

A list of 32 Keywords in C++ Language which are also available in C language are
given below.

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

A list of 30 Keywords in C++ Language which are not available in C language are
given below.

asm dynamic_cast namespace reinterpret_cast bool

explicit new static_cast false catch

operator template friend private class

this inline public throw const_cast

delete mutable protected true try

typeid typename using virtual wchar_t


Constants

Constants refer to as fixed values, unlike variables whose value can be altered, constants -
as the name implies does not change, they remain constant. Constant must have to be
initialized at the time of creating it, and new values cannot be assigned later to it.

Integer Constants

As the name itself suggests, an integer constant is an integer with a fixed value, that
is, it cannot have fractional value like 10, -8, 2019.

For example,

const signed int limit = 20;

 Decimal number system constant: It has the base/radix 10. ( 0 to 9)


For example, 55, -20, 1.
In the decimal number system, no prefix is used.
 Octal number system constant: It has the base/radix 8. ( 0 to 7 )
For example, 034, 087, 011.
In the octal number system, 0 is used as the prefix.
 Hexadecimal number system constant: It has the base/radix 16. (0 to 9, A
to F)
In the hexadecimal number system, 0x is used as the prefix
Floating or Real Constants

We use a floating-point constant to represent all the real numbers on the number
line, which includes all fractional values.

For instance,

const float pi = 3.14159;


We may represent it in 2 ways:

 Decimal form: The inclusion of the decimal point ( . ) is mandatory.


For example, 2.0, 5.98, -7.23.
 Exponential form: The inclusion of the signed exponent (either e or E) is
mandatory.
For example, the universal gravitational constant G = 6.67 x 10-11 is
represented as 6.67e-11 or 6.67E-11.
Character Constants
Character constants are used to assign a fixed value to characters including
alphabets and digits or special symbols enclosed within single quotation marks( ‘
’ ).

Each character is associated with its specific numerical value called the ASCII
(American Standard Code For Information Interchange) value.

Apart from these values, there is a set in C known as Escape Sequences

For example, ‘+’, ‘A’, ‘d’.


Operator

An operator is simply a symbol that is used to perform operations. There can be


many types of operations like arithmetic, logical, bitwise etc.

There are following types of operators to perform different types of operations in C


language.

o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operator
o Unary operator
o Ternary or Conditional Operator
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Eample

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a variable by ++x


1

-- Decrement Decreases the value of a variable --x


by 1
Binary Plus Operator (+):-

#include <iostream.h>
#include <conio.h>
using namespace std;
void main()
{
int x,y;
cout<<”Enter any 2 numbers”;
cin>>x>>y;
clrscr();
cout <<”Sum of 2 nos is ”<< x + y;
}
Output:-
Enter any 2 numbers
4
5
Sum of 2 nos is 9.

Binary Minus Operator (-):-

#include <iostream.h>

using namespace std;

void main()

int x = 5;

int y = 3;

cout << x - y;

Output:- 2
Binary Multiplication operator (*):-

#include <iostream.h>

using namespace std;

void main()

int x = 7;

int y = 4;

cout << x * y;

Output:- 28

Binary Division operator(/) :-

#include <iostream.h>

using namespace std;

void main()

int x = 15;

int y = 5;

cout << x / y;

Output: - 3
Binary Modulus Operator(%)

#include <iostream.h>

using namespace std;

void main()

int x = 18;

int y = 6 ;

cout << x % y;

Output:- 3

Unary Increment Operator:-

Pre-increment Operator :- This operator increment the value first assigned to a


variable which show the incremented result.

#include <iostream.h>

using namespace std;

void main()

int x = 5;

++x;

cout << x;

Output :- 6
Post-increment Operator :- This operator stores the value first then increment it
which is assigned to a variable.

#include <iostream.h>

using namespace std;

void main()

int x = 5;

x++;

cout << x;

Output :- 6

Pre-decrement Operator :- This operator decrement the value first assigned to a


variable which show the incremented result.

#include <iostream.h>

using namespace std;

void main()

int x = 7;

--x;

cout << x;

Output :- 6
Post-decrement Operator :- This operator stores the value first then decrement it
which is assigned to a variable.

#include <iostream.h>

using namespace std;

void main()

int x = 5;

x--;

cout << x;

Output :- 4
Comparison Operators/Relational

Comparison/Relational operators are used to compare two values.

Note: The return value of a comparison is either true (1) or false (0).

In the following example, we use the greater than operator (>) to find out if
5 is greater than 3:

Example

int x = 5;

int y = 3;

cout << (x > y); // returns 1 (true) because 5 is greater than 3

A list of all comparison operators:

Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


Example1:-

#include <iostream.h>

using namespace std;

void main()

int x = 5;

int y = 3;

cout << (x == y); // returns 0 (false) because 5 is not equal to 3

return 0;

Output 0

Example2:-

#include <iostream.h>

using namespace std;

void main()

int x = 5;

int y = 3;

cout << (x != y); // returns 1 (true) because 5 is not equal to 3

return 0;

Output 1
Example 3:-

#include <iostream.h>

using namespace std;

void main()

int x = 5;

int y = 3;

cout << (x > y); // returns 1 (true) because 5 is greater than 3

Output  1

Example 4:-

#include <iostream.h>

using namespace std;

void main()

int x = 5;

int y = 3;

cout << (x < y); // returns 0 (false) because 5 is not less than 3

Output  0
Example 5:-

#include <iostream.h>

using namespace std;

void main()

int x = 5;

int y = 3;

cout << (x >= y); // returns 1 (true) because five is greater than, or equal, to 3

Output  1

Example 6 :-

#include <iostream.h>

using namespace std;

void main()

int x = 5;

int y = 3;

cout << (x <= y); // returns 0 (false) because 5 is neither less than or equal to 3

return 0;

Output  0
C++Logical Operators

Logical operators are used to determine the logic between variables or values:

Operator Name Description Example

&& Logical and Returns true if both statements x < 5 && x < 10
are true

|| Logical or Returns true if one of the x < 5 || x < 4


statements is true

! Logical not Reverse the result, returns false !(x < 5 && x < 10)
if the result is true

Ternary Operator:-

The conditional operator can often be used instead of the if else statement. Since it is the
only operator that requires three operands in c++, It is also called the ternary operator.

A ternary operator has the following form,


exp1 ? exp2 : exp3 ;
The expression exp1 will be evaluated always. Execution of exp2 and exp3 depends
on the outcome of exp1. If the outcome of exp1 is non-zero exp2 will be evaluated,
otherwise exp3 will be evaluated.
Example :- X = Y > 5 ? 4 : 8;

If Y is greater than 5 then 4 will be assigned to variable X or else the value 8 will be
assigned to X
C++ program to find greatest of 3 numbers by using conditional /ternary operator

# include <iostream.h>

void main()
{
int a, b, c, big ;

printf("Enter three numbers : ") ;

cin>>a>>b>>c;

big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;

cout<<”The biggest number is”<< big;


}

Output :-
Enter three numbers:
4
1
8
The biggest number is 8;
Precedence of Operators in C++:-

The precedence of operator species that which operator will be evaluated first and
next. The associativity specifies the operators direction to be evaluated, it may be left
to right or right to left.

The precedence and associativity of C++ operators is given below:

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

Additive +- Right to left

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == !=/td> Right to left

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Right to left

Logical AND && Left to right

Logical OR || Left to right


Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Comma , Left to right

You might also like