RECURSION IN C LANGUAGE

                                                 RECURSION IN C LANGUAGE
                   In C , it is possible for the function to call themselves.A function is called 'recursive' if a statement within body of function calls the same function. Sometimes called 'circular definition' ,recursion is thus the process of defining something in terms of itself.

                  Let us now see a simple example of recursion. Suppose we want to calculate the factorial value of integer . As we know ,the factorial of a number is the product of all the integers between one and that number. For example , 4 factorial is 4*3*2*1. This can also be expressed as 4 !=4*3! where  '!' stands for factorial. Thus factorial of number can be expressed in the form of itself. Hence this can be programmed  using recursion.However , before we try to write a recursive function for calculating factorial let us take a look st the non-recursive function for calculating the factorial value of an integer .


recursion in c language
C INPUT RECURSION  PROGRAM

Here is output

output of recursion
OUTPUT IN C RECURSION PROGRAM 

                    Work through   the above program carefully, till you understand the logic of the program properly. Recursive factorial function can be understood only if you are through the  above logic.
following is the recursive version of the function to calculate the factorial value. 

                                 here is input of recursion in c language

input program recursion in c language
INPUT RECURSION IN C LANGUAGE

Here is output of c program

outut  recursion program in c language
OUTPUT PROGRAM OF RECURSION


                    Let us understand this recursive factorial function thoroughly . In the first run when the number entered through scanf() is one ,let us see what action does rec() take.The value of a is copied  into x . Since x turns out to be one , the condition if (x==1) is satisfied and hence one  is returned through the returned statement.
When the number entered through scanf() is two, the (x == 1) test fails , so we reach the statement,

f =x*rec(x*1);

              And here is where we meet recursion. How do we handle the expression X *rec (x-1)? We multiply x by rec(x-1). Since the current value of x is two, it is same as saying that we must calculate the value. We know that value returned by rec(1) is, so the expression reduces to (2*1), or simply 2. Thus the statement.

x*rec(x-1);
 
                                               DATE TYPE REVISITED
                The range of an integer constant depends upon the compiler. For a 16-bit compiler like Turbo C or C++. The range is -32768 to +32767. For a 32-bit compiler like visual studio or gcc the range would be -2147483648 to +2147483647. Here is 16-bit compiler means that when it compiles a C program. It generates machine language code that is targeted towards working on a, 32-bit compiler like visual studio generates machine language code that is targeted towards a 32-bit microprocessor like intel 8068/8088. Machine language code that is  targeted towards a 32-bit microprocessor like intel  pentium. Note that this does not mean that program compiled using Turbo C would not work on 32-bit processor. It would as if it were a 16-bit processor. This happens because a 32-bit processor provides support for programs compiled using 16-bit compilers.If this backward compatibility support is not provided the 16-bit program would not run on it. This is precisely what happens on the new intel itanium processors, which have withdrawn support for 16-bit code. 
                C offers a variation of the integer data type that provides what are called short and long integer values.The intention of providing this variations is to provide integer with different ranges wherever possible.Through not a rule ,short and long integer would usually occupy 2 and 4 bytes respectively.Each compiler can decide appropriate sizes depending on the operating systems and hardware, for which it is being written,subject to the following rules:

1) Shorts are at least 2 bytes big.
2) Longs are at least 4 bytes big.
3) Shorts are never bigger than ints.
4)  Ints are never bigger than longs.


compiler    short       int            long
 16-bit (turbo c/c++) 2 2 4
 32-bit(visual studio, gcc) 2 4 4



long int declare as
long int declare as

                Long integers cause the program to run a bit slower, but the range of values that we can use is expanded tremendously. The value of a long integer typically vary from -2147483648 to  +2147483647. More than this you should not need unless you are taking a world census.

                   If there are such things as longs, symmetry requires shorts as well -- integers that need less space in memory and thus help speed up program execution. Short integer variables are declared as,


short int declare as
short int declare as


              C allows the abbreviation of short int to short and of long int to long. So the declarations made above can be written as, 
long int declare as
long int declare as


                      Naturally, most C programmers prefer this short-cut.

             Sometimes, we come across situations where the constant is small enough to be an int, but still we want to give it as much storage as a long. In such cases, we add the suffix 'L' or 'l' at the end of the number, as in 23L.
  • Integers, signed and unsigned
               Sometimes, we know in advance that the value stored in a given integer variable will always be positive-- when it is being used to only count things, for example. In such a case we can declare the variable to be unsigned, as in,

unsigned int num_students ;

           With such a declaration, the range of permissible integer values will shift from the range -2147483648 to  +2147483647 to the range 0 to 4294967295. Thus, declaring an integer as unsigned almost double the size of the largest possible value that it can otherwise take. This so happens because on declaring the integer has unsigned, the left most bit is now free and is not used to store the sign of number not that an unsigned integer still occupies two bytes .This is how an unsigned integer can be declared:

unsigned int i;
unsigned i;

                 Like an unsigned there also exists a short unsigned int and a long unsigned int. by default,a short int is signed short int and a long int is a signed long int.

                                   Chars,signed and unsigned 
                  Parallel to signed and unsigned ints (either short or long)there also exist signed and unsigned chars,both occupying one byte each,but having different range.to begin with,it might appear strange as to how a char can have a sign.consider the statement

char ch='A';

             Here what gets stored ch is the binary equivalent of the ASCII/Unicode value of 'A' .And if 65 is binary can be stored ,then -54's binary can also be stored (in a signed char).A signed char is same a an ordinary char and has a range from -128 to +127; whereas ,an unsigned char has a range from 0 to 255.Let now see a program that illustrates this range:

#include<stdio.h>
int main()
{
char ch=143;
printf("\n%d%c\n",ch,ch);
return 0;
}

  • Float and doubles
               A float occupies four bytes in memory and can range from -3.4e38 to +3.4e38. If this is insufficient ,then C offers a double data type that occupies 8 bytes in memory and has a range from 1.7e308 to +1.7e308. A variable of type double can be declared as, 

double a, population;

            If the situation demands usage of real number that lie even beyond thr range of offered by double data type,than there exists a long double that can range from -1.7e4932  to +1.7e4932. A long double occupy 10 bytes in memory.




Previous
Next Post »
}; //]]>