/* */ Click to Join Live Class with Shankar sir Call 9798158723

C-Interview Questions level 2


Q1) 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.,br>
	
	 void main( )
     { 
     time_t curtime ;
     char ctm[50] ;

     time ( &curtime ) ; //retrieves current time & 
     stores in curtime
     printf ( "\nCurrent Date & Time: %s", ctime ( 
     &curtime ) ) ; 
     } 

	
Q2) How do I change the type of cursor and hide a cursor?
  int x = 3000, y = 2000 ; 
long int z = x * y ;

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. ,br>
	
	 #include 
     main( )
     { 
     /* Hide cursor */
     _setcursortype ( _NOCURSOR ) ;

    /* Change cursor to a solid cursor */
    _setcursortype ( _SOLIDCURSOR ) ;

    /* Change back to the normal cursor */
    _setcursortype ( _NORMALCURSOR ) ; 
     } 

	
Q3) 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 
    #include 
    #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.

Q4) 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 ) ; 
    }

	
Q5) The functions memcmp( ) and memicmp( )

Answer:- : 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
#include 
#include 

main( )
{ 
char str1[] = "This string contains some 
characters" ;
char str2[] = "this string contains" ;
int result ;

result = memcmp ( str1, str2, strlen ( str2 ) ) ;
printf ( "\nResult after comapring buffer using 
memcmp( )" ) ;show ( result ) ;

result = memicmp ( str1, str2, strlen ( str2 ) ) ;
printf ( "\nResult after comapring buffer using 
memicmp( )" ) ;
show ( result ) ; 
} 

show ( int r )
{ 
if ( r == 0 ) 
printf ( "\nThe buffer str1 and str2 hold 
identical data" ) ; 
if ( r > 0 ) 
printf ( "\nThe buffer str1 is bigger than buffer 
str2" ) ; 
if ( r < 0 ) 
printf ( "\nThe buffer str1 is less than buffer 
str2" ) ; 
}
Q6) How do I write code to find an amount of free disk space available on current drive?

Answer:- Use getdfree( ) function as shown in follow code.
      
	    #include 
        #include 
        #include 
        #include 

        main( )
        {
        int dr ; struct dfree disk ;
        long freesp ;

        dr = getdisk( ) ;
        getdfree ( dr + 1 , &disk ) ;

        if ( disk.df_sclus == 0xFFFF )
        {
        printf ( "\ngetdfree( ) function failed\n");
        exit ( 1 ) ;
        }

        freesp = ( long ) disk.df_avail
        * ( long ) disk.df_bsec
        * ( long ) disk.df_sclus ;
        printf ( "\nThe current drive %c: has %ld bytes 
        available as free space\n", 'A' + dr, freesp ) ;
                              
	  
Q7) 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 ; 
} 

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

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.

             char *str = "RGB' ;
             char ch ;
             int color ;
             // code
             ch = str[ color ] ;
             
Q8) Function atexit( ) recevies parameter as the address of function of the type void fun ( void )

Answer:- 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 
  #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" );
  }
Q9) 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’ 
             }        
            
Q10) 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.