Monday, July 9, 2018

C Questions And Answers – 2018A9

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 function ecvt( ) in a program?

 

Ans:

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 nonzero, then the number is negative. The function returns a pointer to the string containing digits.

 

Following program demonstrates the use of this function.

 

#include <stdlib.h>

main( )

{

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

 

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

 

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:

 

// 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.

 

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

 

How do I write code to find an amount of free disk space available on current drive?

 

Ans:

Use getdfree( ) function as shown in follow code.

 

#include <stdio.h>

#include <stdlib.h>

#include <dir.h>

#include <dos.h>

 

main( )

{

int dr ; struct dfree disk ;

long freesp ;

dr = getdisk( ) ;

getdfree ( dr + 1 , &disk ) ;

 

if ( disk.df_sclus == 0xFFFF )

{

printf ( "\ngetdfree( ) function failed\n");

exit ( 1 ) ;

}

 

freesp = ( long )disk.df_avail*( long ) disk.df_bsec*( long ) disk.df_sclus ;

printf ( "\nThe current drive %c: has %ld bytes available as free space\n",        'A' + dr, freesp ) ;

}

 

 

Use of array indices...

 

If we wish to store a character in a char variable ch and the character to be stored depends on the value of another variable say color (of type int), then the code would be as shown below:

 

switch ( color )

{

case 0 : ch = 'R' ;break ;

case 1 : ch = 'G' ;break ;

case 2 : ch = 'B' ;break ;

}

 

In place of switch-case we can make use of the value in color as an index for a character array. How to do this is shown in following code snippet.

 

char *str = "RGB' ;

char ch ;

int color ;

// code

ch = str[ color ] ;

 

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

 

Function atexit( ) receives parameter as the address of function of the type void fun(void).

 

The function whose address is passed to atexit( ) gets called before the termination of program. If atexit( ) is called for more than one function then the functions are called in "first in last out" order. You can verify that from the output.

 

#include <stdio.h>

#include <stdlib.h>

 

void fun1( )

{

printf("Inside fun1\n");

}

 

void fun2( )

{

printf("Inside fun2\n");

}

 

main( )

{

atexit ( fun1 ) ;

/* some code */

atexit ( fun2 ) ;

printf ( "This is the last statement of program?\n" );

}

 

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

 

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

No comments:

Post a Comment