Recursion
Computer Science, GATE

Recursion Using C

In c, it is possible for functions to call themselves. A function calling itself is called as recursion sometimes called as ‘circular definition’. It is the process of defining something in terms of itself.

void abc()
{
abc(); //Function abc is calling itself again and again this is recursion
}

int main()
{
abc();
}



Recursion is used in problems like TowerofHanoi, tree traversal like Inorder,Preorder,Postorder etc.

Lets take an example which uses recursion. The example below is classical example for understanding the concept of recursion.

#include<stdio.h>
#include<conio.h>
long rec(int x);
int main()
{
long Factorial=1;
int number=1;
printf(“Enter No. to Find Factorial\n”);
scanf(“%d”,&number);
Factorial=rec(number);
printf(“Factorial of %d is %ld\n”,number,Factorial);
return 0;
}
long rec(int x) //Recursive function for calculating factorial
{
int f
if(x==1)
return 1;
else
f=x*rec(x-1); //Here function calling itself
return f;
}

Understanding above Code

This program can be understood as follow for number = 3.

Recursion

When we call function rec for x=3 it check is x equal to 1 no so it goes to else part and perform 3*rec(3-1). Now rec is again called for x=2 it again checks is is equal to 1 no so it goes to else part and perform 2*rec(2-1).Again rec is called for x=1 and finally here x is equal to 1 so it return 0 to the last function i.e., rec(2-1) becomes 1 and then multiply 2 to it goes to last function call i.e., rec(3-1) and it performs 3*2 = 6 and return it to main() hence function terminates here.

So we have seen here how a function call itself again and again untill a terminating condition is met here that condition is x equal to 1 or not once x becomes equal to 1 it backtracks to last function call.



Recursion uses stack because here the function which was called first was executed at the end i.e., in FIRST IN LAST OUT manner.

For More on Computer Science Click Here..!!

System Calls
Computer Science

System Calls for copying files in c

This article is fully dedicated to using system calls in c  so if you are new to system calls then you should first learn about system calls what they are and why they are used then only this article will be useful. For basics on system calls you must read Book named Operating System Principles by Galvin



//Program to copy content of one file to another file using system calls
//Author: Shubham Lashkan

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#define buffersize 10000
int main()
{
char source[25],destination[25]; //Source and destination filename
char buffer[buffersize]; //Character buffer
ssize_t read_in,write_out; //Number of bytes returned by single read and write operation
printf(“Enter source file name”);
scanf(“%s”,&source);
printf(“%s”,source);
int sourcefiledesc = open(source,O_RDONLY); //Source file open in read only mode
if(sourcefiledesc < 0 )
{
printf(“Source file not Exist”); //Source file not found
}
else
{
printf(“Enter destination file name”);
scanf(“%s”,&destination);
/* Destination file open in write mode and if not found then create*/
int destfiledesc = open(destination,O_WRONLY | O_CREAT);
while((read_in = read(sourcefiledesc,&buffer,buffersize))>0)
{
write_out = write(destfiledesc,&buffer,read_in);
}
close(sourcefiledesc);
close(destfiledesc);
}
return 0;
}



Description of above code of copying file using system calls

Now lets understand above code. We have used file system calls namely Create,open,close,read and write.

First thing we had included all the necessary header files. Now we have defined a constant that refer to size of buffer.Then we open our source file in read only mode and destination file in write only mode.

int sourcefiledesc = open(source,O_RDONLY);

int destfiledesc = open(destination,O_WRONLY | O_CREAT);

Here sourcefiledesc and destfiledesc are file descriptor that uniquely identifies opened file process.

while((read_in = read(sourcefiledesc,&buffer,buffersize))>0)
{
write_out = write(destfiledesc,&buffer,read_in);
}

From above code we start copying content of one file to other for that we had use while loop. Write system call is using number of bytes returned by read system call in that way write system call how much data is to be written in destination file.After copy operation is complete we close both the file descriptors.



Above code is very easy to understand if you have code for file handling in c. In case you have any doubt then let us know in the comment box below.