< >= <=), logical (&& || !), assignment (= += -= etc.), increment/decrement (++ --), and bitwise operators (& | ^ << >>). Expressions are combinations of operators and operands that compute a value. Common examples show how operators work on integer and floating point operands."> < >= <=), logical (&& || !), assignment (= += -= etc.), increment/decrement (++ --), and bitwise operators (& | ^ << >>). Expressions are combinations of operators and operands that compute a value. Common examples show how operators work on integer and floating point operands.">
[go: up one dir, main page]

0% found this document useful (0 votes)
111 views26 pages

C Operators, Operands, Expressions & Statements

C provides various operators that take operands and produce results. Operators include arithmetic (+ - * / %), relational (== != > < >= <=), logical (&& || !), assignment (= += -= etc.), increment/decrement (++ --), and bitwise operators (& | ^ << >>). Expressions are combinations of operators and operands that compute a value. Common examples show how operators work on integer and floating point operands.

Uploaded by

cs.ramgopal7036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views26 pages

C Operators, Operands, Expressions & Statements

C provides various operators that take operands and produce results. Operators include arithmetic (+ - * / %), relational (== != > < >= <=), logical (&& || !), assignment (= += -= etc.), increment/decrement (++ --), and bitwise operators (& | ^ << >>). Expressions are combinations of operators and operands that compute a value. Common examples show how operators work on integer and floating point operands.

Uploaded by

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

C Operators, Operands,

Expressions & Statements


C OPERATORS, OPERANDS,
EXPRESSION & STATEMENTS
 Operators are symbols which take one or more operands or
expressions and perform arithmetic or logical computations. 

 Operands are variables or expressions which are used in


conjunction with operators to evaluate the expression.
 
 Expressions are sequences of operators, operands, and
punctuators that specify a computation.
Types of operator

• Arithmetic
• Relational
• Logical
• Assignment
• Increment and Decrement
• Conditional
• Bitwise
• Special
ARITHMETIC OPERATORS:
C provides all the basic arithmetic operators, they are +, -, *, /, %.
Integer division truncates (shorten) any fractional part.
The modulo division produces the remainder of an integer
division.
Ex: a+b
a–b
a*b
a/b
a%b
Here a and b are variables and are known as operands.
Note: Modulo (%) cannot be used for floating and double data
types.
Assume variable A holds 10 and variable B holds 20 then

Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from the first A – B will give -10

* Multiplies both operands A * B will give 200

/ Divides numerator by de-numerator B / A will give 2


Remainder of after an integer division
% B % A will give 0
Real Arithmetic / Floating Pont Arithmetic:
• Floating Point Arithmetic involves only real
operands of decimal or exponential notation.
If x, y & z are floats, then
• x = 6.0/7.0 = 0.857143
• y = -1.0/3.0 =- 0.333333
• z = 3.0/2.0 = 1.500000
• % cannot be used with real operands
Mixed mode Arithmetic:
• When one of the operands is real and the
other is integer the expression is a mixed
mode arithmetic expression.
• Example:
• 15/10.0 = 1.500000
• 15/10 = 1
• 10/15 = 0
• -10.0/15 = -0.666667
Relational Operators:
Assume variable A holds 10 and variable B holds 20, then:
Operator Description Example

== Checks if the values of two operands are equal or not, if yes (A == B) is not
true.
then condition becomes true.

!= Checks if the values of two operands are equal or not, if (A != B) is true.


values are not equal then condition becomes true.

> Checks if the value of left operand is greater than the value (A > B) is not
of right operand, if yes then condition becomes true. true.

< Checks if the value of left operand is less than the value of (A < B) is true.
right operand, if yes then condition becomes true.

>= Checks if the value of left operand is greater than or equal to (A >= B) is not
the value of right operand, if yes then condition becomes true.
true.

<= Checks if the value of left operand is less than or equal to the (A <= B) is true.
value of right operand, if yes then condition becomes true.
Logical Operators:
Assume variable A holds 1 and variable B holds 0, then

Operator Description Example


Called Logical AND operator. If both the operands are (A && B) is
&& nonzero, then condition becomes true. false.

Called Logical OR Operator. If any of the two


|| operands is non-zero, then condition becomes true. (A || B) is true.

Called Logical NOT Operator. Use to reverses the !(A && B) is


logical state of its operand. If a condition is true then true.
!
Logical NOT operator will make false.
Bitwise Operators
Bitwise operator works on bits and performs bit-by-bit operation. These operators can
operate upon int and char but not on float and double..Bit wise operators in C
language are; & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR), << (left
shift) and >> (right shift).

The truth tables for &, |, and ^ are as follows:

P Q p&q p|q p^q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1
Assume variable A holds 60 (00111100) and variable B holds 13 (00001101),
then: Operator Description Example

& Binary AND Operator copies a bit to the result if it (A & B) will give 12,
exists in both operands.
which is 0000 1100
| Binary OR Operator copies a bit if it exists in either (A | B) will give 61,
operand.
which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in (A ^ B) will give 49,
one operand but not both.
which is 0011 0001
~ Binary Ones Complement Operator is unary and has (~A ) will give -61, which
the effect of ‗flipping‘ bits.
is 1100 0011 in 2‘s
complement form.

<< Binary Left Shift Operator. The left operands value A << 2 will give 240
is moved left by the number of bits specified by the
which is 1111 0000
right operand.

>> Binary Right Shift Operator. The left operands A >> 2 will give 15
value is moved right by the number of bits specified
which is 0000 1111
by the right operand.
<< (left shift operator):
First operand<< second operand

Whose bits get left Decides number of


shifted place to shift the bits

1. When bits are shifted left then trailing positions are filled with zeroes.

2. Left shifting is equivalent to multiplication by 2rightoperand

Example:
3<<1
0000 0011<<1
0000 0110
=6 i.e. (3* 21)
>> (right shift operator):
First operand >> second operand

Whose bits get right Decides number of


shifted place to shift the bits

1. When bits are shifted left then leading positions are filled with zeroes.

2. Left shifting is equivalent to division by 2rightoperand

Example:
3>>1
0000 0011>>1
0000 0001
=1 i.e. (3/ 21)
Assignment Operators:
In C programs, values for the variables are assigned using assignment operators.
There are following assignment operators supported by C language:

Operator Description Example


= assignment operator, Assigns values from right side
operands to left side operand C = A + B will assign
value of A + B into C

+= It adds right operand to the


left operand and assign the result to left operand C += A is equivalent to
C = C+ A

-= Subtract AND assignment operator, It subtracts right


operand from the left operand and assign the result to left C -= A is equivalent to
Operand C = C– A

*= Multiply AND assignment operator, It multiplies right


operand with the left operand and assign the result to left C *= A is equivalent to
Operand C = C* A

/= Divide AND assignment operator, It divides left operand


ith the right operand and assign the result to left operand C /= A is equivalent to
C = C/ A

%= Modulus AND assignment operator, It takes modulus using


two operands and assign the result to left operand C %= A is equivalent to
C = C% A
Perform the following operation if A=60 and B=16
• A<<2
• A>>2
• A&B
• A|B
• A^B
Solution:
• A<<2 is 1111 0000
• A>>2 is 0000 1111
• A&B is 00010000
• A|B is 00111100
• A^B is 00101100
Increment/Decrement OPERATOR
In C, ++ and -- are called increment and decrement operators respectively. Both of
these operators are unary operators, i.e, used on single operand. ++ adds 1 to operand
and -- subtracts 1 to operand respectively.

++x or x ++ == > x+=1 == > x=x+1

The difference between pre-increment and post-increment lies in the


point at which the value of their operand is incremented.

1.In case of pre-increment operator, firstly the value of its operand is


incremented and then it is used for the evaluation of expression.
2.In case of post-increment operator, the value of operand is used first for
the evaluation of the expression and after its use, the value of the
operand is incremented.
3.Increment operator is a token i.e. one unit. There should be no white
space character between two ‘+’ symbols. If white space is placed
between two ‘+’ symbols, they become two unary plus (+) operators.
Conditional Operators (? :)
Conditional operators are used in decision making in C
programming, i.e., executes different statements according to
Test condition whether it is either true or false.
Syntax of conditional operators
conditional_expression ? expression1: expression2
If the test condition is true (that is, if its value is non-zero),
expression1 is returned and if false expression2 is returned.
y = ( x> 5 ? 3 : 4 ) ;
this statement will store 3 in y if x is greater than 5, otherwise it
will store 4 in y.
Misc Operators:
There are few other operators supported by c language.

Operator Description Example

sizeof( ) It is a unary operator which is used in sizeof(a), where a is integer, will


finding the size of data type, return 4.
constant, arrays, structure etc.
& Returns the address of a &a; will give actual address of
variable. the variable.

* Pointer to a variable. *a; will pointer to a variable.


Operators Precedence in C
• Operator precedence determines the grouping
of terms in an expression. This affects how an
expression is evaluated. Certain operators have
higher precedence than others; for example, the
multiplication operator has higher precedence
than the addition operator.
• For example x = 7 + 3 * 2;

here, x is assigned 13, not 20 because operator * has higher precedence than
+, so it first gets multiplied with 3*2 and then adds into 7.
operators with the highest precedence appear at the top of the table, those with
the lowest appear at the bottom. Within an expression, higher precedence
operators will be evaluated first
Sr.No. Operator Name of Operator Associativity Rank
1. () L→R 1
Function call
[]
Array subscript
2. R→L 2
+ Unary plus

- Unary minus
++ Increment
--
Decrement
&
Address-of
*
Deference

sizeof (type) sizeof

Type cast (conversion)


3. * L→R 3
Multiplication Division
/
% Modulus
4. + Addition L→R 4
Subtraction
-
5. << Left Shift L→R 5
>> Right Shift
6. < L→R 6
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to

7. == Equal to L→R 7
!= Not equal to
8. & Bitwise AND L→R 8

9. ^ Bitwise X-OR L→R 9

10. | Bitwise OR L→R 10

11. && Logical AND L→R 11

12. || Logical OR L→R 12


13. ?: Conditional operator R→L 13
14. = R→L 14
Simple assignment
*=
Assign product Assign

/= quotient Assign
%=
modulus Assign sum
+=
Assign difference
-=
Assign bitwise AND
&=
|= Assign bitwise OR
^=
Assign bitwise XOR
<<=
>>= Assign left shift
Assign right shift
15. , Comma operator L→R 15
Example:

x=(20 || 40 ) && (10) Answer: 1

#include <stdio.h>
void main()
{
int a=3,b=2;
Answer: 1,2
a=a==b==0;
printf("%d,%d",a,b); }

100 + 200 / 10 - 3 * 10 Answer: 90


Type casting
 Type casting /type conversion is a way to convert a variable from one data
type to another data type. For example, if you want to store a long value
into a simple integer then you can typecast long to int.

 You can convert values from one type to another explicitly using the cast
operator. There are two types of type casting in c languages that are
implicit conversions and Explicit Conversions.
 New data type should be mentioned before the variable name or value in
brackets which to be typecast.

There are two types of type casting in c language.

• Implicit Conversion

• Explicit Conversion
1. IMPLICIT CONVERSION
Implicit conversions do not require any operator for converted. They are
automatically performed when a value is copied to a compatible type in
the program.
#include<stdio.h> #include<conio.h>
void main()
{
int i=20;
double p;
clrscr();
p=i; // implicit conversion
printf(“implicit value is %d”, p);
getch();
}

Output:-

implicit value is 20.


2. EXPLICIT CONVERSION
In C language, Many conversions, especially those that imply a different
interpretation of the value, require an explicit conversion.
Example :-
#include<stdio.h>
#include<conio.h>
void main()

{
int i=20;
short p;
clrscr();
p = (short) i; // Explicit conversion
printf(“Explicit value is %”, p);
getch();

Output:- Explicit value is 20.

You might also like