Operators and Expression – An expression consists of variables and constants separated by operators. C language uses many type of operators as listed below:
1.
Arithmetic
Operators
2.
Relational
Operators
3.
Logical
Operators
4.
Increment
and Decrement Operators
5.
Assignment
Operators
6.
Conditional
Operators
7.
Bitwise
Operators
Arithmetic
Operators  The basic arithmetic operators in C
are the same as in most other computer languages, and correspond to our usual
mathematical/algebraic symbolism. The following arithmetic operators are
present in C:
Operators

Meaning

Example

Result

+

Addition

4+2

6



Subtraction

42

2

*

Multiplication

4*2

8

/

Division

4/2

2

%

Modulus
operator to get remainder in integer division

5%2

1

Some
of the examples of algebraic expressions and their C notation are given below:
Expression

C
notation

b*g / b

(b*g) / b

a^3+cd

(a*a*a)+cd

Parentheses
can be used in C expression in the same manner as algebraic expression For
example,
a * (b + c).
The operands in arithmetic expressions can be of integer, float, double type. In order to effectively develop C programs, it will be necessary for you to understand the rules that are used for implicit conversation of floating point and integer values in C.
They
are mentioned below:
•
An
arithmetic operator between an integer and integer always yields an integer
result.
•
Operator
between float and float yields a float result.
•
Operator
between integer and float yields a float result.
•
If
the data type is double instead of float, then we get a result of double data
type.
For
example:
Operation

Result

5/3

1

5.0/3

1.3

5/3.0

1.3

5.0/3.0

1.3

The rules of
arithmetic precedence are as follows:
1.
Parentheses
are at the “highest level of precedence”. In case of nested parenthesis, the
innermost parentheses are evaluated first.
For example:
( ((3+4)*5)/6 )
The order of
evaluation is given below.
( ( (3+4) * 5) /
6 )
↓ ↓ ↓
1 2 3
↓ ↓ ↓
1 2 3
2.
Multiplication,
Division and Modulus operators are evaluated next. If an expression contains
several multiplication, division and modulus operators, evaluation proceeds
from left to right. These three are at the same level of precedence.
For example:
5*5+6*7
The order of
evaluation is given below.
5*5+6*7
↓ ↓ ↓
1 3 2
↓ ↓ ↓
1 3 2
3.
Addition,
subtraction are evaluated last. If an expression contains several addition and
subtraction operators, evaluation proceeds from left to right. Or the associativity
is from left to right.
For example:
8/56+5/2
The order of
evaluation is given below.
8/56+5/2
↓
↓ ↓ ↓
1
3 4 2
Library Function
(Intrinsic Functions or Math Functions) – To perform standard mathematical
operations such as square root, absolute value and so on, builtin programs
called library functions are available with the compiler. These functions can
be used in an expression by mentioning their names with required number of
arguments in parenthesis. Following are the commonly used mathematical library
functions:
(Click image for large view)
Note
– The argument x used in this function is declared float or double and the
complier directive #include<math.h>
is used to include any of these functions in a C program.
Unary Plus ( + )
or Unary Minus ( – ) operations – When only one operand is used with +
or – operators, the operation is called unary plus or unary minus, which does
not actually refer to addition or subtraction.
Relational
Operators
– Relational operators are used to compare the value of operands (expressions)
to produce a logical value. A logical value is either true or false. Following
are the relational operators used in C language:
Operators

Meaning

Example

Result

<

Less
than

5>2

False

>

Greater
than

5>2

True

<=

Less
than or equal to

5<=2

False

>=

Greater
than or equal to

5>=2

True

==

Equal
to

5==2

False

!=

Not
equal to

5!=2

True

Note
– In C language the logical value true is represented by integer 1 and false by
0 (zero).
Logical
Operators – Logical operators are used to connect
more relational operations to form a complex expression called logical
expression. A value obtained by evaluating a logical expression is always
logical, i.e. either true or false. Following are the logical operators in C:
Operator

Meaning

Example

Result

&&

Logical
AND

(5<2)&&(5>3)

False



Logical
OR

(5<2)(5>3)

True

!

Logical
NOT

!(5<2)

True

Note
– Logical not ( ! ) is an unary operator which requires only one operand. It is
also referred as invertor which converts the value of the operand from true to
false and vice versa.
The following
truth tables to understand the logical values obtained using the logical
operators:
&&


Operand 1

Operand 2

Result

True

True

True

True

False

False

False

True

False

False

False

False




Operand 1

Operand 2

Result

True

True

True

True

False

True

False

True

True

False

False

False

!


Operand

Result

False

True

True

False

Increment
( ++) and Decrement (  ) Operators  The increment
operator increments the variable by one and decrement operator decrements the
variable by one. These operators can be written in two forms i.e. before a
variable or after a variable. If an increment / decrement operator is written
before a variable, it is referred to as preincrement / predecrement operators
and if it is written after a variable, it is referred to as postincrement /
postdecrement operator.
For
example:
a++ or ++a is equivalent to a = a+1 and
a or  a is equivalent to a = a 1
The
importance of pre and post operator occurs while they are used in the
expressions. Preincrementing (Predecrementing) a variable causes the variable
to be incremented (decremented) by 1, then the new value of the variable is
used in the expression in which it appears. Postincrementing (postdecrementing)
the variable causes the current value of the variable are used in the
expression in which it appears, then the variable value is incremented
(decrement) by 1.
The
explanation is given in the table below:
Operators

Meaning

++a

Increment
a by 1, then use the new value of a

a++

Use
value of a, then increment a by 1

b

Decrement
b by 1, then use the new value of b

b

Use
the current value of b, then decrement by 1

The precedence of these operators is right to left.
Let us consider the following examples:
int a = 2, b=3;
int c;
c = ++a – b ;
printf (“a=%d,
b=%d,c=%d\n”,a,b,c);
OUTPUT
a = 3, b = 2, c = 0.
Since the precedence of the operators is right to
left, first b is evaluated, since it is a post decrement operator, current
value of b will be used in the expression i.e. 3 and then b will be decremented
by 1.Then, a preincrement operator is used with a, so first a is incremented
to 3. Therefore, the value of the expression is evaluated to 0.
Let us take another example,
int a = 1, b = 2, c =
3;
int k;
k = (a++)*(++b) + ++a 
c;
printf(“a=%d,b=%d,
c=%d, k=%d”,a,b,c,k);
OUTPUT
a = 3, b = 3, c = 2, k
= 6
The evaluation is explained below:
k = (a++) * (++b)+ ++a
 c
= (a++) * (3) + 2  2 step1
= (2) * (3) + 2 – 2 step2
= 6 final
result
Assignment
Operator (C Shorthand) – Assignment operators are used
to perform arithmetic operations while assigning a value to a variable.
Operator

Example

Equivalent
Expression (m=15)

Result

+=

m +=10

m = m+10

25

=

m =10

m = m10

5

*=

m *=10

m = m*10

150

/=

m /=

m = m/10

1

%=

m %=10

m = m%10

5

Conditional
Operator ( ? : ) or Ternary Operators  C provides an called as the
conditional operator (?:) which is closely related to the if/else structure.
The conditional operator is C’s only ternary operator  it takes three
operands. The operands together with the conditional operator form a
conditional expression. The first operand is a condition, the second operand
represents the value of the entire conditional expression it is the condition
is true and the third operand is the value for the entire conditional
expression if the condition is false.
The
syntax is as follows:
(condition)?
(expression1): (expression2);
If
condition is true, expression1 is evaluated else expression2 is evaluated.
Expression1/Expression2 can also be further conditional expression i.e. the
case of nested if statement (will be discussed in the next unit).
Let
us see the following examples:
(i) x= (y<20) ? 9: 10;
This means, if (y<20), then x=9 else
x=10;
(ii) printf (“%s\n”, grade>=50?
“Passed”: “failed”);
The above statement will print “passed”
grade>=50 else it will print “failed”
(iii) (a>b) ? printf (“a is greater
than b \n”): printf (“b is greater than a \n”);
If a is greater than b, then first
printf statement is executed else second printf statement is executed.
Bitwise
Operators
– Bitwise operators are used to perform operations at binary digit level. These
operators are not commonly used and are used only in special applications where
optimized use of storage is required.
Operator

Meaning

<<

Shifts
the bits to left

>>

Shifts
the bits to right

~

Bitwise
inversion (one’s complement)

&

Bitwise
logical end



Bitwise
logical or

^

Bitwise
exclusive or

Additional
Operators – There two other operators that can be
used in a C Program:
1.
Sizeof
operator
2.
Comma
operator
Sizeof
operator  C provides a compiletime unary
operator called sizeof that can be used to compute the size of any object. The
expressions such as:
sizeof
object and sizeof(type name)
Result
in an unsigned integer value equal to the size of the specified object or type
in bytes. Actually the resultant integer is the number of bytes required to
store an object of the type of its operand. An object can be a variable or
array or structure. An array and structure are data structures provided in C,
introduced in latter units. A type name can be the name of any basic type like int or double or a derived type like a structure or a pointer.
Example:
sizeof(char) =
1bytes
sizeof(int) = 2
bytes
Comma
Operator  A comma operator is used to separate a
pair of expressions. A pair of expressions separated by a comma is evaluated
left to right, and the type and value of the result are the value of the type
and value of the right operand. All side effects from the evaluation of the
left operand are completed before beginning evaluation of the right operand.
The left side of comma operator is always evaluated to void. This means that
the expression on the right hand side becomes the value of the total
commaseparated expression.
Example:
x = (y=2, y 
1);
First
assigns y the value 2 and then x the value 1. Parenthesis is necessary since
comma operator has lower precedence than assignment operator.
Generally,
comma operator (,) is used in the for loop (will be introduced in the next
unit)
For
example:
for (i = 0,j =
n;i<j; i++,j)
{
printf (“A”);
}
In
this example for is the looping construct. In this loop, i = 0 and j = n are
separated by comma (,) and i++ and j—are separated by comma (,). The example
will be clear to you once you have learnt for loop.
Like it ? Share it.
Post a Comment
Comment Rules :
1. Do not post Adult/illegal Links.
2. Try to comment in only English Language.
3. Do not post other website's links which are useless.
4. Your Comment should be based on the Topic for other queries Kindly Visit our Contact Us Page.
5. Do not use Abusive Language.
6. Respect each other.
Thank You for following the rules. Please Comment....