Sunday, July 8, 2018

C Questions And Answers – 2018A8

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 convert an unsigned long integer value to a string?

 

Ans:

The function ultoa( ) can be used to convert an unsigned long integer value to a string. This function takes three arguments, first the value that is to be converted, second the base address of the buffer in which the converted number has to be stored (with a string

terminating null character '\0') and the last argument specifies the base to be used in converting the value.

 

Following example demonstrates the use of this function.

 

#include <stdlib.h>

 

void main( )

{

unsigned long ul = 3234567231L ;

char str[25] ;

ultoa ( ul, str, 10 ) ;

printf ( "str = %s unsigned long = %lu\n", str, ul ) ;

}

 

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

 

ceil( ) and floor( )

 

The math function ceil( ) takes a double value as an argument. This function finds the smallest possible integer to which the given number can be rounded up. Similarly, floor( )

being a math function, takes a double value as an argument and returns the largest possible integer to which the given double value can be rounded down. The following program demonstrates the use of both the functions.

 

#include <math.h>

 

void main( )

{

double no = 1437.23167 ;

double down, up ;

down = floor ( no ) ;

up = ceil ( no ) ;

printf ( "The original number %7.5lf\n", no ) ;

printf ( "The number rounded down %7.5lf\n", down ) ;

printf ( "The number rounded up %7.5lf\n", up ) ;

}

 

The output of this program would be,

The original number 1437.23167

The number rounded down 1437.00000

The number rounded up 1438.00000

 

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

 

How do I write code to get the current drive as well as set the current drive?

 

Ans:

The function getdisk( ) returns the drive number of current drive. The drive number 0 indicates 'A' as the current drive, 1 as 'B' and so on. The Setdisk( ) function sets the current drive. This function takes one argument which is an integer indicating the drive to be set.

 

Following program demonstrates use of both the functions.

 

#include <dir.h>

 

main( )

{

int dno, maxdr ;

dno = getdisk( ) ;

printf ( "\nThe current drive is: %c\n", 65 + dno ) ;

maxdr = setdisk ( 3 ) ;

dno = getdisk( ) ;

printf ( "\nNow the current drive is: %c\n", 65 + dno ) ;

}

 

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

 

The functions memcmp( ) and memicmp( )

 

The functions memcmp( ) and memicmp( ) compares first n bytes of given two blocks of

memory or strings. However, memcmp( ) performs comparison as unsigned chars whereas memicmp( ) performs comparison as chars but ignores case (i.e. upper or lower case). Both the functions return an integer value where 0 indicates that two memory buffers compared are identical. If the value returned is greater than 0 then it indicates that the first buffer is bigger than the second one. The value less than 0 indicate that the first buffer is less than the second buffer.

 

The following code snippet demonstrates use of both

 

#include <stdio.h>

#include <mem.h>

 

main( )

{

char str1[] = "This string contains some characters" ;

char str2[] = "this string contains" ;

int result ;

result = memcmp ( str1, str2, strlen ( str2 ) ) ;

printf ( "\nResult after comapring buffer using memcmp( )" ) ;

show ( result ) ;

result = memicmp ( str1, str2, strlen ( str2 ) ) ;

printf ( "\nResult after comapring buffer using memicmp( )" ) ;

show ( result ) ;

}

 

show ( int r )

{

if ( r == 0 )

printf ( "\nThe buffer str1 and str2 hold identical data" ) ;

if ( r > 0 )

printf ( "\nThe buffer str1 is bigger than buffer str2" ) ;

if ( r < 0 )

printf ( "\nThe buffer str1 is less than buffer str2" ) ;

}

 

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

 

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

No comments:

Post a Comment