Language in C Interview Questions and Answers

Ques 11. How do I change the type of cursor and hide a cursor?

Ans. 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.

Ques 12. How do I write code that would get error number and display error message if any standard error occurs?

Ans. Following code demonstrates this.

#include <stdio.h>#include <stdlib.h>#include <errno.h>

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.

Is it helpful?
YesNoAdd CommentView Comments

Ques 13. How do I write code to get the current drive as well as set the current drive?

Ans. 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.

Ques 14. The functions memcmp( ) and memicmp( )

Ans. The functions memcmp( ) and memicmp( ) compares first n bytes of given two blocks of memory or strings. However, memcmp( ) performs comparison as unsigned chars whereas memicmp( ) performs comparison as chars but ignores case (i.e. upper or lower case). Both the functions return an integer value where 0 indicates that two memory buffers compared are identical. If the value returned is greater than 0 then it indicates that the first buffer is bigger than the second one. The value less than 0 indicate that the first buffer is less than the second buffer. The following code snippet demonstrates use of both

Use of array indices...If we wish to store a character in a char variable ch and the character to be stored depends on the value of another variable say color (of type int), then the code would be as shown below:

switch ( color ){case 0 :ch = 'R' ;break ;case 1 :ch = 'G' ;break ;case 2 :ch = 'B' ;break ;}In place of switch-case we can make use of the value in color as an index for a character array. How to do this is shown in following code snippet.