Ketan Kore
Sunbeam Infotech
Sunbeam Infotech www.sunbeaminfo.com
Storage class
• Each running process
Initial have following sections:
Storage Life Scope
value • Text
• Data
• Heap
auto / local Stack Garbage Block Block • Stack
CPU • Storage class decides
register Garbage Block Block
register • Storage (section)
• Life (existence)
Data • Scope (visibility)
static Zero Program Limited
section
• Accessing variable
extern / Data outside the scope raise
Zero Program Program
global section compiler error.
Sunbeam Infotech www.sunbeaminfo.com
Storage class
• Local variables declared inside the function.
• Created when function is called and destroyed when function is completed.
• Global variables declared outside the function.
• Available through out the execution of program.
• Declared using extern keyword, if not declared within scope.
• Static variables are same as global with limited scope.
• If declared within block, limited to block scope.
• If declared outside function, limited to file scope.
• Register is similar to local storage class, but stored in CPU register for faster access.
• register keyword is request to the system, which will be accepted if CPU register is available.
Sunbeam Infotech www.sunbeaminfo.com
Recursion
• Function calling itself is called as • Advantages
recursive function. 1. Simplified Readable programs
• To write recursive function consider (Divide and conquer Problems )
• Explain process/formula in terms of itself • Disadvantages
• Decide the end/terminating condition • Needs More Space ( FAR on stack )
• Examples: • Needs More Time ( FAR Created )
• n! = n * (n-1)! 0! = 1
• xy = X * xy-1 x0 = 1
• Tn = Tn-1 + Tn-2 T1 = T2 = 1
• factors(n) = 1st prime factor of n *
factors(n)
Sunbeam Infotech www.sunbeaminfo.com
Sunbeam Infotech www.sunbeaminfo.com
Recursion execution
int fact(int n) { int fact(int n) { int fact(int n) { int fact(int n) { int fact(int n) { int fact(int n) {
int r; int r; int r; int r; int r; int r;
if(n==0) if(n==0) if(n==0) if(n==0) if(n==0) if(n==0)
return 1; return 1; return 1; return 1; return 1; return 1;
r = n * fact(n-1); r = n * fact(n-1); r = n * fact(n-1); r = n * fact(n-1); r = n * fact(n-1); r = n * fact(n-1);
return r; return r; return r; return r; return r; return r;
} } } } } }
int main() {
int res; 5! = 5 * 4!
res = fact(5);
4! = 4 * 3!
printf("%d", res);
return 0; 3! = 3 * 2!
} 2! = 2 * 1!
1! = 1 * 0!
0! = 1
Sunbeam Infotech www.sunbeaminfo.com
Thank you!
Ketan Kore<
[email protected]>
Sunbeam Infotech www.sunbeaminfo.com