You are on page 1of 12

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #33, October 25, 2011

Please switch off your mobile phones.

Announcements
Lab 11 starts from Friday, 28th October. Thursday section will do lab on 29th October (Saturday) y ( y) Lab exam in the week of 14th to 18th November Labs 11 and 12 will not be pre-announced to give you practice End-sem exam is on 25th November, 8:00 AM t afternoon. Copies can be seen on 28th afternoon

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Typecasting revisited
implicit and explicit

Unsigned type Conditional operator

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Typecasting
Explicit type casting can be used anywhere
int i; int j; float f; f = i / j; f = (float) i / (float) j ;

// will result in integer division // will result in floating point division

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Typecasting
int i; short s; s = 300; i=s*s; i = (int) (s * s) ; i = (int) s * s; (i t) // will result in overflow // will still result in overflow // by force converting first instance of s b f ti fi t i t f // system will convert second one also, and // the result will also be stored in an int

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Unsigned type


We can tell system that we will only store non negative non-negative numbers by attaching keyword unsigned to declaration Example:
unsigned short int unsigned int unsigned long int s; i; l;

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Conditional Operator


Requires three operands:
condition ? expression_1 : expression_2

Meaning:
If condition is true then evaluate (and set value of) expression 1 If condition is false then evaluate (and set value of) expression 2

Example:
int z, a, b; z = (a > b) ? a : b
Lec-33

// set z to max of a and b


Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6

Recap: Conditional Operator


z = (a > b) ? a : b

This is equivalent to the following code:


if (a > b) z = a; else z = b;

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Assignment Operator
Multiple assignments are possible, where the evaluation is possible done right to left.
i = j = 3; +=, -=, *=, /=, etc. // same as j = 3; i = j;

Can combine arithmetic operator with assignment Examples:


i += 7; i += j += k;
Lec-33

// same as i = i + 7; // same as j = j + k; i = i + j; // second assignment will use new value of j


Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8

Combining multiple assignments with arithmetic operations:

Comma Operator
Comma is an operator in C language C
We have seen this in for statemennt
for (i = 0 , j = 1; i < n; i++ , j++)

Evaluates each expression separated by comma, and returns the rightmost


Evaluation is guaranteed from left to right int i j k i t i, j, k; i = 2, 3, 4; // i will be set to 4 j = (k = 4, k+3); // j will be set to 7

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

10

#define
#define FALSE #define TRUE if (i < j) b = TRUE; else b = FALSE; FALSE 0 1

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

11

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

12

#define
When compiler sees the string FALSE in the program, it will be replaced by string 0 Useful when we want to use mnemonics instead of constants The first string in #define has to follow the rules of variable naming, the second part can be anything As a convention, we use upper-case letters for such names Note that we are only doing a replacement, not creating a memory area FALSE is not a variable A string within double quotes will not be replaced e g replaced, e.g., printf (FALSE); // It will not print 0 Substrings will not be substituted i = TRUE1; // It will not make i to be 11, Error
Lec-33 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 13

#define
A very common use of #define is the following:
#define MAXSIZE 100 int students[MAXSIZE]; char grades [MAXSIZE];

If we want to change the size, we only need to do it once.

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

14

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

15

Macros using #define


#define MAX(X, Y) z = MAX(i, j); Compiler will change this to: z = i > j ? i : j; j X>Y?X:Y

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

16

Macros
A more complex example:
#define LEAP(Y) ((Y%100 != 0)&&(Y%4 ==0) || (Y%400 ==0) ? 1 : 0 ; Will check for Leap year. Notice the semi-colon at the end.

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

17

Macros
Useful when something needs to be done multiple times in the program One can always write a one-line function But functions have performance overhead O li Macros are a good solution in such cases One line d l i i h Macros are type independent single Macro for all types is possible Be careful with parenthesis No space between macro name and opening parenthesis #define SQUARE(X) X * X SQUARE(5) SQUARE(4+1)

// Problem

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

18

Macros: Pitfalls
#define MAX(X, Y) i = MAX(square (p), q); will get replaced by: i = square (p) > q ? square (p) : q ( ) ( ) X>Y?X:Y

Note that the function square (p) may be called twice unnecessarily
Lec-33 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 19

10

Macros: Pitfalls
#define MAX(X, Y) i = MAX(p++, q); will get replaced by: i = p++ > q ? p++ : q ++ ++ X>Y?X:Y

Note that p may have been incremented twice


that may not have been the intention
Lec-33 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 20

#define
Scope: definitions are valid till end of file (not restricted to functions or blocks like variable declarations) We can change the definition through another #define We can stop the use of a definition through #undef
#define MAXSIZE 100 #undef MAXSIZE
Lec-33 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 21

11

Conditional Compilation
#define DEBUG 1 #ifdef DEBUG printf (This is printed only if I am debugging the code\n); #endif

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

22

Wishing you all a very Happy Diwali

Lec-33

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

23

12

You might also like