1 of 21

Expressions and Operators in C

2 of 21

Expressions in C

  • We use math formulae all the time in physics, chem, math
    • a = b / 5
    • x = y * y + z * z
    • x = (int)(pow((double)y, 2.0) + pow((double)z, 2.0))

  • these formulae expressions
    • x = y * y + z * z is an expression for C
    • y * y + z * z is also an expression for C
    • y * y is also an expression for C
    • z * z is also an expression for C

2

3 of 21

Expressions and Operators

  • Expressions in C consist of one or more variables/constants
  • An expression contains one or more operators, such as

c = a + b - 2;

  • Operators in C can be one of the following type
    • Arithmetic
    • Unary
    • Relational and logical
    • Assignment
    • Conditional

3

Yes.

But I will tell you some other interesting things about them and other operators

I think I have already seen/used Arithmetic and Assignment operators in previous lectures/labs!

4 of 21

Arithmetic operators

  • Already seen. Operate on int, float, double (and char)

4

Op

Meaning

Example

Remarks

+

Addition

9+2 is 11

9.1+2.0 is 11.1

-

Subtraction

9-2 is 7

9.1-2.0 is 7.1

*

Multiplication

9*2 is 18

9.1*2.0 is 18.2

/

Division

9/2 is 4

Integer division

9.1/2.0 is 4.55

Real division

%

Remainder

9%2 is 1

Only for int

5 of 21

Unary operators

5

Operator

-

Negative of an expression

++/--

Increment/decrement a variable

sizeof

Output memory box size for a variable

type (examples: int, float, double, etc)

Type-casting

6 of 21

Unary Operators - Negative

  • Operators that take only one argument (or operand)
    • -5
    • -b
  • Observe that – is both an arithmetic and unary operator
    • Meaning depends on context
    • This is called overloading

6

7 of 21

Unary operators – increment and decrement

  • Increment (++) increases a variable by 1
  • Decrement (--) decreases a variable by 1
  • ++variable is the pre-increment operator
      • Means increment, then use
  • variable++ is the post-increment operator
      • Means use, then increment
  • Likewise, the -- can be pre/post decrement

7

int main(){

char a = ‘A’; float b = 3.31;

printf("%c\t%f\n",++a,b++);

printf("%c\t%f",--a,b--);

return 0;

}

B 3.31

A 4.31

Work with all data types

Note: The variable’s value will change for the rest of the program

8 of 21

Unary operators - sizeof

  • Syntax
    • sizeof var
    • sizeof(type)
  • Returns size of the operand in bytes
    • sizeof(char) will return 1
    • sizeof(float) will (mostly) return 4
  • Very useful when you are porting programs across computers

8

9 of 21

Unary operators - typecast

  • Syntax
    • (type) var, for example – (int) a, (float) a, etc
  • We have already seen this
  • What will be the output of this program?

9

int main(){

double a = 67.2;

printf("size is %d\n", sizeof a);

printf("size is %d\n", sizeof((char) a));

printf("%c", (char) a);

return 0;

}

Size is 8

Size is 1

C

10 of 21

Precedence Rules for Unary Operators

  • Precedence rules tell us the order in which the operators will be applied in any C expression
  • Unary ops are above arithmetic ops, only below brackets
  • If a is 1 and b is 2, what will a + -b be evaluated as?

  • What about this program?

10

int main(){

int a = 1; int b = 2;

printf("%d", a + - + - b);

return 0;

}

3

-1

Bracket has the highest precedence

Note +x is x

11 of 21

Associativity Rules for Unary Operators

  •  Associativity rules tell us how the operators of same precedence are grouped (e.g., a+b+c will be evaluated as (a+b)+c, not a+(b+c))
  • For unary operators, the associativity is from right to left
    • Important to remember this
    • Most other operators’ associativity is left to right (e.g., + operator)
  • What will this program print?

11

int main(){

int a = 1;

printf("%d", - ++a);

return 0;

}

-2

12 of 21

Relational Operators

  • Compare two quantities

  • Work on int, char, float, double

12

Operator

Function

>

Strictly greater than

>=

Greater than or equal to

<

Strictly less than

<=

Less than or equal to

==

Equal to

!=

Not equal to

Result is 0 or 1

1 means condition true, 0 means false

13 of 21

Relational Operators: Some Examples

13

Rel. Expr.

Result

Remark

3>2

1

3>3

0

‘z’ > ‘a’

1

ASCII values used for char

2 == 3

0

‘A’ <= 65

1

'A' has ASCII value 65

‘A’ == ‘a’

0

Different ASCII values

(‘a’ – 32) == ‘A’

1

5 != 10

1

1.0 == 1

AVOID

May give unexpected result due to approximation

Avoid mixing int and float values while comparing. Comparison with floats is not exact!

14 of 21

Assignment Operator

  • Basic assignment (variable = expression)

14

Variant

Meaning

Var += a

Var = Var + a

Var -= a

Var = Var – a

Var *=a

Var = Var *a

Var /=a

Var = Var/a

Var %=a

Var = Var%a

15 of 21

Precedence of Assign Operators

  • Always the last to be evaluated
    • x *= -2 *(y+z)/3
    • x = x*(-2*(y+z)/3)
  • Seldom need to worry about it

15

16 of 21

Operator Precedence

16

Example: a + b – c * d % e /f

Operators

Description

Associativity

(unary) + -

Unary plus/minus

Right to left

* / %

Multiply, divide, remainder

Left to right

+ -

Add, subtract

Left to right

< > >= <=

less, greater comparison

Left to right

== !=

Equal, not equal

Left to right

=

Assignment

Right to left

LOW

HIGH

(a+b) - (((c *d ) % e) / f)

Earlier the ASCII table. Now this table? Have to memorize this??

No.

Write it in your notebook

17 of 21

Logical Operators

17

Logical Op

Function

Allowed Types

&&

Logical AND

char, int, float, double

||

Logical OR

char, int, float, double

!

Logical NOT

char, int, float, double

  • Remember
  • value 0 represents false.
  • any other value represents true. Compiler returns 1 by default

18 of 21

Logical Operators: Truth Table

18

E1

E2

E1 && E2

E1 || E2

0

0

0

0

0

Non-0

0

1

Non-0

0

0

1

Non-0

Non-0

1

1

E

!E

0

1

Non-0

0

“E” for expression

19 of 21

Logical Operators: Some Examples

19

Expr

Result

Remark

2 && 3

1

2 || 0

1

‘A’ && ‘0’

1

ASCII value of ‘0’≠0

‘A’ && 0

0

‘A’ && ‘b’

1

! 0.0

1

0.0 == 0 is guaranteed

! 10.05

0

Any real ≠ 0.0

(2<5) && (6>5)

1

Compound expr

20 of 21

Logical Operators: Precedence and Associativity

  • NOT has same precedence as equality operator
  • AND and OR are lower than relational operators
  • OR has lower precedence than AND
  • Associativity goes left to right

20

1 && 0 || 1 || 0

0 || 1 || 0

1 || 0

1

2 == 2 && 3 == 1 || 1==1 || 5==4

21 of 21

Operator Precedence for various operators

21

Operators

Description

Associativity

unary + unary -

Unary plus/minus

Right to left

* / %

Multiply, divide, remainder

Left to right

+ -

Add, subtract

Left to right

< > >= <=

Relational operators

Left to right

== !=

Equal, not equal

Left to right

&&

And

Left to right

||

Or

Left to right

=

Assignment

Right to left

INCREASING

LOW

HIGH

Note: Precedence of brackets () are above every other operator

Note: This list doesn’t include some other operators that we have not yet seen