0% found this document useful (0 votes)
85 views23 pages

1) Lec3 Operators PDF

Operators allow expressions to be formed from constants, variables, and functions in C. There are several types of operators including arithmetic, relational, logical, bitwise, assignment, and more. The order of evaluation for operators is based on precedence and associativity rules. For example, multiplication and division have higher precedence than addition and subtraction. Understanding operators is essential for properly constructing and evaluating expressions in C code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views23 pages

1) Lec3 Operators PDF

Operators allow expressions to be formed from constants, variables, and functions in C. There are several types of operators including arithmetic, relational, logical, bitwise, assignment, and more. The order of evaluation for operators is based on precedence and associativity rules. For example, multiplication and division have higher precedence than addition and subtraction. Understanding operators is essential for properly constructing and evaluating expressions in C code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CS1L001 : Programming and Data Structures

Operators in C

Joy Mukherjee

School of Electrical Sciences


Computer Science and Engineering
Indian Institute of Technology Bhubaneswar

1 / 23
Outline

1 Expressions

2 Operators

3 Typecasting

4 Type Promotion

2 / 23
Outline

1 Expressions

2 Operators

3 Typecasting

4 Type Promotion

3 / 23
Expressions

Operators, constants, functions, and variables are the constituents of


expressions. An expression in C is any valid combination of these elements.
A constant is an expression.
A defined variable is an expression.
If E is an expression, then so also is (E).
If E is an expression and op a unary operator defined in C, then op E
is again an expression.
If E1 and E2 are expressions and op is a binary operator defined in C,
then E1 op E2 is again an expression.
If V is a variable and E is an expression, then V = E is also an
expression.

4 / 23
Order of Evaluation

C does not specify the order in which the subexpressions of an


expression are evaluated.
This leaves the compiler free to rearrange an expression to produce
more optimal code.
x = f1() + f2();
does not ensure that f1( ) will be called before f2( ).

5 / 23
Outline

1 Expressions

2 Operators

3 Typecasting

4 Type Promotion

6 / 23
Types of Operators

Arithmetic + - * / % ++ - -
Relational < > <= >= == !=
Bitwise & |
Logical && || !
Ternary ?:
Assignment = += -= *= /= %=
Comma ,

7 / 23
Arithmetic Operators
C does not provide a built-in exponentiation operator.
Operator Meaning Description
- unary Applicable for integers and real numbers.
negation Makes no sense for unsigned operands.
+ binary ad- Integers and real numbers.
dition
- binary Integers and real numbers.
subtrac-
tion
* binary Integers and real numbers.
multipli-
cation
/ binary di- If both are integers division means quo-
vision tient, otherwise for real numbers division
means real division.
% binary re- Integer operands.
mainder
Table: Arithmetic Operators 8 / 23
Post-increment and Pre-increment Operators

int a = 3, b, c;

Operation Variable Values


b = a++; b = 3 and a = 4.
b = ++a; b = 5 and a = 5.
b = a- -; b = 5 and a = 4.
b = - -a; b = 3 and a = 3.
c = (++a) * (- -b); c = 4*2 = 8, b = 2 and a = 4.
c = (++a) * (b- -); c = 5*2 = 10, b = 1 and a = 5.
c = (a++) * (b- -); c = 5*1 = 5, b = 0 and a = 6.
Table: Post-increment and Pre-increment Operators

9 / 23
Bitwise Operators

Apply to unsigned integer operands and work on each individual bit.


Do not use them for signed integers.

Operator Meaning Example


& AND a = 237 11101 1 0 1
b = 174 10101 1 1 0
a & b is 172 10101 1 0 0
| OR a = 237 11101 1 0 1
b = 174 10101 1 1 0
a | b is 239 11101 1 1 1
Table: Bitwise Operators

10 / 23
Bitwise Operators

Operator Meaning Example


XOR a = 237 11101 1 0 1
b = 174 10101 1 1 0
a b is 67 01000 0 1 1
Complement a = 237 11101 1 0 1
a is 18 00010 0 1 0
Right-shift a = 237 11101 1 0 1
a 2 is 59 00111 0 1 1
Left-shift b = 174 10101 1 1 0
b 1 is 92 01011 1 0 0
Table: Bitwise Operators

11 / 23
Relational Operators

Relational: Relationships that values of expressions can have with one


another.
A relational condition evaluates to a Boolean value, i.e., either true
or false.

Usage Condition is true iff


E1 == E2 E1 and E2 evaluate to the same value
E1 ! = E2 E1 and E2 evaluate to different values
E1 < E2 E1 evaluates to a value less than E2
E1 <= E2 E1 evaluates to a value less than or equal to E2
E1 > E2 E1 evaluates to a value greater than E2
E1 >= E2 E1 evaluates to a value greater than or equal to E2
Table: Relational Operators

12 / 23
Relational Operators: Example
int x = 15, y = 40;
At that time, the following truth values hold:

x == y False
x! = y True
y %x == 10 True
600 < x y False
600 <= x y True
B > A True
x/0.3 == 50 False (due to floating point errors)
Table: Examples of Relational Operators

Note that the equality checker is == and not the single =. Recall that =
is the assignment operator. In a place where a relational condition is
expected, an assignment of the form E1 = E2 makes sense and could be a
potential source of bugs.
13 / 23
Logical Operators

Logical refers to the ways these relationships can be connected.


In C, true = any value other than zero. False = zero.
Expressions that use relational or logical operators return 0 for false
and 1 for true.
Logical operators are used to combine multiple relational conditions.
C , C1 and C2 are assumed to be relational conditions (including
expressions).

Meaning Syntax Condition is true iff


AND C1 &&C2 Both C1 and C2 are true
OR C1 ||C2 Either C1 or C2 or both are true
NOT !C C is false
Table: Logical Operators

14 / 23
Logical Operators: Example

(7 7 < 50)&&(50 < 8 8) True


(7 7 < 50)&&(8 8 < 50) False
(7 7 < 50)||(8 8 < 50) True
!(8 8 < 50) True
(A > B )||(a > b ) False
(A > B )||(A < B ) True
(A < B )&&!(a > b ) True
Table: Examples of Logical Operators

Note that here is yet another source of logical bug. Using a single & and |
in order to denote a logical operator actually means letting the program
perform a bitwise operation and possibly ending up in a logically incorrect
answer.

15 / 23
The ? Operator

C has a ternary operator that is equivalent to the if-then-else


statements.
Exp1 ? Exp2: Exp3;
where Exp1, Exp2, and Exp3 are expressions. Notice the use and
placement of the colon.
The ? operator works like this: Exp1 is evaluated. If it is true, Exp2 is
evaluated and becomes the value of the expression. If Exp1 is false,
Exp3 is evaluated, and its value becomes the value of the expression.

16 / 23
The Comma Operator

The comma operator combines together several expressions.


Essentially, the comma causes a sequence of operations to be
performed from left ti right.
When you use it on the right side of an assignment statement, the
value assigned is the value of the last expression of the
comma-separated list. For example,
x = (y=3, y+1);
first assigns y the value 3 and then assigns x the value 4.
The parentheses are necessary because the comma operator has a
lower precedence than the assignment operator.

17 / 23
Summary of Precedence and Associativity

Type Operators Associativity


() [] -> . L-to-R
Unary ! ++ + - * (type) sizeof & R-to-L
Arithmetic */% L-to-R
Arithmetic +- L-to-R
Bitwise L-to-R
Relationl < <= > >= L-to-R
Relationl == ! = L-to-R
Bitwise & L-to-R
Bitwise L-to-R
Bitwise | L-to-R
Logical && L-to-R
Logical || L-to-R
Ternary ?: R-to-L
Assignment = += = = /= %= &= = |= R-to-L
Comma , L-to-R

Table: Precedence and Associativity of Operators

18 / 23
Outline

1 Expressions

2 Operators

3 Typecasting

4 Type Promotion

19 / 23
Typecasting

If a variable that requires more storage in bytes, and you want to


assign it to a variable that requires more storage in bytes, C opts for
truncation.
In order to convert a value <val> of any type to a value of
<another type> use the following directive:
(<another type>)<val>
Here <val> may be a constant or a value stored in a named variable.

20 / 23
Typecasting: Example

double piTo4 = 97.4090910340.

Typecasting command Output value


(int)9.8696044011 The truncated integer 9
(int)-9.8696044011 The truncated integer -9
(float)9 The floating-point value 9.000000
(int)piTo4 The integer 97
(char)piTo4 The integer 97, or equivalently the character
a
(int *)piTo4 An integer pointer that points to the mem-
ory location 97.
(double)piTo4 The same value stored in piTo4
Table: Examples of Typecasting

Typecasting also applies to expressions and values returned by functions.


21 / 23
Outline

1 Expressions

2 Operators

3 Typecasting

4 Type Promotion

22 / 23
Type Promotion

When constants and variables of different types are mixed in an expression,


they are all converted to the same type. The compiler converts all
operands up to the type of the largest operand, which is called type
promotion.
1 Integral Promotion: char and short int int
Type Conversion Algorithm:
   long unsigned int
2

long double double float unsigned long


3 Special Case: If one operand is long and the other is unsigned int,
and if the value of the unsigned int cannot be represented by a long,
both operands are converted to unsigned long.

23 / 23

You might also like