C interview question:The sizeof( ) function doesn’t return the size of the block of memory pointed to by a pointer. Why?
Answer:The sizeof( ) operator does not know that malloc( ) has been used to allocate a pointer. sizeof( ) gives us the size of pointer itself. There is no handy way to find out the size of a block allocated by malloc( ).
C interview qustion: What is FP_SEG And FP_OFF…
Answer:Sometimes while working with far pointers we need to break a far address into its segment and offset. In such situations we can use FP_SEG and FP_OFF macros. Following program illustrates the use of these two macros.
unsigned s, o ;
char far *ptr = "Hello!" ;
s = FP_SEG ( ptr ) ;
o = FP_OFF ( ptr ) ;
printf ( "\n%u %u", s, o ) ;
C interview question:How do I write a program to convert a string containing number in a hexadecimal form to its equivalent decimal?
Answer: The following program demonstrates this:
char str = "0AB" ;
int h, hex, i, n ;
n = 0 ; h = 1 ;
for ( i = 0 ; h == 1 ; i++ )
if ( str[i] >= '0' && str[i] <= '9' )
hex = str[i] - '0' ;
if ( str[i] >= 'a' && str[i] <= 'f' )
hex = str[i] - 'a' + 10 ;
if ( str[i] >= 'A' && str[i] <= 'F' )
hex = str[i] - 'A' + 10 ;
h = 0 ;
if ( h == 1 )
n = 16 * n + hex ;
printf ( "\nThe decimal equivalent of %s is %d",
str, n ) ;
The output of this program would be the decimal equivalent of 0AB is 171.
C interview question:How do I write code that reads the segment register settings?
Answer: We can use segread( ) function to read segment register settings. There are four segment registers—code segment, data segment, stack segment and extra segment. Sometimes when we use DOS and BIOS services in a program we need to know the segment register's value. In such a situation we can use segread( ) function. The following program illustrates the use of this function.
struct SREGS s ;
segread ( &s ) ;
printf ( "\nCS: %X DS: %X SS: %X ES: %X",s.cs,
s.ds, s.ss, s.es ) ;
C interview question:What is environment and how do I get environment for a specific entry?
Answer: While working in DOS, it stores information in a memory region called environment. In this region we can place configuration settings such as command path, system prompt, etc. Sometimes in a program we need to access the information contained in environment. The function getenv( ) can be used when we want to access environment for a specific entry. Following program demonstrates the use of this function.
char *path = NULL ;
path = getenv ( "PATH" ) ;
if ( *path != NULL )
printf ( "\nPath: %s", path ) ;
printf ( "\nPath is not set" ) ;
C interview question:How do I display current date in the format given below?
Saturday October 12, 2002
Answer: Following program illustrates how we can display date in above given format.
struct tm *curtime ;
time_t dtime ;
char str ;
time ( &dtime ) ;
curtime = localtime ( &dtime ) ;
strftime ( str, 30, "%A %B %d, %Y", curtime ) ;
printf ( "\n%s", str ) ;
Here we have called time( ) function which returns current time. This time is returned in terms of seconds, elapsed since 00:00:00 GMT, January 1, 1970. To extract the week day, day of month, etc.from this value we need to break down the value to a tm structure. This is done by the function localtime( ). Then we have called strftime( ) function to format the time and store it in a string str.