Technical Chat
C Workshop
Day- 6
extern int g=5;
void abc()
{
int a;
static int s=5;
a=s--;
++g;
printf(“n %d %d %d”, a,s,g);
if(a>=3)
abc();
printf(“n %d %d %d”, a,s,g);
}
void xyz()
{
auto int a;
static int s;
a=++s;
--g;
printf(“n %d %d %d”, a,s,g);
if(a<=3)
xyz();
printf(“n %d %d %d”, a,s,g);
}
void main()
{
void xyz(void);
xyz();
abc();
}
O/p-
1 1 4
2 2 3
3 3 2
4 4 1
4 4 1
3 4 1
2 4 1
1 4 1
5 4 2
4 3 3
3 2 4
2 1 5
2 1 5
3 1 5
4 1 5
5 1 5
Classification of Recursion
● Internal Recursion Process
● External Recursion Process
● Whenever a recursion function is calling itself
then it is called internal recursion process.
● When one recursion function is calling another
recursion function then it is called external
recursion process.
extern int g=5;
void abc()
{
auto int a;
static int s=5;
a=s--;
++g;
printf(“n %d %d %d”, a,s,g);
if(a>=3)
abc();
printf(“%d %d %d”, a,s,g);
}
void main()
{
void xyz(void);
xyz();
}
void xyz()
{
int a;
static int s;
a=s++;
--g;
printf(“n %d %d %d”, a,s,g);
if(a<=1)
{
abc();
xyz();
}
printf(“n %d %d %d”, a,s,g);
}
O/p-
0 1 4
5 4 5
4 3 6
3 2 7
2 1 8
2 1 8
3 1 8
4 1 8
5 1 8
1 2 7
1 0 8
1 0 8
2 3 7
2 3 7
1 3 7
0 3 7
recursion by using main()
● By using auto variable it became stack overflow
● Because for every call auto variable will be
constructed.
void main()
{
int a= 5;
++a;
printf(“%d”, a);
if (a<=6)
main();
printf(“%d”,a);
}
O/p-
6666666666.........Stack overflow
void main()
{
static int s=2;
++s;
printf(“%d”,s);
if(s<=4)
main()
printf(“%d”,s);
}
O/p- 235555
Power of a number through recursion:
int power(int b, int e)
{
if (e<0)
return 0;
else if(e==0)
return 1;
else
return(b*power(b-e-1));
}
void main()
{
int a,b,p;
printf(“Enter value of a:”);
scanf(“%d”, &a);
printf(“Enter value of b:”);
scnaf(“%d”, &b);
p=power(a,b);
printf(“n %d^%d value is :
%d”, a,b,p);
}
Factorial through recursion.
void main()
{
int n,f;
int fact(int);
printf(“Enter a value:”);
scanf(“%d”, &n);
f=fact(n);
printf(“n %d fact value is: %d”,n,f);
}
int fact(int n)
{
if(n<0)
return 0;
else if(n<=1)
return 1;
else
return (n*fact(n-1));
}
Dangling Pointer
● A pointer variable which is pointing to a
dead or inactive location, is called
dangling pointer.
Function pointer
● A pointer variable which holds the address of the
function, is called function pointer.
● Function pointer calls are faster then normal
function calls.
● By using function pointer it is possible to pass a
function as a argument to another function.

C workshop day 6

  • 1.
  • 2.
    extern int g=5; voidabc() { int a; static int s=5; a=s--; ++g; printf(“n %d %d %d”, a,s,g); if(a>=3) abc(); printf(“n %d %d %d”, a,s,g); } void xyz() { auto int a; static int s; a=++s; --g; printf(“n %d %d %d”, a,s,g); if(a<=3) xyz(); printf(“n %d %d %d”, a,s,g); } void main() { void xyz(void); xyz(); abc(); }
  • 3.
    O/p- 1 1 4 22 3 3 3 2 4 4 1 4 4 1 3 4 1 2 4 1 1 4 1 5 4 2 4 3 3 3 2 4 2 1 5 2 1 5 3 1 5 4 1 5 5 1 5
  • 4.
    Classification of Recursion ●Internal Recursion Process ● External Recursion Process ● Whenever a recursion function is calling itself then it is called internal recursion process. ● When one recursion function is calling another recursion function then it is called external recursion process.
  • 5.
    extern int g=5; voidabc() { auto int a; static int s=5; a=s--; ++g; printf(“n %d %d %d”, a,s,g); if(a>=3) abc(); printf(“%d %d %d”, a,s,g); } void main() { void xyz(void); xyz(); } void xyz() { int a; static int s; a=s++; --g; printf(“n %d %d %d”, a,s,g); if(a<=1) { abc(); xyz(); } printf(“n %d %d %d”, a,s,g); }
  • 6.
    O/p- 0 1 4 54 5 4 3 6 3 2 7 2 1 8 2 1 8 3 1 8 4 1 8 5 1 8 1 2 7 1 0 8 1 0 8 2 3 7 2 3 7 1 3 7 0 3 7
  • 7.
    recursion by usingmain() ● By using auto variable it became stack overflow ● Because for every call auto variable will be constructed. void main() { int a= 5; ++a; printf(“%d”, a); if (a<=6) main(); printf(“%d”,a); } O/p- 6666666666.........Stack overflow
  • 8.
    void main() { static ints=2; ++s; printf(“%d”,s); if(s<=4) main() printf(“%d”,s); } O/p- 235555
  • 9.
    Power of anumber through recursion: int power(int b, int e) { if (e<0) return 0; else if(e==0) return 1; else return(b*power(b-e-1)); } void main() { int a,b,p; printf(“Enter value of a:”); scanf(“%d”, &a); printf(“Enter value of b:”); scnaf(“%d”, &b); p=power(a,b); printf(“n %d^%d value is : %d”, a,b,p); }
  • 10.
    Factorial through recursion. voidmain() { int n,f; int fact(int); printf(“Enter a value:”); scanf(“%d”, &n); f=fact(n); printf(“n %d fact value is: %d”,n,f); } int fact(int n) { if(n<0) return 0; else if(n<=1) return 1; else return (n*fact(n-1)); }
  • 11.
    Dangling Pointer ● Apointer variable which is pointing to a dead or inactive location, is called dangling pointer.
  • 12.
    Function pointer ● Apointer variable which holds the address of the function, is called function pointer. ● Function pointer calls are faster then normal function calls. ● By using function pointer it is possible to pass a function as a argument to another function.