Sunday, April 23, 2006

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

-------------------------------------------------------------------------------------------------13.
How do I write code that would get error number and display error message if any standard error occurs?
Ans: 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.-------------------------------------------------------------------------------------------------14.
How do I write code to get the current drive as well as set the current drive?
Ans: 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 ) ; }-------------------------------------------------------------------------------------------------15.
The functions memcmp( ) and memicmp( )
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 <>#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 ) ;}
17.
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 ; } 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 ;// codech = str[ color ] ;-------------------------------------------------------------------------------------------------18.
Function atexit( ) recevies parameter as the address of function of the type void fun ( void ). 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" );}
-------------------------------------------------------------------------------------------------
19.
How do I write a user-defined function, which deletes each character in a string str1, which matches any character in string str2?
Ans: 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’ }-------------------------------------------------------------------------------------------------
20.
How does free( ) know how many bytes to free?
Ans: 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.-------------------------------------------------------------------------------------------------21.
What is the use of randomize( ) and srand( ) function?
Ans: While generating random numbers in a program, sometimes we require to control the series of numbers that random number generator creates. The process of assigning the random number generators starting number is called seeding the generator. The randomize( ) and srand( ) functions are used to seed the random number generators. The randomize( ) function uses PC's clock to produce a random seed, whereas the srand( ) function allows us to specify the random number generator's starting value.
-------------------------------------------------------------------------------------------------22.
How do I determine amount of memory currently available for allocating?
Ans: We can use function coreleft( ) to get the amount of memory available for allocation. However, this function does not give an exact amount of unused memory. If, we are using a small memory model, coreleft( ) returns the amount of unused memory between the top of the heap and stack. If we are using a larger model, this function returns the amount of memory between the highest allocated memory and the end of conventional memory. The function returns amount of memory in terms of bytes.
-------------------------------------------------------------------------------------------------23.
How does a C program come to know about command line arguments?
Ans: When we execute our C program, operating system loads the program into memory. In case of DOS, it first loads 256 bytes into memory, called program segment prefix. This contains file table, environment segment, and command line information. When we compile the C program the compiler inserts additional code that parses the command, assigning it to the argv array, making the arguments easily accessible within our C program.
24.
When we open a file, how does functions like fread( )/fwrite( ), etc. get to know from where to read or to write the data?Ans: When we open a file for read/write operation using function like fopen( ), it returns a pointer to the structure of type FILE. This structure stores the file pointer called position pointer, which keeps track of current location within the file. On opening file for read/write operation, the file pointer is set to the start of the file. Each time we read/write a character, the position pointer advances one character. If we read one line of text at a step from the file, then file pointer advances to the start of the next line. If the file is opened in append mode, the file pointer is placed at the very end of the file. Using fseek( ) function we can set the file pointer to some other place within the file.

No comments: