C-Interview Questions level 6
Q1) Are the expressions *ptr++ and ++*ptr same?
Answer:- *ptr++ increments the pointer and not the value pointed by it,
whereas ++*ptr increments the value being pointed to by ptr.
,
Answer:- 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( ).
#includemain( ) { 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
Answer:- 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.
#includevoid main( ) { unsigned long ul = 3234567231L ; char str[25] ; ultoa ( ul, str, 10 ) ; printf ( "str = %s unsigned long = %lu\n", str, ul ) ; }
Answer:- 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.
#includevoid 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.23167 The number rounded down 1437.00000 The number rounded up 1438.00000
Answer:- 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.
#includemain( ) { 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 notation val = 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 = 0 string = 34567000 dec = 3 sign = 1 string = 35467 dec = 6 sign = 0
Answer:- We can use the system( ) function to execute the DIR command along with its options.
Following program shows how this can be achieved:
// mydir.c main ( int argc, char *argv[ ] ) { char str[30] ; if ( argc < 2 ) exit ( 0 ) ; sprintf ( str, "dir %s %s", argv[1], argv[2] ) ; system ( str ) ; }
If we run the executable file of this program at command prompt passing the command line arguments as follows:
> mydir abc.c /s This will search the file 'abc.c' in the current directory.
Answer:-
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 ) ; }
Answer:- 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.
#includemain( ) { int i, j ; window ( 20, 8, 60, 17 ) ; for ( i = 0 ; i < 8 ; i++ ) for ( j = 0 ; j < 10 ; j++ ) cprintf ( "Hello!" ) ; }
Answer:-
main( ) { char *pwd ; pwd = getpass ( "Enter Password" ) ; if ( strcmp ( pwd, "orgcity" ) ) printf ( "\nPassword %s is incorrect", pwd ) ; else printf ( "\nCorrect Password" ) ; }
Answer:- We can use the function _getdrive( ) to obtain the current drive. The _getdrive( )
function uses DOS function 0X19 to get the current drive number
#includemain( ) { int disk ; disk = _getdrive( ) + 'A' - 1 ; printf ( "The current drive is: %c\n", disk ) ; }
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.