Ques 56. How to obtain the current drive through C ?
We can use the function _getdrive( ) to obtain the current drive. The _getdrive( ) function uses DOS function 0X19 to get the current drive number
#include <direct.h> main( ) { int disk ; disk = _getdrive( ) + 'A' - 1 ; printf ( "The current drive is: %cn", disk ) ; }
Is it helpful?
Add CommentView Comments
Ques 57. How come the output for both the programs is different when the logic is same?
main( ) { int i, j ;
for ( i = 1, j = 1 ; i <= 5, j <= 100 ; i++, j++ ) { gotoxy ( 1, 1, ) ; printf ( "%d %d", i, j ) ; } }
main( ) { int i, j ;
for ( i =1, j = 1; j <= 100, i <= 5; i++, j++ ) { gotoxy ( 1, 1 ) ; printf ( "%d %d", i, j ) ; } }
Output -> 5 5
Even if logic of both the programs is same the output of the first program comes out to be 100, 100, but of the second program it is 5, 5. The comma operator plays a vital role inside the for loop. It always considers the value of the latest variable. So, at the time of testing the condition in for loop, the value of j will be considered in the first program and value of i in the second.
Is it helpful?
Add CommentView Comments
Ques 58. Can we get the x and y coordinate of the current cursor position ?
The function wherex( ) and wherey( ) returns the x-coordinate and y-coordinate of the current cursor position respectively. Both the functions return an integer value. The value returned by wherex( ) is the horizontal position of cursor and the value returned by wherey( ) is the vertical position of the cursor. Following program shows how to use the wherex( ) and wherey( ) functions.
#include <conio.h> main( ) { printf ( "Justn Ton Testn Wheren the cursorn goes" ) ;
Ques 59. How do I programmatically delete lines in the text window?
While writing programs that perform screen-based I/O, you may want to-delete the current line's contents, moving one line up, all of the output that follows. In such cases a function called delline( ) can be used. Following code snippet illustrates the use of function delline( ).
#include <conio.h> main( ) { int i ; clrscr( ) ;
for ( i = 0; i <= 23; i++ ) printf ( "Line %drn", i ) ;
printf ( "Press a key to continue : " ) ; getch( ) ;
gotoxy ( 2, 6 ) ;
for ( i = 6; i <= 12; i++ ) delline( ) ;
getch( ) ; }
Is it helpful?
Add CommentView Comments
Ques 60. How do I get the time elapsed between two function calls ?
The function difftime( ) finds the difference between two times. It calculates the elapsed time in seconds and returns the difference between two times as a double value.
main( ) { int a[] = { 2, -34, 56, 78, 112, 33, -7, 11, 45, 29, 6 } ; int s ; time_t t1, t2 ; // time_t defines the value used for time function
s = sizeof ( a ) / 2 ; t1 = time ( NULL ) ; sel_sort ( a, s ) ; // sort array by selection sort bub_sort ( a, s ) ; // sort array by bubble sort method t2 = time ( NULL ) ; printf ( "nThe difference between two function calls is %f", difftime ( t2, t1 ) ) ; }
In the above program we have called difftime( ) function that returns the time elapsed from t1 to t2.