Sunday, 20 May 2012

Define gets(), malloc() function, pointer, union, and recurtion.


gets()
The name of the function gets() stands for “get string”. The gets() function reads till the next new line character Enter key and can include spaces and taken in a string thus reading whole sentences and adds the NULL character to it and assign it to the required variables which comes as an argument of the function. Following example demonstrates the use of the function gets().
/*Demonstration of gets() function */
main()
{
char employee_name[20];
gets(employee_name);
printf("Employee: %s\n",employee_name);
}

malloc ( ) function
The malloc() function allocates a specified number of bytes in memory. The allocated region is not filled with zero. The starting address is returned if the function is successful. A zero is returned if the function fails to allocate memory. To assign sufficient memory for x, we can make use of the library function malloc(), as follows:
X = malloc(10*size of(int))
This function reserves a block of memory whose size (in bytes) is equivalent to the size of an integer.

Pointer
A pointer is a derive data type in C. it is built from one of the fundamental data types available in C. Pointer contains memory address contains the instruction and data as a record.
Pointer is used to access and manipulate data stored in the memory. It is also known as referential variable.

Union
Unions are concept borrowed from structures and therefore follow the same syntax as structures. However, there is a major distinction between them in terms of storage. In structures, each member has its own storage location; where as all the members of a union uses the same location. This implies that, although a union may contain many members of different types, it can handle only one member at a time. Like structure, a union can be declared using the keyword union as follows:
union item
{
   int m;
   float x;
   char c;
}code;
This declare a variable code of type union item.

Recursion 
Recursion is a special case of chaining of a function, where a function calls itself. A very simple example of recursion is given below:
main()
{
          printf("This is an example of recursion\n");
          main();
}
When executed this program will produce an output something like this:
          This is an example of recursion
          This is an example of recursion
          This is an example of recursion
Execution is terminated abruptly; otherwise the execution will continue indefinitely.

No comments:

Post a Comment