Sunday, April 23, 2006

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

-------------------------------------------------------------------------------------------------49.
How do I use scanf( ) to read the date in the form 'dd-mm-yy' ?Ans: There are two ways 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 ) ;
And another best way is to use suppression character * as...
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.-------------------------------------------------------------------------------------------------50.
How do I print a floating-point number with higher precision say 23.34568734 with only precision up to two decimal places?
Ans: This can be achieved through the use of suppression char '*' in the format string of printf( ) as shown in the following program. main( ){ int i = 2 ;float f = 23.34568734 ;printf ( "%.*f", i, f ) ; }The output of the above program would be 23.35.
-------------------------------------------------------------------------------------------------51. Are the expressions *ptr++ and ++*ptr same?
Ans: No. *ptr++ increments the pointer and not the value pointed by it, whereas ++*ptr increments the value being pointed to by ptr. -------------------------------------------------------------------------------------------------
52. strpbrk( )
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
53.
Can we convert an unsigned long integer value to a string?
Ans: 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 ) ; }-------------------------------------------------------------------------------------------------54. ceil( ) and floor( )
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.23167The number rounded down 1437.00000The number rounded up 1438.00000-------------------------------------------------------------------------------------------------55.
How do I use function ecvt( ) in a program?
Ans: 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 notationval = 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 = 0string = 34567000 dec = 3 sign = 1string = 35467 dec = 6 sign = 0 -------------------------------------------------------------------------------------------------
56.
How to run DIR command programmatically?Ans: We can use the system( ) function to execute the DIR command along with its options. Following program shows how this can be achieved:// mydir.cmain ( int argc, char *argv[ ] ){char str[30] ;if ( argc <> mydir abc.c /sThis will search the file 'abc.c' in the current directory.-------------------------------------------------------------------------------------------------57.
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?Ans: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 ) ; }
58.
How to restrict the program's output to a specific screen region?Ans: 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 < j =" 0" pwd =" getpass">main( ){int disk ;disk = _getdrive( ) + 'A' - 1 ;printf ( "The current drive is: %c\n", disk ) ;}-------------------------------------------------------------------------------------------------

No comments: