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

C-Interview Questions level 10


Q1) What does the error "Null Pointer Assignment" mean and what causes this error?

Answer:- The Null Pointer Assignment error is generated only in small and medium memory models. This error occurs in programs which attempt to change the bottom of the data segment. In Borland's C or C++ compilers, Borland places four zero bytes at the bottom of the data segment, followed by the Borland copyright notice "Borland C++ - Copyright 1991 Borland Intl.". In the small and medium memory models, a null pointer points to DS:0000. Thus assigning a value to the memory referenced by this pointer will overwrite the first zero byte in the data segment. At program termination, the four zeros and the copyright banner are checked. If either has been modified, then the Null Pointer Assignment error is generated. Note that the pointer may not truly be null, but may be a wild pointer that references these key areas in the data segment. Data Structures How to build an expression trees ? Ans: An expression tree is a binary tree which is built from simple operands and operators of an (arithmetic or logical ) expression by placing simple operands as the leaves of a binary tree and the operators as the interior nodes. If an operator is binary , then it has two nonempty subtrees, that are its left and right operands (either simple operands or sub expressions). If an operator is unary, then only one of its subtrees is nonempty, the one on the left or right according as the operator is written on the right or left of its operand. We traditionally write some unary operators to the left of their operands, such as "-" ( unary negation) or the standard functions like log( ), sin( ) etc. Others are written on the right, such as the factorial function ()!. If the operator is written on the left, then in the expression tree we take its left subtree as empty. If it appears on the right, then its right subtree will be empty. An example of an expression tree is shown below for the expression ( -a < b ) or ( c + d ) .
Q2) Can we get the remainder of a floating point division ?

Answer:- Yes. Although the % operator fails to work on float numbers we can still get the remainder of floating point division by using a function fmod( ). The fmod( ) function divides the two float numbers passed to it as parameters and returns the remainder as a floating-point value. Following program shows fmod( ) function at work.
      #include 
       
      main( )
      {
      printf ( "%f", fmod ( 5.15, 3.0 ) ) ;
      }
       
The above code snippet would give the output as 2.150000.
Q3) How to extract the integer part and a fractional part of a floating point number?

Answer:- C function modf( ) can be used to get the integer and fractional part of a floating point.
 
      #include "math.h"
       
      main( )
      {
      double val, i, f ;
      val = 5.15 ;
      f = modf ( val, &i ) ;
      printf ( "\nFor the value %f integer part = %f and fractional part = %f", 
      val, i, f ) ;
      }
       
      The output of the above program will be:
       
      For the value 5.150000 integer part = 5.000000 and fractional part = 
      0.150000
      
Q4) How do I define a pointer to a function which returns a char pointer?

Answer:- char * ( *p )( ) ; or typedef char * ( * ptrtofun )( ) ; ptrtofun p ; Here is a sample program which uses this definition. main( ) { typedef char * ( * ptrtofun ) ( ) ; char * fun( ) ; ptrtofun fptr ; char *cptr ; fptr = fun ; cptr = (*fptr) ( ) ; printf ( "\nReturned string is \"%s\"", cptr ) ; } char * fun( ) { static char s[ ] = "Hello!" ; printf ( "\n%s", s ) ; return s ; }
Q5) What's wrong with the following declaration: char* ptr1, ptr2 ; get errors when I try to use ptr2 as a pointer.

Answer:-
    * applies only to ptr1 and not to ptr2. Hence ptr1 is getting declared as a char pointer, whereas, ptr2 is being declared merely as a char. This can be rectified in two ways : 
        char *ptr1, *ptr2 ; 
        typedef char* CHARPTR ; CHARPTR ptr1, ptr2 ;
  
Q6) How to use scanf( ) to read the date in the form of dd-mm-yy?

Answer:- To read the date in the form of dd-mm-yy one possible way is, int dd, mm, yy ; char ch ; /* for char '-' */ printf ( "\nEnter the date in the form of dd-mm-yy : " ) ; scanf( "%d%c%d%c%d", &dd, &ch, &mm, &ch, &yy ) ; Another way is to use suppression character * is as follows: int dd, mm, yy ; scanf( "%d%*c%d%*c%d", &dd, &mm, &yy ) ; The suppression character '*' suppresses the input read from the standard input buffer for the assigned control character.
Q7) Why the output of sizeof ( 'a' ) is 2 and not 1 ?

Answer:- Character constants in C are of type int, hence sizeof ( 'a' ) is equivalent to sizeof ( int ), i.e. 2. Hence the output comes out to be 2 bytes.
Q8) Can we use scanf( ) function to scan a multiple words string through keyboard?

Answer:- Yes. Although we usually use scanf( ) function to receive a single word string and gets( ) to receive a multi-word string from keyboard we can also use scanf( ) function for scanning a multi-word string from keyboard. Following program shows how to achieve this. main( ) { char buff[15] ; scanf ( "%[^\n]s", buff ) ; puts ( buff ) ; } In the scanf( ) function we can specify the delimiter in brackets after the ^ character. We have specified '\n' as the delimiter. Hence scanf( ) terminates only when the user hits Enter key.
Q9)How to set the system date through a C program ?

Answer:- We can set the system date using the setdate( ) function as shown in the following program. The function assigns the current time to a structure date.
 
      #include "stdio.h"
      #include "dos.h"
       
      main( )
      {
      struct date new_date ;
       
      new_date.da_mon = 10 ;
      new_date.da_day = 14 ;
      new_date.da_year = 1993 ;
       
      setdate ( &new_date ) ;
      }
    
Q10) How can I write a general-purpose swap without using templates?

Answer:- Given below is the program which uses the stringizing preprocessor directive ## for building a general purpose swap macro which can swap two integers, two floats, two chars, etc.
      #define swap( a, b, t ) ( g ## t = ( a ), ( a ) = ( b ), ( b ) = g ## t )
      
      int gint;
      char gchar;
      float gfloat ;
      main( )
      {
          int a = 10, b = 20 ;
          char ch1 = 'a' , ch2 = 'b' ;
          float f1 = 1.12, f2 = 3.14 ;
          swap ( a, b, int ) ;
          printf ( "\na = %d b = %d", a, b ) ;
          swap ( ch1, ch2, char ) ;
          printf ( "\nch1 = %c ch2 = %c", ch1, ch2 ) ;
          swap ( f1, f2, float ) ;
          printf ( "\nf1 = %4.2f f2 = %4.2f", f1, f2 ) ;
      }
      swap ( a, b, int ) would expand to,
      ( gint = ( a ), ( a ) = ( b ), ( b ) = gint )