C Interview Questions Part 7

Q:What is atexit() ?
A:Function atexit( ) recevies parameter as the address of function of the type void fun ( void ). The function whose address is passed to atexit( ) gets called before the termination of program. If atexit( ) is called for more than one function then the functions are called in "first in last out" order. You can verify that from the output.
.
#include

void fun1( )

{
printf("Inside fun1\n");
}
void fun2( )
{
printf("Inside fun2\n");
}
main( )
{
atexit ( fun1 ) ;
/* some code */
atexit ( fun2 ) ;
printf ( "This is the last statement of
program?\n" );
}

Q:How do I write a user-defined function, which deletes each character in a string str1, which matches any character in string str2?
A: The function is as shown below:

Compress ( char str1[], char str2[] )
{
int i, j, k ;
for ( i = k = 0 ; str1[i] != ‘\0’ ; i++ )
{
for ( j = 0 ; str2[j] != ‘\0’ && str2[j] !=
str1[i] ; j++ );
if ( str2[j] == ‘\0’ )
str1[k++] = str1[I] ;
}
str1[k] = ‘\0’
}

Q:How does free( ) know how many bytes to free?
A: The malloc( ) / free( ) implementation remembers the size of each block allocated and returned, so it is not necessary to remind it of the size when freeing.

Q:What is the use of randomize( ) and srand( ) function?
A: While generating random numbers in a program, sometimes we require to control the series of numbers that random number generator creates. The process of assigning the random number generators starting number is called seeding the generator. The randomize( ) and srand( ) functions are used to seed the random number generators. The randomize( ) function uses PC's clock to produce a random seed, whereas the srand( ) function allows us to specify the random number generator's starting value.