C and C++ Employment test questions for freshers, experienced graduates and diploma holders. See more c and c++ resources for technical interview and written test paper preparation
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:
main ( int argc, char *argv[ ] )
char str ;
if ( argc < 2 )
exit ( 0 ) ;
sprintf ( str, “dir %s %s”, argv, argv ) ;
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.
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?
char name ;
int age ;
float salary ;
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 ) ;
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.
int i, j ;
window ( 20, 8, 60, 17 ) ;
for ( i = 0 ; i < 8 ; i++ )
for ( j = 0 ; j < 10 ; j++ )
cprintf ( “Hello!” ) ;
Sometimes you need to prompt the user for a password. When the user types in the password, the characters the user enters should not appear on the screen. A standard library function getpass( ) can be used to perform such function. Maximum number of characters that can be entered as password is 8.
char *pwd ;
pwd = getpass ( “Enter Password” ) ;
if ( strcmp ( pwd, “orgcity” ) )
printf ( “\nPassword %s is incorrect”, pwd ) ;
printf ( “\nCorrect Password” ) ;
How to obtain the current drive through C ?
Ans: We can use the function _getdrive( ) to obtain the current drive. The _getdrive( ) function uses DOS function 0X19 to get the current drive number
int disk ;
disk = _getdrive( ) + ‘A’ – 1 ;
printf ( “The current drive is: %c\n”, disk ) ;