Saturday, July 14, 2018

C Questions And Answers – 2018A14

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.

 

-------------------------------------------------------------------------------------------------

 

How to delete a line from text displayed on the screen?

 

Ans:

Sometimes, specially when we are creating a text editor like program we may wish to

allow user to delete a line. We can do so by using two functions namely clreol( ) and delline( ).

 

The clreol( ) function deletes the line from the current cursor position to the end of line. The delline() function deletes the entire line at the current cursor position and moves up the following line.

 

Following program shows how to use these functions.

 

#include <conio.h>

 

main( )

{

int i ;

for ( i = 1 ; i <= 20 ; i++ )

printf ( "This is Line %d\n", i ) ;

getch( ) ;

gotoxy ( 1, 7 ) ;

clreol( ) ;

getch( ) ;

gotoxy ( 1, 12 ) ;

delline( ) ;

getch( ) ;

}

 

-------------------------------------------------------------------------------------------------

 

How do I programmatically insert lines in the text window?

 

Ans:

We can insert a blank line in the text window using the insline( ) function. This function inserts line at current cursor position. While doing so, it shifts down the lines that are below the newly inserted line.

 

#include <conio.h>

 

void main( )

{

printf ( "The little snail was slowly moving up. She wanted\r\n" ) ;

printf ( "to reach the top of the tree. It was chilly\r\n" ) ;

printf ( "winter season. Most of the animals were resting in\r\n" ) ;

printf ( "their nests as there was a heavy snow fall.\r\n" ) ;

printf ( "\r\nPress any key to continue:" ) ;

gotoxy ( 10, 2 ) ;

getch( ) ;

insline( ) ;

getch( ) ;

}

 

-------------------------------------------------------------------------------------------------

 

What will be the output of the following program?

 

main( )

{

unsigned int num ;

int i ;

printf ( "\nEnter any number" ) ;

scanf ( "%u", &num ) ;

for ( i = 0 ; i < 16 ; i++ )

printf ( "%d", ( num << i & 1 << 15 ) ? 1 : 0 ) ;

}

 

Ans:

The output of this program is the binary equivalent of the given number. We have used

bitwise operators to get the binary number.

 

-------------------------------------------------------------------------------------------------

 

If we have declared an array as global in one file and we are using it in another file then why doesn't the sizeof operator works on an extern array?

 

Ans:

An extern array is of incomplete type as it does not contain the size. Hence we cannot use sizeof operator, as it cannot get the size of the array declared in another file. To resolve

this use any of one the following two solutions:

 

1. In the same file declare one more variable that holds the size of array. For example,

 

array.c

int arr[5] ;

int arrsz = sizeof ( arr ) ;

 

myprog.c

extern int arr[] ;

extern int arrsz ;

 

2. Define a macro which can be used in an array declaration. For example,

myheader.h

#define SZ 5

 

array.c

#include "myheader.h"

int arr[SZ] ;

 

myprog.c

#include "myheader.h"

extern int arr[SZ] ;

 

-------------------------------------------------------------------------------------------------

 

How do I write printf( ) so that the width of a field can be specified at runtime?

 

Ans:

This is shown in following code snippet.

 

main( )

{

int w, no ;

printf ( "Enter number and the width for the number field:" ) ;

scanf ( "%d%d", &no, &w ) ;

printf ( "%*d", w, no ) ;

}

 

Here, an '*' in the format specifier in printf( ) indicates that an int value from the argument list should be used for the field width.

 

-------------------------------------------------------------------------------------------------

 

How to find the row and column dimension of a given 2-D array?

 

Ans:

Whenever we initialize a 2-D array at the same place where it has been declared, it is not necessary to mention the row dimension of an array. The row and column dimensions of such an array can be determined programmatically as shown in following program.

 

void main( )

{

int a[][3] = { 0, 1, 2,9,-6, 8,7, 5, 44,23, 11,15 } ;

int c = sizeof ( a[0] ) / sizeof ( int ) ;

int r = ( sizeof ( a ) / sizeof ( int ) ) / c ;

int i, j ;

printf ( "\nRow: %d\nCol: %d\n", r, c ) ;

for ( i = 0 ; i < r ; i++ )

{

for (j=0;j<c;j++ )

printf ( "%d ", a[i][j] ) ;

printf ( "\n" ) ;

}

}

 

-------------------------------------------------------------------------------------------------

 

…till next post, bye-bye & take care.

Friday, July 13, 2018

C Questions And Answers – 2018A13

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.

 

-------------------------------------------------------------------------------------------------

 

How do I use swab( ) in my program ?

 

Ans:

The function swab( ) swaps the adjacent bytes of memory. It copies the bytes from source string to the target string, provided that the number of characters in the source string is even. While copying, it swaps the bytes which are then assigned to the target string.

 

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

main ( )

{

char *str1 = "hS eesll snsiasl not eh es as oher " ;

char *str2 ;

clrscr( ) ;

swab ( str1, str2, strlen ( str1 ) ) ;

printf ( "The target string is : %s\n", str2 ) ;

// output -- She sells snails on the sea shore

getch( ) ;

}

-------------------------------------------------------------------------------------------------

 

Turbo C provides various command line compiler options which we can use through TCC. The compiler options include : displaying specific warning messages, generating 8087 hardware instructions, using a filename for generating assembly code, etc. Instead of compiler options being executed at command line we can use these compiler options in our program. This can be achieved using #pragma options. We can use various flags with #pragma options to use the compiler options. All these flags are available in turbo C's online help.

 

-------------------------------------------------------------------------------------------------

 

I have an array declared in file 'F1.C' as,

int a[ ] = { 1, 2, 3, 4, 5, 6 } ;

and used in the file 'F2.C' as,

extern int a[ ] ;

 

In the file F2.C, why sizeof doesn't work on the array a[ ]?

 

Ans:

An extern array of unspecified size is an incomplete type. You cannot apply sizeof to it, because sizeof operates during compile time and it is unable to learn the size of an array that is defined in another file. You have three ways to resolve this problem:

 

1. In file 'F1.C' define as,

int a[ ] = { 1, 2, 3, 4, 5, 6 } ;

int size_a = sizeof ( a ) ;

and in file F2.C declare as,

extern int a[ ] ;

extern int size_a ;

 

2. In file 'F1.H' define,

#define ARR_SIZ 6

In file F1.C declare as,

#include "F1.H"

int a[ ARR_SIZ ] ;

and in file F2.C declare as,

#include "F1.H"

extern int a[ ARR_SIZ ] ;

 

3. In file 'F1.C' define as,

int a[ ] = { 1, 2, 3, 4, 5, 6, -1 } ;

and in file 'F2.C' declare as,

extern int a[ ] ;

 

Here the element -1 is used as a sentinel value, so the code can understand the end without any explicit size.

 

-------------------------------------------------------------------------------------------------

 

What is environment and how do I get environment for a specific entry?

 

Ans:

While working in DOS, it stores information in a memory region called environment. In

this region we can place configuration settings such as command path, system prompt, etc.

 

Sometimes in a program we need to access the information contained in environment. The function getenv( ) can be used when we want to access environment for a specific entry.

 

Following program demonstrates the use of this function.

 

#include <stdio.h>

#include <stdlib.h>

main( )

{

char *path = NULL ;

path = getenv ( "PATH" ) ;

if ( *path != NULL )

printf ( "\nPath: %s", path ) ;

else

printf ( "\nPath is not set" ) ;

}

 

-------------------------------------------------------------------------------------------------

 

How do I display current date in the format given below?

Saturday October 12, 2002

 

Ans:

Following program illustrates how we can display date in above given format.

 

#include <stdio.h>

#include <time.h>

main( )

{

struct tm *curtime ;

time_t dtime ;

char str[30] ;

time ( &dtime ) ;

curtime = localtime ( &dtime ) ;

strftime ( str, 30, "%A %B %d, %Y", curtime ) ;

printf ( "\n%s", str ) ;

}

 

Here we have called time( ) function which returns current time. This time is returned in terms of seconds, elapsed since 00:00:00 GMT, January 1, 1970. To extract the week day, day of month, etc. from this value we need to break down the value to a tm structure. This is done by the function localtime( ). Then we have called strftime( ) function to format the time and store it in a string str.

 

-------------------------------------------------------------------------------------------------

 

…till next post, bye-bye & take care.

Thursday, July 12, 2018

C Questions And Answers – 2018A12

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.