Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing
Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing
Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing
Announcements
Lab 11 starts from Friday, 28th October. Thursday ( y (Todays) section will do lab on 29th October (Saturday) y ) ( y) Lab 12 starts from Thursday, 3rd November.
Monday section will do lab on 5th November (Saturday)
Lab exam in the week of 14th to 18th November Labs 11 and 12 will not be pre-announced to give you practice pre announced End-sem exam is on 25th November, 8:00 AM Copies can be seen on 28th afternoon.
Lec-34 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 1
Recap
Assignment operator Comma operator #define Macros using #define
Pitfalls
Lec-34
Recap: #define
A very common use of #define is the following:
#define MAXSIZE 100 int students[MAXSIZE]; char grades [MAXSIZE];
Lec-34
Lec-34
Recap: 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-34
Note that the function square (p) may be called twice unnecessarily
Lec-34 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6
#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-34 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8
Conditional Compilation
#define DEBUG 1 #ifdef DEBUG printf (This is printed only if I am debugging the code\n); #endif
Lec-34
There can only be a single main ( ) function. From any function in any file, you can call functions of any other file
We have already seen this we can call C library functions C The prototype of functions in file2 that we call from any function of file1 should be mentioned in file1 also
Again, we have been doing this through including header files
Lec-34 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 10
Functions in file2.c do not call any functions in file1.c Compute( ) function in file1.c calls square( )
but does not call sqrt ( )
Lec-34 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 11
Lec-34
12
Lec-34
13
Static variables
Normally declared variables are called automatic variables
The memory is allocated for each invocation of function For a recursive function, memory is allocated for each recursive call function The memory is taken away when the function returns Previously stored value is not available to the next invocation of the same function
Static variables
Within a function we can define static variables as: function,
static int roll_numbers[500];
If static is not used, and it is recursive function, then every call would have needed such a large memory area, and there th was no way to access what previous call to the t h t i ll t th function was doing.
Lec-34
17
Lec-34
18
Lec-34
19
10
Any Questions?
Lec-34
20
11