Download Operator - CSC Technologies

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Laws of Form wikipedia , lookup

Bra–ket notation wikipedia , lookup

Tensor operator wikipedia , lookup

?: wikipedia , lookup

Transcript
‘C’-Language
1
Operators
Chapter-2
Operators
1. Arithmetic operators
2. Relational operator
3. Logical operators
4. Assignment operator
5. Increment & decrement operators
6. Conditional operator
7. Bitwise operators
8. Special operators
‘C’-Language
2
Operators
Def: An operator is a symbol that tells the computer to perform certain mathematical or
logical manipulations. Operators are used in programs to manipulate data and variables. They
usually form a part of the mathematical of logical expressions.
C operators can be classified into a number of categories. They include:
1)
2)
3)
4)
5)
6)
7)
8)
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators.
Bitwise operators
Special operators
1.Arithimetic Operators:
There are five arithmetic operators in C. They are
Operator
+
*
/
%
Meaning
Addition
subtraction
Multiplication
Division
Remainder after integer division
The % operator is sometimes referred to as the modulus operator. The remainder
operator (%) requires that both operands be integers and the second operand be nonzero.
Similarly, the division operator (/) requires that the second operand be nonzero
Division of one integer quantity by another is referred to as integer division. This
operation always results in a truncated quotient i.e. the decimal portion of the quotient will be
dropped. On the other hand, if a division operation is carried out with tow floating-point
numbers, or with one floating-point number and one integer, the result will be a floating-point
quotient.
Ex: Suppose that ‘a’ and ‘b’ are integer variables whose values are 10 and 3, respectively.
Several arithmetic expressions involving these variables are shown below, together with their
resulting values.
Expression
a+b
a-b
a*b
a/b
a%b
value
13
7
30
3
1
Ex: Now suppose that ‘v1’ and ‘V2’ are floating-point variables whose values are 12.5 and 2.0
respectively. Several arithmetic expressions involving these variables are shown below,
together with their resulting values.
‘C’-Language
3
Expression
V1+v2
V1-v2
V1*v2
V1/v2
Operators
Value
14.5
10.5
25.0
6.25
Def: Combination of some operands, constants with arithmetic operators is called an arithmetic
expression.
Ex:
1) a+b*2/c+d*5
2) x/y*x*2/y*3
The following is the hierarchy of arithmetic operators.
1.( )
----- parenthesis
2.* /
3.+
%
-
Ex: Determine the hierarchy of operations and evaluate the following expression:
I=2*3/4+4/4+8-2+5/8
I=6/4+4/4+8-2+5/8
I=1+4/4+8-2+5/8
I=1+1+8-2+5/8
I=1+1+8-2+0
I=2+8-2+0
I=10-2+0
I=8+0
I=8
2) Relational Operators:
We often compare tow quantities, and depending on their relation, take certain
decisions. For example, we may compare the age of two persons, or the price of two items,
and so on. These comparisons can be done with the help of relational operators.
The value of a relational expression is either true or false. For example 10 <20 true, but
20<10 false.
Operator
<
<=
>
>=
==
!=
Meaning
is less than
is less than or equal to
is greater than
is greater than or equal to
is equal to
is not equal to
A simple relational expression contains only one relational operator and takes the
following form:
ae-1 relational operator ae-2
‘C’-Language
4
Operators
Ae-1 and ae-2 are arithmetic expressions, which may be simple constants, variables or
combination of them.
Ex: 1. 4.5 <= 10
2. 4.5 < -10
3. a+b == c+d
true
false
true
4) Logical Operators:
In addition to the relational operators, C has the following three logical operators.
Operator Meaning
&&
logical AND
||
logical OR
!
logical NOT
The logical operators && and || are used when we want to test more than one condition
and make decisions.
Ex: a>b && x==10
An expression of this kind which combines tow or more relational expression s is termed
as a logical expression or a compound relational expression. Like the simple relational
expressions, a logical expression also yields a value of true or false.
Truth Tables:
&&
Exp1
T
T
F
F
Exp2
T
F
T
F
||
Result
T
F
F
F
Exp1
T
T
F
F
Exp2
T
F
T
F
Result
T
T
T
F
!
Expression
T
F
Result
F
T
Ex:
1.(age>55 && salary<1000)
2.(number<0 || number>100)
4) Assignment Operators:( =)
Assignment operators are used to assign the result of an expression to variable. The
most commonly used assignment operator is =.
Syn:
Variable = expression;
Where identifier generally represents a variable, and expression represents a constant , a
variable or a more complex expression.
‘C’-Language
Ex:
1)
2)
3)
4)
5)
5
Operators
a=3;
x=y;
delta=0.001;
sum=a+b;
area = length * width
The right side expression to the assignment operator is evaluated first, and that
obtained resultant is stored into the left side variable to that assignment operator.
Compound Assignment:
Consider the following assignment statement
x=x+a;
C provides a compact and short-hand form of indicating this operation by compound
assignment operator ‘+=’. Thus x=x+a; can be represented by x+=a.
The general form of compound assignment is
First-operand compound-assignment-operator
second-operand;
The forms of compound assignment operators and their corresponding expanded
assignment statements are given below.
Operator
+=
-=
*=
/=
%=
example
x+=a
x-=a
x*=a
x/=a
x%=a
equivalent assignment statement
x=x+a
x=x-a
x=x*a
x=x/a
x=x%a
5) Increment and Decrement operators:
These are very useful operators, generally not found in other programming languages,
they are ++ and --.
The ++ operator increments the value contained in a variable by one. The general form
of this operation is
Syn:
Variable++; ------------------>post increment operator
Variable--; ------------------>pre increment operator
Both are equivalent to
Variable =Variable+1
It is important to distinguish between pre-increment and post-increment operations. The
pre-increment operator, first increment and do the operation. On the other hand in the post
increment operator first do the operation and then increment.
i.e.
a=++b; is equivalent to b=b+1
a=b;
a=b++; is equivalent to a=b;
b=b+1;
‘C’-Language
6
Operators
Ex:
b=5;
a=++b; the result will be b=6
a=6
a=b++; the result will be a=5
b=6
In a similar way, C uses ‘- -‘as a decrement operator.
The format of – is
Variable-- ; --------------- post decrement
--Variable
--------------- pre decrement
Both are equivalent to
Variable =variable – 1;
Ex:
b=5;
a=--b; the result will be b=4
a=4
a=b--; the result will be a=5
b=4
6) Conditional Operator (?:):
This is also known as ternary operator. It is called ternary operator because it uses three
expressions. It is a short form of if-then-else statement.
The general from is:
Variable = Expression1? Expression1: Expression2;
If the expression1 being evaluated is true, expression2 will be performed. If the
expression is false, expression3 will be performed.
The following example gives the maximum of two numbers x andy.
Ex:
Int x,y,max;
x=10;
y=5;
max=(x>y) ? x : y;
Every thing between the = and; is the conditional expression. The meaning of the
statement is if x is greater than y, then maximum = x; otherwise maximum=y.
‘C’-Language
7
Operators
7) Bitwise Operators:
One byte is made up of 8 bits. A bit stores either 0 or 1. The C language provides
facilities similar to assembly language to manipulate at the bit level of integers in addition to
operations at the byte level.
The operators provided for bit manipulation are:
Operator Meaning
&
bitwise AND
|
bitwise OR
^
bitwise exclusive OR
~
bitwise one’s complement
operator.
>>
Right shift operator
<<
Left shift operator
These can operate on integers only and not on floating point types.
The bitwise AND operator (&) returns 1 if both the operands are 1 and 0 otherwise.
The bitwise OR operator(|) returns 1 if at least one of the operand is 1 and 0 when both
are 0.
The bitwise XOR(^) operator returns 0 when both the operands are same and 1 when
they are different.
The bitwise one’s complement operator (~) inverts each bit.
converts each 1 bit to 0 and each 0 bit to a 1.
In other words, it
Suppose the variable a and b have the values 7 and 12 respectively.
representation of these variables are
A
----00000111
B
----00001100
A&B
----00000100
A|B
----00001111
A^B
----00001011
~A
----11111000
~B
----11110011
The binary
‘C’-Language
8
Operators
8) Special Operators:
There are special operators used in the C language to perform some particular type of
operations. The following are the special operators in C.
Operator
&
operation
Address
action performed
returns address of operand
*
indirection
returns contents of location
whose address is the operand
sizeof
,
comma
returns size in bytes of the operand
to separate elements or
expressions in alist
.
dot
Member of structure or union
-
pointer to a member of a structure or union.
Type Casting:
It is possible to force an expression to be o0f specific type by using a construct called
cast. The operator used to force this conversion is called cast and the process is known as
casting. The general form is
(Type-desired) expression;
Ex:
1.
a=1
c=3.1415.
b=(int ) c ; here b=3
d=(float) a/(float)b = 0.333
Ex:
2.
char ch;
Int x;
X=(int) ch;
Ex
3.
(double)(4*3/7)