Sunday, April 23, 2006

FAQ's of C.. Check out your C Knowledge... Part III

-------------------------------------------------------------------------------------------------

25.
The sizeof( ) function doesn’t return the size of the block of memory pointed to by a pointer. Why?
Ans: 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( ).-------------------------------------------------------------------------------------------------26. FP_SEG And FP_OFF…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. #include
main( ){ unsigned s, o ;char far *ptr = "Hello!" ;
s = FP_SEG ( ptr ) ;o = FP_OFF ( ptr ) ;printf ( "\n%u %u", s, o ) ; }
-------------------------------------------------------------------------------------------------27.
How do I write a program to convert a string containing number in a hexadecimal form to its equivalent decimal?Ans: The following program demonstrates this:main( ){ 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' ; else { if ( str[i] >= 'a' && str[i] <= 'f' ) hex = str[i] - 'a' + 10 ; else if ( str[i] >= 'A' && str[i] <= 'F' ) hex = str[i] - 'A' + 10 ; else 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.-------------------------------------------------------------------------------------------------28. How do I write code that reads the segment register settings? Ans: 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. #include main( ){ struct SREGS s ;segread ( &s ) ;printf ( "\nCS: %X DS: %X SS: %X ES: %X",s.cs, s.ds, s.ss, s.es ) ; }-------------------------------------------------------------------------------------------------29.
What is environment and how do I get environment for a specific entry?
Ans: 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. #include #include
main( ){ char *path = NULL ;
path = getenv ( "PATH" ) ;if ( *path != NULL ) printf ( "\nPath: %s", path ) ; else printf ( "\nPath is not set" ) ; }
-------------------------------------------------------------------------------------------------30.
How do I display current date in the format given below?
Saturday October 12, 2002 Ans: Following program illustrates how we can display date in above given format.
#include #include
main( ){ struct tm *curtime ;time_t dtime ;
char str[30] ;
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.

31.
If we have declared an array as global in one file and we are using it in another file then why doesn't the sizeof operator works on an extern array?
Ans: An extern array is of incomplete type as it does not contain the size. Hence we cannot use sizeof operator, as it cannot get the size of the array declared in another file. To resolve this use any of one the following two solutions: 1. In the same file declare one more variable that holds the size of array. For example,
array.c
int arr[5] ;int arrsz = sizeof ( arr ) ;
myprog.c
extern int arr[] ;extern int arrsz ; 2. Define a macro which can be used in an array declaration. For example,
myheader.h
#define SZ 5
array.c
#include "myheader.h"int arr[SZ] ;
myprog.c
#include "myheader.h"extern int arr[SZ] ;
-------------------------------------------------------------------------------------------------
32.
How do I write printf( ) so that the width of a field can be specified at runtime?
Ans: This is shown in following code snippet.
main( ){ int w, no ;printf ( "Enter number and the width for the number field:" ) ;scanf ( "%d%d", &no, &w ) ;printf ( "%*d", w, no ) ; } Here, an '*' in the format specifier in printf( ) indicates that an int value from the argument list should be used for the field width.-------------------------------------------------------------------------------------------------33.
How to find the row and column dimension of a given 2-D array?
Ans: Whenever we initialize a 2-D array at the same place where it has been declared, it is not necessary to mention the row dimension of an array. The row and column dimensions of such an array can be determined programmatically as shown in following program.
void main( ){ int a[][3] = { 0, 1, 2, 9,-6, 8,7, 5, 44,23, 11,15 } ;
int c = sizeof ( a[0] ) / sizeof ( int ) ;int r = ( sizeof ( a ) / sizeof ( int ) ) / c ;int i, j ;
printf ( "\nRow: %d\nCol: %d\n", r, c ) ;for ( i = 0 ; i < j =" 0">
main( ){ char fname[67] ;
printf ( "\nEnter name of file to open" ) ;gets ( fname ) ;
if ( access ( fname, 0 ) != 0 ){ printf ( "\nFile does not exist." ) ;return ; } }
-------------------------------------------------------------------------------------------------35.
How do I convert a floating-point number to a string?
Ans: Use function gcvt( ) to convert a floating-point number to a string. Following program demonstrates the use of this function.#include
main( ){ char str[25] ;float no ;int dg = 5 ; /* significant digits */
no = 14.3216 ;gcvt ( no, dg, str ) ;printf ( "String: %s\n", str ) ; }36. What is a stack ?Ans: The stack is a region of memory within which our programs temporarily store data as they execute. For example, when a program passes parameters to functions, C places the parameters on the stack. When the function completes, C removes the items from the stack. Similarly, when a function declares local variables, C stores the variable's values on the stack during the function's execution. Depending on the program's use of functions and parameters, the amount of stack space that a program requires will differ.-------------------------------------------------------------------------------------------------

No comments: