Wednesday 27 May 2009

C Interview Questions Part 8

C interview question:If we have declared an array as global in one file and we are using it in another file then why doesn't the sizeof operator works on an extern array?
Answer: An extern array is of incomplete type as it does not contain the size. Hence we cannot use sizeof operator, as it cannot get the size of the array declared in another file. To resolve this use any of one the following two solutions:
1. In the same file declare one more variable that holds the size of array. For example,
array.c
int arr[5] ;
int arrsz = sizeof ( arr ) ;
myprog.c
extern int arr[] ;
extern int arrsz ;
2. Define a macro which can be used in an array
declaration. For example,
myheader.h
#define SZ 5
array.c
#include "myheader.h"
int arr[SZ] ;
myprog.c
#include "myheader.h"
extern int arr[SZ] ;

C interview question:How do I write printf( ) so that the width of a field can be specified at runtime?
Answer: This is shown in following code snippet.
main( )
{
int w, no ;
printf ( "Enter number and the width for the
number field:" ) ;
scanf ( "%d%d", &no, &w ) ;
printf ( "%*d", w, no ) ;
}

Here, an '*' in the format specifier in printf( ) indicates that an int value from the argument list should be used for the field width.

C interview question:How to find the row and column dimension of a given 2-D array?
Answer: Whenever we initialize a 2-D array at the same place where it has been declared, it is not necessary to mention the row dimension of an array. The row and column dimensions of such an array can be determined programmatically as shown in following program.

void main( )
{
int a[][3] = { 0, 1, 2,9,-6, 8,7, 5, 44,23, 11,15 } ;
int c = sizeof ( a[0] ) / sizeof ( int ) ;
int r = ( sizeof ( a ) / sizeof ( int ) ) / c ;
int i, j ;
printf ( "\nRow: %d\nCol: %d\n", r, c ) ;
for ( i = 0 ; i < r ; i++ )
{
for ( j = 0 ; j < c ; j++ )
printf ( "%d ", a[i][j] ) ;
printf ( "\n" ) ;
}
}

Read more...

Thursday 14 May 2009

C Interview Questions Part 7

C interview question:What is atexit() ?
Answer: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" );
}

C interview question:How do I write a user-defined function, which deletes each character in a string str1, which matches any character in string str2?
Answer: 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’
}

C interview question:How does free( ) know how many bytes to free?
Answer: 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.

C interview question:What is the use of randomize( ) and srand( ) function?
Answer: 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.

Read more...

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP