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

C-Interview Questions level 6


Q1) Are the expressions *ptr++ and ++*ptr same?

Answer:- *ptr++ increments the pointer and not the value pointed by it, whereas ++*ptr increments the value being pointed to by ptr. ,
Q2) strpbrk( )

Answer:- The function strpbrk( ) takes two strings as parameters. It scans the first string, to find, the first occurrence of any character appearing in the second string. The function returns a pointer to the first occurrence of the character it found in the first string. The following program demonstrates the use of string function strpbrk( ).
#include 
      main( )
      { 
      char *str1 = "Hello!" ; 
      char *str2 = "Better" ; 
      char *p ; 
      p = strpbrk ( str1, str2 ) ; 

      if ( p )  
      printf ( "The first character found in str1 is %c", *p ) ;  
      else  
      printf ( "The character not found" ) ;  
      }

The output of the above program would be the first character found in str1 is e

Q3) Can we convert an unsigned long integer value to a string?

Answer:- The function ultoa( ) can be used to convert an unsigned long integer value to a string. This function takes three arguments, first the value that is to be converted, second the base address of the buffer in which the converted number has to be stored (with a string terminating null character '\0') and the last argument specifies the base to be used in converting the value. Following example demonstrates the use of this function.
      #include 
      void main( )
      { 
      unsigned long ul = 3234567231L ;
      char str[25] ;

      ultoa ( ul, str, 10 ) ;
      printf ( "str = %s unsigned long = %lu\n", str, ul ) ; 
      }                 
Q4) ceil( ) and floor( )

Answer:- The math function ceil( ) takes a double value as an argument. This function finds the smallest possible integer to which the given number can be rounded up. Similarly, floor( ) being a math function, takes a double value as an argument and returns the largest possible integer to which the given double value can be rounded down. The following program demonstrates the use of both the functions.
      #include 
      void main( )
      {
      double no = 1437.23167 ;
      double down, up ;

      down = floor ( no ) ;
      up = ceil ( no ) ;

      printf ( "The original number %7.5lf\n", no ) ;
      printf ( "The number rounded down %7.5lf\n", down ) ;
      printf ( "The number rounded up %7.5lf\n", up ) ;
      }
       
      The output of this program would be,
      The original number            1437.23167
      The number rounded down  1437.00000
      The number rounded up      1438.00000

             
Q5) How do I use function ecvt( ) in a program?

Answer:- The function ecvt( ) converts a floating-point value to a null terminated string. This function takes four arguments, such as, the value to be converted to string, the number of digits to be converted to string, and two integer pointers. The two-integer pointer stores the position of the decimal point (relative to the string) and the sign of the number, respectively. If the value in a variable, used to store sign is 0, then the number is positive and, if it is non-zero, then the number is negative. The function returns a pointer to the string containing digits. Following program demonstrates the use of this function.
      #include 
      main( )
      { 
      char *str ;
      double val ;
      int dec, sign ;
      int ndig = 4 ;

      val = 22 ;
      str = ecvt ( val, ndig, &dec, &sign ) ;
      printf ( "string = %s dec = %d sign = %d\n", str, dec, sign ) ;

      val = -345.67 ;
      ndig = 8 ;
      str = ecvt ( val, ndig, &dec, &sign ) ;
      printf ( "string = %s dec = %d sign = %d\n", str, dec, sign ) ;

      // number with a scientific notation
      val = 3.546712e5 ;
      ndig = 5 ;
      str = ecvt ( val, ndig, &dec, &sign ) ;
      printf ( "string = %s dec = %d sign = %d\n", str, dec, sign ) ; 
      }

      The output of this program would be 
        
      string = 2200 dec = 2 sign = 0
      string = 34567000 dec = 3 sign = 1
      string = 35467 dec = 6 sign = 0 
Q6) How to run DIR command programmatically?

Answer:- We can use the system( ) function to execute the DIR command along with its options. Following program shows how this can be achieved:
      // mydir.c
       
      main ( int argc, char *argv[ ] )
      {
      char str[30] ;
       
      if ( argc < 2 )
      exit ( 0 ) ;
       
      sprintf ( str, "dir %s %s", argv[1], argv[2] ) ;
      system ( str ) ;
      }
       
      

If we run the executable file of this program at command prompt passing the command line arguments as follows:

      > mydir abc.c /s
       
      This will search the file 'abc.c' in the current directory.
      
Q7) Suppose I have a structure having fields name, age, salary and have passed address of age to a function fun( ). How I can access the other member of the structure using the address of age?

Answer:-
      struct emp 
      { 
      char name[20] ; 
      int age ; 
      float salary ; 
      } ; 
      main( ) 
      { 
      struct emp e ; 
      printf ( "\nEnter name: " ) ; 
      scanf ( "%s", e.name ) ; 
      printf ( "\nEnter age: " ) ; 
      scanf ( "%d", &e.age ) ; 
      printf ( "\nEnter salary: " ) ; 
      scanf ( "%f", &e.salary ) ; 
      fun ( &e.age ) ; 
      } 
      fun ( int *p ) 
      { 
      struct emp *q ; 
      int offset ; 
      offset = ( char * ) ( & ( ( struct emp * ) 0 ) -> age ) - ( char * ) ( ( 
      struct emp* ) 0 ) ; 
      q = ( struct emp * ) ( ( char * ) p - offset ) ; 
      printf ( "\nname: %s", q -> name ) ; 
      printf ( "\nage: %d", q -> age ) ; 
      printf ( "\nsalary: %f", q -> salary ) ; 
      }
 
Q8) How to restrict the program's output to a specific screen region?

Answer:- A C function window( ) can be used to restrict the screen output to a specific region. The window( ) function defines a text-mode window. The parameters passed to this function defines the upper-left and lower-right corner of the region within which you want the output. In the following program, the string 'Hello!' gets printed within the specified region. To print the string we must use cprintf( ) function which prints directly on the text-mode window.
      #include 
      main( )
      {
      int i, j ;
       
      window ( 20, 8, 60, 17 ) ;
      for ( i = 0 ; i < 8 ; i++ )
      for ( j = 0 ; j < 10 ; j++ )
      cprintf ( "Hello!" ) ;
      }
 
Q9) Sometimes you need to prompt the user for a password. When the user types in the password, the characters the user enters should not appear on the screen. A standard library function getpass( ) can be used to perform such function. Maximum number of characters that can be entered as password is 8.

Answer:-
      main( )
      {
      char *pwd ;
       
      pwd = getpass ( "Enter Password" ) ;
       
      if ( strcmp ( pwd, "orgcity" ) )
      printf ( "\nPassword %s is incorrect", pwd ) ;
      else
      printf ( "\nCorrect Password" ) ;
      }
    
Q10) How to obtain the current drive through C ?

Answer:- We can use the function _getdrive( ) to obtain the current drive. The _getdrive( ) function uses DOS function 0X19 to get the current drive number
      #include 
      main( )
      {
      int disk ;
      disk = _getdrive( ) + 'A' - 1 ;
      printf ( "The current drive is: %c\n", disk ) ;
      }