23
1 • Expressions – Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants – #define directive Today’s Material

1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

Embed Size (px)

Citation preview

Page 1: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

1

• Expressions– Arithmetic Operators (+, -, *, /, %, ++, --)

• Preprocessor and defined Constants– #define directive

Today’s Material

Page 2: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

2

What’s an Expression?• A mathematical formula that computes a value

– formed by operators (+, -, *, /, %, …)

celsius = (fahrenheit-32)/1.8;

area = 3.14*radius*radius;

circumference = 2*3.14*radius;

volume = length*width*height;

sum = sum + 5;

Page 3: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

3

Arithmetic Operators• Basic operators for performing arithmetic are

the same in many programming languages:

Modulus (remainder)%

Division/

Multiplication*

Subtraction-

Addition+

Binary operatorsBinary operators

Decrement--

Increment++Unary operatorsUnary operators

Page 4: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

4

Arithmetic Operators: Example

14sum = sum % 3

%Modulus

24sum = sum / 2

/Divide

84sum = sum * 2

*Multiply

24sum = sum – 2

-Subtraction

64sum = sum + 2

+Addition

Value of sum after

Value of Sumbefore

EquationOperatorOperation

34--sum--Decrement

54++sum++Increment

Value of sumafter

Value of sum before

EquationOperatorOperation

Page 5: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

5

Notes on / Operator• If both operands are integers, / performs

integer divisionint a = 5;int b = 4;int result = a/b; /* 1.25 -> 1 */printf(“result: %d\n”, result); /* will print 1 */

Page 6: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

6

Mixed Operands• If one of the operands is a float/double, the

other operand is implicitly converted to float/double before the expression is evaluated– If the l-value is an integer, the result is typecast

to int before assignment– If the l-value is a float/double, the result is

typecast to float/double before assignment

int result;float fresult;result = 5/4.0; /* 1.25 -> 1 */fresult = 5/4.0; /* 1.25 -> 1.25 */

printf(“result: %d, fresult: %.2f\n”, result, fresult);

Page 7: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

7

More on Unary Operators

• result = ++a; /* increment first, then assign */• Equivalent to:

a = a+1;result = a;

• result = --a; /* decrement first, then assign */

• Equivalent to:a = a-1;result = a;

Page 8: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

8

More on Unary Operators

• result = a++; /* assign, then increment */• Equivalent to:

result = a; a = a+1;

• result = a--; /* assign, then decrement */• Equivalent to:

result = a;a = a-1;

• Unary operators can be used with both int & float/double operands. But typically applied to ints

Page 9: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

9

Compound Assignment Operators

• Compound assignment operators:+= -= *= /= %= …

• For example:sum += data; /* sum = sum + data */a -= 6; /* a = a – 6; */product *= data; /* product = product*data */a %= 2; /* a = a%2 */a /= b+c; /* a = a/(b+c) */…

Page 10: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

10

Associativity & Precedence• When an expression contains more than one

operator of the same type, associativity rules apply

result = a+b+c; result = (a+b)+c; /* left associativity */

a = b = c; a = (b=c); /* right associativity */

• When an expression contains different operators together, precedence rules apply

result = a+b*c; result = a+(b*c); /* * has higher precedence than + */

Page 11: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

11

Associativity & Precedence Rules

right=, +=, -=, ..

4

left+, -3

left*, /, %2

right -, ++, --1

AssociativityOperatorPrecedence

• It is difficult to remember all associativity and precedence rules

• So it is best to parenthesize the expression yourself to avoid ambiguity

Page 12: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

12

Expression Examples(1)int a = 10, b = 22;int result = a+b*3;printf("a: %d, b: %d, <result=a+b*3>: %d\n", a, b, result);

int a = 10, b = 22;int result = (a+b)*3;printf("a: %d, b: %d, <result=(a+b)*3>: %d\n", a, b,

result);

a: 10, b: 22, <result=a+b*3>: 76

a: 10, b: 22, <result=(a+b)*3>: 96

Page 13: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

13

Expression Examples(2)

int a = 7, b = 9, c = 4;int result = (a+10)%b/c;printf("a: %d, b: %d, c: %d, <result=(a+10)%%b/c>: %d\n", a, b, c, result);

int a = 7, b = 9, c = 4;int result = (a+10)%(b/c);printf("a: %d, b: %d, c: %d, <result=(a+10)%%(b/c)>: %d\n", a, b, c, result);

a: 7, b: 9, c: 4, <result=(a+10)%b/c>: 2

a: 7, b: 9, c: 4, <result=(a+10)%(b/c)>: 1

Page 14: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

14

Expression Examples(3)

int a = 7, b = 8;printf("a(before): %d, ", a);a*=b+1;printf("a(after): %d, b: %d\n", a, b);

a = b = c = 1; printf("a(before): %d, b(before): %d, ", a, b);a+=b+=c;printf("a(after): %d, b(after): %d, c: %d\n", a, b, c);

a(before): 7, a(after): 63, b: 8

a(before): 1, b(before): 1, a(after): 3, b(after): 2, c: 1

Page 15: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

15

Expression Examples(4)

a = 10; b = 5; printf("a(before): %d, b(before): %d, ", a, b);result = (a++ + ++b);printf("a(after): %d, b(after): %d, result: %d\n", a, b,result);

a = 10; b = 5; printf("a(before): %d, b(before): %d, ", a, b);result = (++a + ++b);printf("a(after): %d, b(after): %d, result: %d\n", a,

b,result);

a(before): 10, b(before): 5, a(after): 11, b(after): 6, result: 16

a(before): 10, b(before): 5, a(after): 11, b(after): 6, result: 17

Page 16: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

16

Example C Program (1)

#include <stdio.h>

/* Convert fahrenheit to celsius */main() { float fahrenheit, celsius;

printf(“Enter a temp in fahrenheit: “);

scanf(“%f”, &fahrenheit);

celsius = (fahrenheit-32)/1.8;

printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius);

}

Prompt the user and get the fahrenheit temperature to convert

celsius = (fahrenheit-32)/1.8

Print the fahrenheit and celsius degrees

StartStart

EndEnd

Page 17: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

17

Example C Program(2)#include <stdio.h>

/* Compute sum, product, avg of 2 ints */main() { int number1, number2; int sum, product, average;

printf(“Enter 2 integers: “); scanf(“%d%d”, &number1, &number2);

sum = number1+number2; product = number1*number2; average = sum/2;

printf(“sum: %d, product: %d, avg:%d\n”,

sum, product, average);} /* end-main */

Prompt the user and get number1 and number2

sum = number1 + number2

Print sum, product and average

product = number1 * number2

average =sum/2

StartStart

EndEnd

Page 18: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

18

Example C Program(3)#include <stdio.h>

/* Computes the circumference and area of a circle */main() { float radius, circumference, area;

printf(“Enter the radius: “); scanf(“%f”, &radius); circumference =

2*3.141592*radius; area = 3.141592*radius*radius; printf(“Circumference: %f, area:

%f\n”, circumference, area); } /* end-main */

Prompt the user to enter the radius

circumference = 2*3.141592*radius

Print circumference and area of the circle

area = 3.141592*radius*radius;

StartStart

EndEnd

Get radius of the circle

Page 19: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

19

The Need for Symbolic Constants

• In the previous C program, we twice used the number PI = 3.141592 inside the program

• Using such magic numbers inside of C programs usually leads to bugs– We may type one of the PI numbers wrong!– If we want to add more precision to PI, we need

to change all occurrences of the PI numbers, which may also lead to bugs

• Question: Is there a solution?– Yes: Symbolic constants

Page 20: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

20

Defining Symbolic Constants• Defined using the preprocessor directive

#define

• Example:

#define TRUE 1#define FALSE 0

#define PI 3.1415926535897932

• Using symbolic constants:– circumference = 2*PI*radius;– area = PI*radius*radius;

Page 21: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

21

Example C Program(3)#include <stdio.h>

#define PI 3.1415926535897932

/* Computes the circumference and area of a circle */main() { float radius, circumference, area;

printf(“Enter the radius: “); scanf(“%f”, &radius); circumference = 2*PI*radius; area = PI*radius*radius; printf(“Circumference: %f, area: %f\n”, circumference,

area); } /* end-main */

Page 22: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

22

Resolving Symbolic Constants

• Symbolic constants are resolved by the preprocessor before compilation begins– Preprocessors works like

find/replace– Replaces every occurrence

of PI with its definition 3.1415926535897932

– Preprocessor also removes comments from the code so that the compiler does not have to deal with them

prog.c

COMPILER

ASSEMBLER

LINKER

LOADER + OS

prog.s

prog.oprintf.o

scanf.o

prog.exe

program running

PREPROCESSOR

prog.i

Page 23: 1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

23

A Common #define Bug• Find the error here:

#define SIZE 5;int main(){ printf(“Size is %d\n”, SIZE);}

int main(){ printf(“Size is %d\n”, 5;);}

• After find/replace by preprocessor: