Like
the C language, C++ also comprises a character set from which the tokens (basic
types of elements essential for programming coding ) are constructed. The
character set comprises of “A” .. “Z” , “a” .. “z”, 0 .. 9, +, -, /, *,\, (, ),
[, ], {, }, =, !=, <, >, . , ’ “ ; : %, ! , &, ?, _, #, <=,
>=, @, white space, horizontal tab, carriage return and other characters.
A
quick recap of the basic types : The basic types are collectively called as TOKENS.
A token is the smallest individual unit in a program. Tokens are classified as
shown in Figure.
Classification of
Tokens
1. Keywords
Keywords
have special meaning to the language compiler. These are reserved words for
special purpose. These words cannot be used as normal identifiers. The following Table shows the list of keywords used in C++.
auto
break
case
const
class
|
continue
default
delete
do
else
|
enum
for
friend
goto
if
|
inline
new
operator
private
protected
|
public
return
signed
sizeof
static
|
struct
switch
this
unsigned
virtual
while
|
2. Identifiers
Identifiers are also called as variables. Variables are memory
boxes that hold values or constants. A variable name must begin with an
alphabet or underscore followed by alphabets or numbers. For example _test ;
test ; sum12 are some valid identifiers. We shall see more about variables
after dealing with data types
3. Constants
Constants are data items whose values cannot be changed. A
constant is of numeric or non-numeric type. Numeric constants consist of only
numbers, either whole numbers or decimal numbers. Integer, floating-point are
numeric constants.
3.1 Integer
Constant
·
Integer Constant must have at least one digit and must not contain
any fractional part.
·
May be prefixed with a + or – sign
·
A sequence of digits starting with 0 (zero) is treated as
Octal constant Ex. 010 = 8 ( [8] 10 = [10] 8 )
·
A sequence of digits starting with 0x is treated as
hexadecimal integer. Ex. 0xF = 15 ([15] 10 = [F] 16 )
3.2 Floating Point
Constant
Floating
Point Constant is a signed real number. It includes an integer portion, a
decimal point, a fractional portion and an exponent. While representing a floating
point constant the integer portion or the decimal portion can be omitted but
never both. For example 58.64 is a valid floating point (Real) constant. It can
be represented in exponent form as follows :
·
5.864E1
=> 5.864 x 101
=>
58.64
·
5864E-2
=> 5864 x 10–2
=>
58.64
·
0.5864E2
=> 0.5864x102
=>
58.64
The
letter E or e is used to represent the floating-point constant exponent form.
3.3 Character
Constant
Character
constant is a constant that contains a single character enclosed within single
quotes. It can be any character as defined in the character set of C++ language
(alphabet, numeral, mathematical, relational or any other special character as
part of the ASCII set). Certain special characters like tab, backspace, line
feed, null, backslash are called as non-graphic character constants. These
characters are represented using escape sequences. Escape sequences are
represented using characters prefixed with a backslash. The following Table shows the
escape sequences.
3.4 String Literal
String
Literal is a sequence of characters surrounded by double quotes. String
literals are treated as array of characters. Each string literal is by default
added with a special character ‘\0’ which marks the end of a string. For
example “testing”
4. Operator
Operator
specifies an operation to be performed that yields a value. An operand is an
entity on which an operator acts. For example:
RESULT = NUM1 + NUM2
NUM1 and NUM2 are
operands. + is the additional operator, that performs the addition of
the numbers. The result (value) generated is stored in the variable RESULT by
virtue of “=” (Assignment) operator. The following Table shows the operators in C++.
The following
operators are specific to C++.
:: .* ->*
The operators # and
## are used only by the preprocessor.
Operators are classified
as
·
Arithmetic
·
Assignment
·
Component
Selection
·
Conditional
·
Logical
·
Manipulator
·
Member
dereferencing
·
Memory
Management
·
Preprocessor
·
Relational
·
Scope
Resolution
·
Shift
·
Type
Cast
Based
on operand requirements, operators are also classified as unary, binary and
ternary operators.
- Unary operators require one operand
- Binary operator requires two operands
- Ternary operator requires three operands
4.1 Arithmetic
Operators
Arithmetic
Operators are used to perform mathematical operations. The list of arithmetic
operators are:
·
+
·
-
·
*
multiplication operator
·
/
division operator
·
%
modulus operator - gives the remainder of
·
integer divison
·
+=
, -=, *= , /= , %=
Arithmetic
expressions are formed using arithmetic operators, numerical
constants/variables, function call connected by arithmetic operators.
Examples :
·
a
= -5;
·
a
= +b;
·
a
/= 5; (a = a/5)
·
a++;
(Post increment operator . Equivalent to a = a+1)
·
a—
; (Post decrement operator. Equivalent to a = a-1)
·
++a;
(Pre increment operator. Equivalent to a = a+1)
·
—a
; (Pre decrement operator. Equivalent to a = a – 1)
·
a
*= 2 ( a = a * 2)
·
a
%= 5 ( a = a/5)
·
a
= b + pow(x,y) ( a = b + x y )
Operators
are executed in the order of precedence. The operands and the operators are
grouped in a specific logical way for evaluation. This logical grouping is
called as association. Table indicates the Mathematical Operators, its
Type, and Association.
Expression
|
Operation
|
Example
|
a++
|
Get the value of a,
then increment the value of the variable by 1
|
a =5;
c = a++
Execution
:
c = a;
a = a+ 1;
Hence the value
stored in the variable c is 5.
|
++a
|
Increment the value
of the variable a by 1, and then get the value
|
a = 5;
c = ++a;
Execution:
a=a+1
c = a;
Hence the value of
c will be 6
|
a--
|
Get the value of a,
then decrement it by 1
|
a = 5;
c = a--;
Execution
:
c = a;
a = a –1
What will be the
value of c?
|
--a
|
Decrement the value
of a by 1, then get the value of a
|
a = 5
c= --a;
Execution
:
a = a – 1
c = a;
What will be the
value of c?
|
4.2 Relational
Operators
Relational Operators
are used to compare values. The list of relational operators are:
·
=
= equal to
·
>
greater than
·
<
lesser than
·
>=,
<= greater than or equal to , lesser that or equal to
·
!=
not equal to
Relational operators
are used to compare numeric values. A relational expression is constructed
using any two operands connected by a relational operator. For example the
relational operators are used to construct conditions such as
·
10
> 20
·
500.45
<= 1005
·
99
!= 99.5
·
9
= = 9
The result of a
relational operation is returned as true or false. The numeric constant zero
(0) represents False value, and any nonzero constant represents true value. The
above expressions output will be
·
0
(10 > 20 is false ) ;
·
1
( 500.45 < 1005 is evaluated to True hence any non zero constant) ;
·
1
(99 != 99.5 will be evaluated to True hence non zero constant)
·
1
( 9 = = 9 will be evaluated to true, hence the output will be non zero
constant)
4.3 Logical Operators
(Boolean Operators)
Logical operators
combine the results of one or more conditions. The various logical operators
are && (AND) , || (OR) , ! (NOT)
Example : c = 5 , d =
6 , choice = ‘y’, term = ‘2’ (Assume True is indicated as 1 and False as 0)
Result_1 = (c = = d) &&
(choice != term)
Result_2 = ( ‘y’ = = ‘Y’) || (term !=
‘0’)
Result_3 = (c = = d) && ‘(y’ =
= ‘Y’ )|| choice != term
Result_4 = (c = = d) || (‘y’ = = ‘Y’)
&& choice ! = term
The values stored in
Result_1 is 0 (False), Result_2 is 1 (True), Result_3 is 1 (True) and Result_4
is 0 (False).
4.4 Conditional
Operator (?:)
(num1 > num2) ?
“true”:”else” - ?: Is a ternary operator – (num1>num2,”true”,”false” are the
operands. A ternary operator ( ?:) is also called as conditional operator. The
general syntax is E1 ? E2 : E3 where E1,E2,E3 are operands. E1 should
essentially be of scalar type, E2 and E3 are values or statements. For example to
assign the maximum value of the two values one can express it as:
max = (num1 >
num2) ? num1
: num2; The variable max will take the value of num1 if num1 is greater
than num2, otherwise max will be assigned with the value of num2.
4.5 Assignment Operators
= is the simple
assignment operator. It is used to assign the result of an expression (on the
right hand side) to the variable (on the left hand side of the operator). In
addition to the simple assignment operator, there are 10 ‘shorthand’ assignment
operators. Refer to the following Table for all assignment operators.
Expression
|
Working
|
Result
|
A=5
|
The value 5 is
assigned to the variable A.
|
The variable takes
the value 5.
|
A += 2
|
A+= 2
is interpreted as A = A +2
|
The value stored in
A is 7
|
A - = 2
|
A = A
– 2
|
The
value stored in A is 3
|
A *= 4
|
A = A * 4
|
The value stored in
A is 20
|
A /= 2
|
A = A / 2
|
The value stored in
A is 2
|
A %= 2
|
A = A % 2
|
The value stored in
A is 1
|
The following Table gives the complete
Operator precedence of all operators used in C++.
5. Punctuators
Punctuators are
characters with a specific function. Refer to the following Table for
Punctuators and their Purpose.
Punctuators
|
Purpose
|
;
|
Terminates a C++
statement
|
//
|
Treats statements
prefixed with this as comments
|
/* */
|
Blocks enclosed
within these characters are treated as comment
|
{ }
|
Used to group a set
of C++ statements. Coding for a function is also enclosed
within these
symbols
|
[ ]
|
Index value for an
element in an array is
indicated within
these brackets
|
‘ ’
|
Is used to enclose
a single cha
|
“ ”
|
Is used to enclose
a set of characters
|
ReplyDeletenice article for beginners.thank you.
javacodegeeks
welookups