Showing posts with label operators in c. Show all posts
Showing posts with label operators in c. Show all posts

Wednesday, May 14, 2008

Introduction (operators in c)

Introduction

An operator specifies an operation to be performed. C is rich in operator. Operators join the various variables and constants to from an expression.

Some operator requires one operand and some require more than one operands.

An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation in data stored in variables.

C is extremely rich in operators It has as many as 45 different operators.

Arithmetic operators

Arithmetic operators

Arithmetic Operators are used to Arithmetical calculation.

There are five Arithmetic operators in C:

Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after integer division

Relational operators

Relational operators

Relational operators are used to compare two operands and to check whether they are equal, unequal, greater than and lesser than other.

There are 6 relational operators:

Operator Meaning
< Less than
> Greater than
<= Less than equal to
>= Greater than equal to
== Equal to
!= Not equal to


The value of the relational operator is either one or zero. If the relation is true, result is 1 otherwise it is 0.

Logical operators

Logical operators

Logical operators are used to combine two or more relational expressions.

There are three logical operators:

Operator Meaning
&& Logical And
|| Logical or
! Logical not


The expression which combines two or more relational expressions is termed as logical expression or compound relational expression.

The result of a logical expression is either one or zero.

Example:

a) if (age > 50 && weight < 80)
b) if (a < 0 || ch = = 'a')
c) if ( ! (a < 0))

Increment & Decrement operators

Increment & Decrement operators

These types of operators operate on only one operand, therefore these operators are also called Unary operators.

These two powerful operators in C are + + (Increment), _ _ (Decrement). Operands must be declared as variables not a constant.

These operators may be used either after or before the operand.
When they are used before the operand, it is termed as Prefix while when they are used after the operand they are termed as Postfix.

In prefix operations the value of operator is incremented or decremented first and then the expression is evaluated. Prefix operators has the effect of Change then use.

In postfix operation the expression is evaluated first and then the value of operator is either incremented or decremented. Postfix operators has the effect of Use Then Change.

e.g.: b=a++; this is postfix increment expression. In the expression firstly b=1; then a=a+1; will be executed ,

while in prefix increment expression
b=--a;

firstly a =a-1;then b=a; will be executed.

An example program clarifies the Postfix and Prefix operators:

Bitwise operators

Bitwise operators

The smallest element in memory on which we are able to operate as yield is a byte; and we operate on it by use of the data type char Bitwise operator is used for manipulation of data at bit level.

These operators are used for testing the bits, shifting them right to left. Bitwise operator may not be applied to float or double data type.

This is a powerful feature of C to manipulate a bit. The programmer can access and manipulate individual bits within a piece of data.



Some of the bitwise operators in C are:

Operator Meaning
& Bitwise Logical AND
| Bitwise Logical OR
^ Bitwise Logical XOR
<< Left Shift
>> Right Shift
~ Once Compliment

Saturday, May 10, 2008

Assignment operator

Assignment operator

Assignment operators are used to assign the result of
an expression to a variable. The most commonly used
assignment operator is (=).

eg: i=i+10;

i=i+10 is an assignment expression
which assigns the value of i+10 to i.

Expression like i=i+10, i=i-5, i=i*2 etc. can be
rewritten using shorthand assignment operators.

e.g.: i=i+5 is equivalent to i+=5
i=i*(y+1) is equivalent to i*=(y+1)

Operator Precedence:

While executing an arithmetic statement which has two
or more operators, we may have some problems about how
exactly does it get executed.

To answer these questions satisfactorily we have to
understand the precedence of operators.

Precedence defines the sequence in which operators
are to be applied on the operands. Operators of same
precedence are evaluated from left to right or right to
left, depending upon the level.

This is known as associativity property of an operator.

Summary of precedence of associativity is given below:

Description Operator Associativity
Function Expression ( ) Left to Right
Array Expression [ ] Left to Right
Structure Operator -> Left to Right
Structure Operator . Left to Right


Description Operator Associativity
Unary minus - Right to Left
Increment/Decrement ++/-- Right to Left
One's Compliment ~ Right to Left
Negation ! Right to Left
Address of & Right to Left
Value at address * Right to Left
Type cast (type) Right to Left
Size in bytes sizeof Right to Left


Description Operator Associativity
Multiplication * Left to Right
Division / Left to Right
Modulus % Left to Right
Addition + Left to Right
Subtraction - Left to Right


Description Operator Associativity
Left Shift << Left to Right
Right Shift >> Left to Right


Description Operator Associativity
Less Than < Left to Right
Less Than Equal to <= Left to Right
Greater than > Left to Right
Greater than Equal to >= Left to Right


Description Operator Associavity
Equal to == Left to Right
Not equal to != Left to Right


Description Operator Associavity
Bitwise AND & Left to Right
Bitwise XOR ^ Left to Right


Description Operator Associavity
Bitwise OR ^ Left to Right


Description Operator Associavity
Logical AND && Left to Right
Logical OR || Left to Right


Description Operator Associavity
Conditional ?: Right to Left


Description Operator Associavity
Assignment = Right to Left
Assignment *= /= %= Right to Left
Assignment += -= &= Right to Left
Assignment ^= |= Right to Left
Assignment <<= >>= Right to Left


Description Operator Associavity
Comma , Right to Left





Copyright

Copyright © 2008 C Tutorial, All rights are reserved