Esc101: Fundamentals of Computing Esc101: Fundamentals of Computing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #34, October 27, 2011

Please switch off your mobile phones.

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

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

Recap: #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-34

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

Recap: 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-34

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

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

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

Recap: 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-34 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6

Recap: 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-34 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 7

#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

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

Program in multiple source files


A C program can be written in multiple files
Different programmers may be writing code for different parts of the problem You may want to write related functions in a single file which may be reused in another problem solving

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

Program in multiple source files


Let file1.c contain file1 c
main ( ) compute ( )

Let file2.c contain


double square (double x) double sqrt (double x)

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

Program in multiple source files


In file1 c we need to include the prototypes of called file1.c, function of file2.c
The following line should be included in file1.c
double square (double x)

But no need to mention sqrt( ), since it is not being called

We compile both the files together


gcc fil 1 file2.c file1.c fil 2 The executable file, a.out, can now be used as usual

Lec-34

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

12

Lec-34

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

13

Global variables in case of multiple files


We have seen that variables could be declared at the beginning of the file before any function is defined.
We have been calling them global variables These can be used from any function in that file Technically correct term for them is external variables as they are external to each function which is using them If a global variable is defined in file1.c, how do we use it in functions in file2.c
W could declare them explicitly in each function that uses We ld d l h l l hf h them as follows: extern int roll_numbers [ ]; Or such a declaration could be done in the beginning of file2.c, before any function is defined
Lec-34 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 14

Scope of external variables


External variables are defined in one file, and are declared in all such files where a function may access them In the file where an external variable is defined, the scope is from the d fi i i till the end of the file f h definition ill h d f h fil
extern keyword is not used memory allocation takes place while compiling this file

In other files, there is only a declaration


extern keyword is to be used no memory allocation takes place if the declaration is outside all functions, then scope is from th d l ti i t id ll f ti th i f the declaration till the end of the file if the declaration is inside a function, then scope is from the declaration till the end of that function Defining global (external) variables in multiple files is an error
Lec-34 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 15

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

By declaring a variable static


the variable becomes a permanent variable memory allocation takes place only once, even across recursive calls y p y memory is not taken away after the function call, hence one can access value stored during the previous function call

there is no change in scope of the variable though


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

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

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

17

Lec-34

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

18

Four types of variables


There are four classes of variables in C
automatic external static register

Lec-34

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

19

10

Any Questions?

Lec-34

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

20

11

You might also like