Tuesday, July 17, 2018

C Questions And Answers – 2018A17

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 get the memory size ?

 

Ans:

Consider the following program

 

#include <stdio.h>

#include <bios.h>

main( )

{

int memsize;

memsize = biosmemory( ) ;

printf ( "RAM size = %dK\n",memsize ) ;

return 0 ;

}

 

The function biosmemory uses BIOS interrupt 0x12 to return the size of memory.

 

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

 

Float Format

How does C compiler stores float values?

 

Ans:

In C, the float values are stored in a mantissa and exponent form. While writing a number we specify the exponent part in the form of base 10. But, in case of C compiler, the exponent for floats is stored in the form of base 2. Obviously, because, computer stores the numbers in binary form. The C compiler follows an IEEE standard to store a float. The IEEE format expresses a floating-point number in a binary form known as `normalized' form.

 

Normalization involves adjusting the exponent so that the "binary point" (the binary analog of the decimal point) in the mantissa always lies to the right of most significant nonzero digit. In binary representation, this means that the most significant digit of the mantissa is always a 1.

 

This property of the normalized representation is exploited by the IEEE format when storing the mantissa. Let us consider an example of generating the normalized form of a floating point number. Suppose we want to represent the decimal number 5.375. Its binary equivalent can be obtained as shown below:

 

2 | 5

.375 x 2 = 0.750 0

|------

.750 x 2 = 1.500 1

2 | 2 1

.500 x 2 = 1.000 1

|------

2 | 1 0

|------

| 0 1

Writing remainders in reverse writing whole parts in the same order we get 101 order in which they are obtained we get 011 thus the binary equivalent of 5.375 would be 101.011. The normalized form of this binary number is obtained by adjusting the exponent until the decimal point is to the right of most significant 1. In this case the result is 1.01011 x 22. The IEEE format for floating point storage uses a sign bit, a mantissa and an exponent for representing the power of 2. The sign bit denotes the sign of the number: a 0 represents a positive value and a 1 denotes a negative value. The mantissa is represented in binary. Converting the floating-point number to its normalized form results in a mantissa whose most significant digit is always 1. The IEEE format takes advantage of this by not storing this bit at all. The exponent is an integer stored in unsigned binary format after adding a positive integer bias.

 

This ensures that the stored exponent is always positive. The value of the bias is 127 for

floats and 1023 for doubles. Thus, 1.01011 x 22 is represented as shown below:

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

| 0 | 100 0000 1 | 010 1100 0000 0000 0000 0000 |

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

 

sign bit exponent- mantissa stored in normalized form obtained after adding a bias

127 to exponent 2

 

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

 

How do I use the function ldexp( ) in a program?

 

Ans:

The math function ldexp( ) is used while solving the complex mathematical equations. This function takes two arguments, a double value and an int respectively. The order in which ldexp( ) function performs calculations is ( n * pow ( 2, exp ) ) where n is the double value and exp is the integer.

 

The following program demonstrates the use of this function.

 

#include <stdio.h>

#include <math.h>

 

void main( )

{

double ans ;

double n = 4 ;

ans = ldexp ( n, 2 ) ;

printf ( "\nThe ldexp value is : %lf\n", ans ) ;

}

 

Here, ldexp( ) function would get expanded as ( 4 * 2 * 2 ), and the output would be the ldexp value is : 16.000000

 

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

 

Can we get the mantissa and exponent form of a given number?

 

Ans:

The function frexp( ) splits the given number into a mantissa and exponent form. The function takes two arguments, the number to be converted as a double value and an int to

store the exponent form. The function returns the mantissa part as a double value.

 

Following example demonstrates the use of this function.

 

#include <math.h>

#include <stdio.h>

 

void main( )

{

double mantissa, number ;

int exponent ;

number = 8.0 ;

mantissa = frexp ( number, &exponent ) ;

printf ( "The number %lf is ", number ) ;

printf ( "%lf times two to the ", mantissa ) ;

printf ( "power of %d\n", exponent ) ;

return 0 ;

}

 

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

 

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

No comments:

Post a Comment