C Interview Questions and Answers Part 2

10/18/2010 No Comment


Question: How do I compare character data stored at two different memory locations?
Answer: Sometimes in a program we require to compare memory ranges containing strings. In such a situation we can use functions like memcmp( ) or memicmp( ). The basic difference between two functions is that memcmp( ) does a case-sensitive comparison whereas memicmp( ) ignores case of characters. Following program illustrates the use of both the functions.

#include
main( )
{
char *arr1 = "Kicit" ;
char *arr2 = "kicitNagpur" ;
int c ;
c = memcmp ( arr1, arr2, sizeof ( arr1 ) ) ;
if ( c == 0 )
printf ( "\nStrings arr1 and arr2 compared using memcmp are identical" ) ;
else
printf ( "\nStrings arr1 and arr2 compared using memcmp are not identical") ;
c = memicmp ( arr1, arr2, sizeof ( arr1 ) ) ;
if ( c == 0 )
printf ( "\nStrings arr1 and arr2 compared using memicmp are identical" );
else
printf ( "\nStrings arr1 and arr2 compared using memicmp are not identical" ) ;
}

Fixed-size objects are more appropriate as compared to variable size data objects. Using variable-size data objects saves very little space. Variable size data objects usually have some overhead. Manipulation of fixed-size data objects is usually faster and easier. Use fixed size when maximum size is clearly bounded and close to average. And use variable-size data objects when a few of the data items are bigger than the average size. For example,

char *num[10] = { "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;

Instead of using the above, use
char num[10][6] = { "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;

The first form uses variable-size data objects. It allocates 10 pointers, which are pointing to 10 string constants of variable size. Assuming each pointer is of 4 bytes, it requires 90 bytes. On the other hand, the second form uses fixed size data objects. It allocates 10 arrays of 6 characters each. It requires only 60 bytes of space. So, the variable-size in this case does not offer any advantage over fixed size.

Question:The Spawnl( ) function...
Answer:DOS is a single tasking operating system, thus only one program runs at a time. The Spawnl( ) function provides us with the capability of starting the execution of one program from within another program. The first program is called the parent process and the second program that gets called from within the first program is called a child process. Once the second program starts execution, the first is put on hold until the second program completes execution. The first program is then restarted. The following program demonstrates use of spawnl( ) function.


/* Mult.c */
int main ( int argc, char* argv[ ] )
{
int a[3], i, ret ;
if ( argc <> 3 )
{
printf ( "Too many or Too few arguments..." ) ;
exit ( 0 ) ;
}
for ( i = 1 ; i < ret =" a[1]" val =" spawnl">


Questions:Are the following two statements identical?

char str[6] = "Kicit" ;

char *str = "Kicit" ;

Answer: No! Arrays are not pointers. An array is a single, pre-allocated chunk of contiguous elements (all of the same type), fixed in size and location. A pointer on the other hand, is a reference to any data element (of a particular type) located anywhere. A pointer must be assigned to point to space allocated elsewhere, but it can be reassigned any time. The array declaration char str[6] ; requests that space for 6 characters be set aside, to be known by name str. In other words there is a location named str at which six characters are stored. The pointer declaration char *str ; on the other hand, requests a place that holds a pointer, to be known by the name str. This pointer can point almost anywhere to any char, to any contiguous array of chars, or nowhere.


Question:Is the following code fragment correct?

const int x = 10 ;

int arr[x] ;

Answer: No! Here, the variable x is first declared as an int so memory is reserved for it. Then it is qualified by a const qualifier. Hence, const qualified object is not a constant fully. It is an object with read only attribute, and in C, an object associated with memory cannot be used in array dimensions.


Question:How do I write code to retrieve current date and time from the system and display it as a string?

Answer:Use time( ) function to get current date and time and then ctime( ) function to display it as a string. This is shown in following code snippet.

#include

void main( )

{

time_t curtime ;

char ctm[50] ;

time ( &curtime ) ; //retrieves current time &

stores in curtime

printf ( "\nCurrent Date & Time: %s", ctime (

&curtime ) ) ;

}


Question:How do I change the type of cursor and hide a cursor?

Answer: We can change the cursor type by using function _setcursortype( ). This function can change the cursor type to solid cursor and can even hide a cursor. Following code shows how to change the cursor type and hide cursor.

#include

main( )

{

/* Hide cursor */

_setcursortype ( _NOCURSOR ) ;

/* Change cursor to a solid cursor */

_setcursortype ( _SOLIDCURSOR ) ;

/* Change back to the normal cursor */

_setcursortype ( _NORMALCURSOR ) ;

}

Question:How do I write code that would get error number and display error message if any standard error occurs?

Answer: Following code demonstrates this.

#include

main( )

{

char *errmsg ;

FILE *fp ;

fp = fopen ( "C:\file.txt", "r" ) ;

if ( fp == NULL )

{

errmsg = strerror ( errno ) ;

printf ( "\n%s", errmsg ) ;

}

}

Here, we are trying to open 'file.txt' file. However, if the file does not exist, then it would cause an error. As a result, a value (in this case 2) related to the error generated would get set in errno. errno is an external int variable declared in 'stdlib.h' and also in 'errno.h'. Next, we have called sterror( ) function which takes an error number and returns a pointer to standard error message related to the given error number.


Question:How do I write code to get the current drive as well as set the current drive?

Answer: The function getdisk( ) returns the drive number of current drive. The drive number 0 indicates 'A' as the current drive, 1 as 'B' and so on. The Setdisk( ) function sets the current drive. This function takes one argument which is an integer indicating the drive to be set. Following program demonstrates use of both the functions.

#include

main( )

{

int dno, maxdr ;

dno = getdisk( ) ;

printf ( "\nThe current drive is: %c\n", 65 + dno

) ;

maxdr = setdisk ( 3 ) ;

dno = getdisk( ) ;

printf ( "\nNow the current drive is: %c\n", 65 +

dno ) ;

}


Related Posts


No comments :

 

Aired | The content is copyrighted and may not be reproduced on other websites. | Copyright © 2009-2016 | All Rights Reserved 2016

Contact Us | About Us | Privacy Policy and Disclaimer