Professional Documents
Culture Documents
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
Recap
Typecasting revisited
implicit and explicit
Lec-33
Recap: Typecasting
Explicit type casting can be used anywhere
int i; int j; float f; f = i / j; f = (float) i / (float) j ;
Lec-33
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
Lec-33
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
Lec-33
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;
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++)
Lec-33
Lec-33
10
#define
#define FALSE #define TRUE if (i < j) b = TRUE; else b = FALSE; FALSE 0 1
Lec-33
11
Lec-33
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];
Lec-33
14
Lec-33
15
Lec-33
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
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
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
#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
22
Lec-33
23
12