There are many commonly asked questions regarding C programming language. Below are some collected such question-answer examples. The questions are usually related with Turbo C IDE in windows or GCC under Linux environment [not always].
For more such examples, click C_Q&A label.
-------------------------------------------------------------------------------------------------
Can we get the x and y coordinate of the current cursor position ?
Ans :
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 ( "Just\n To\n Test\n Where\n the cursor\n goes" ) ;
printf ( "Current location is X: %d Y: %d\n", wherex( ), wherey( ) ) ;
}
-------------------------------------------------------------------------------------------------
How do I programmatically delete lines in the text window?
Ans:
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 %d\r\n", i ) ;
printf ( "Press a key to continue : " ) ;
getch( ) ;
gotoxy ( 2, 6 ) ;
for ( i = 6; i <= 12; i++ )
delline( ) ;
getch( ) ;
}
-------------------------------------------------------------------------------------------------
How do I get the time elapsed between two function calls ?
Ans:
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.
#include <time.h>
#include <stdio.h>
#include <dos.h>
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.
-------------------------------------------------------------------------------------------------
FP_SEG And FP_OFF…
Sometimes while working with far pointers we need to break a far address into its segment and offset. In such situations we can use FP_SEG and FP_OFF macros.
Following program illustrates the use of these two macros.
#include <dos.h>
main( )
{
unsigned s, o ;
char far *ptr = "Hello!" ;
s = FP_SEG ( ptr ) ;
o = FP_OFF ( ptr ) ;
printf ( "\n%u %u", s, o ) ;
}
-------------------------------------------------------------------------------------------------
How do I write a program to convert a string containing number in a hexadecimal form to its equivalent decimal?
Ans:
The following program demonstrates this:
main( )
{
char str[] = "0AB" ;
int h, hex, i, n ;
n = 0 ; h = 1 ;
for ( i = 0 ; h == 1 ; i++ )
{
if ( str[i] >= '0' && str[i] <= '9' )
hex = str[i] - '0' ;
else
{
if ( str[i] >= 'a' && str[i] <= 'f' )
hex = str[i] - 'a' + 10 ;
else if ( str[i] >= 'A' && str[i] <= 'F' )
hex = str[i] - 'A' + 10 ;
else
h = 0 ;
}
if ( h == 1 )
n = 16 * n + hex ;
}
printf ( "\nThe decimal equivalent of %s is %d",str, n ) ;
}
The output of this program would be the decimal equivalent of 0AB is 171.
-------------------------------------------------------------------------------------------------
How do I write code that reads the segment register settings?
Ans:
We can use segread( ) function to read segment register settings. There are four segment registers—code segment, data segment, stack segment and extra segment.
Sometimes when we use DOS and BIOS services in a program we need to know the segment register's value. In such a situation we can use segread( ) function.
The following program illustrates the use of this function.
#include <dos.h>
main( )
{
struct SREGS s ;
segread ( &s ) ;
printf ( "\nCS: %X DS: %X SS: %X ES: %X", s.cs,s.ds, s. ss, s.es ) ;
}
-------------------------------------------------------------------------------------------------
…till next post, bye-bye & take care.
No comments:
Post a Comment