Logo F2FInterview

C Advanced Interview Questions

Q   |   QA
‹‹ previous123

Float numbers are stored in exponential form i.e.

(Mantissa)*10^ (Exponent)

Here * indicates multiplication and ^ indicates power.
In memory only Mantissa and Exponent is stored not *, 10 and ^.

Total size of float data type: 32 bit
Those bits are used in following manner:

Exponent bit: 8
Mantissa bit: 24
Mantissa is signed number, so 24 bit are used as:
Mantissa_sign bit: 1
Mantisaa_data bit: 23

For only mantissa:
Mantissa_sign bit will zero if number is positive and Mantissa_sign bit will one if number is negative.
Exponent is also signed number, So 8 bit are used as:

Exponent_sign bit: 1
Exponent_data bit: 7

Following figure illustrate how floating point number is stored in memory.

 

Five important rules:

Rule 1: To find the mantissa and exponent, we convert data into scientific form.

Rule 2: Before the storing of exponent, 127 is added to exponent.

Rule 3: Exponent is stored in memory in first byte from right to left side.

Rule 4: If exponent will negative number it will be stored in 2’s complement form.

Rule 5: Mantissa is stored in the memory in second byte onward from right to left side. Example:

Memory representation of:

float a = -10.3f;

For this you have to follow following steps:

step1: convert the number (10.3) into binary form
Binary value of 10.3 is: 1010.0100110011001100110011001100110011…

step2: convert the above binary number in the scientific form. Scientific form of 1010.0100110011001100110011001100110011…=
1.01001001100110011001100 11001100110011…*10^3
Note: First digit i.e. 1, decimal point symbol, base of power i.e. 10, power symbol ^ and multiplication symbol * are not stored in the memory.

Step3: find exponent and mantissa and signed bit
Mantissa_data bit in binary = 0100100 11001100 11001101 (Only first 23 bit from left side)

Mantissa_sign bit: 1 (Since it is a negative number)
Exponent in decimal: 3

Question:
Why we have taken right most bit of mantissa_data bit one instead of zero?

Step5: Add 127 in the exponent and convert in the binary number form.

(Why 127? since size of exponent_data bit is 7 and maximum possible number in seven bit will 1111111 in binary or 127 in decimal)

Exponent= 127+3=130
Binary value of 130 in eight bit: 1000001 0
Exponent_data bit: 1000001 (Take first seven bit from left side)
Exponent_sign bit: 0 (Take rightmost bit)

Step6: Now store the Mantissa_data bit, Mantissa_sign bit, Exponent_data bit and Exponent_sign bit at appropriate location as shown in the following figure.

Note: Mantissa_data bits are stored from left to right while Exponent_data bits are stored from right to left.

How to check above memory representation is correct?

Answer:
We will take one char pointer and visit each byte of a float number and observe the output.

#include<stdio.h>
int main(){
int i;
float f=-10.3f;
char *p=(char *)&f;
for(i=0;i<4;i++)
printf("%d ",*p++);
return 0;
}

Output: -51 -52 36 -63
Explanation:
Binary value of -51 in eight bit: 11001101
Binary value of -52 in eight bit: 11001100
Binary value of 36 in eight bit: 00100100
Binary value of -63 in eight bit: 11000001

This is exactly same as which we have represented in memory in the above figure. 

#include<dos.h>
#include<stdio.h>
      int main(){ 
      union REGS i,o;
      i.h.ah=2; //positioning the cursor
      i.h.bh=0;
      i.h.dh=30;
      i.h.dl=45;
      int86(0x10,&i,&o);
      printf("World");
      return 0;
} 

Pragma is implementation specific directive i.e each pragma directive has different implementation rule and use. There are many type of pragma directive and varies from one compiler to another compiler .If compiler does not recognize particular pragma the it simply ignore that pragma statement without showing any error or warning message and execute the whole program assuming this pragma statement is not present.
For example suppose there is any pragma directive is #pragma world.

#include<stdio.h>
#pragma world
int main(){
    printf("C is powerful language ");
    return 0;
}

Output: C is powerful language
Explanation:
Since #pragma world is unknown for Turbo c 3.0 compilers so it will ignore this directive without showing any error or warning message and execute the whole program assuming #pragma world statement is not present.

List of pragma directives in turbo c 3.0:

1. #pragma startup
2. #pragma exit
3. #pragma warn
4. #pragma option
5. #pragma inline
6. #pragma argsused
7. #pragma hdrfile
8. #pragma hdrstop
9. #pragma saveregs

Array whose content is address of another variable is known as array pointers. For example:

#include<stdio.h>
int main(){
float a=0.0f,b=1.0f,c=2.0f;
    float * arr[]={&a,&b,&c};
    b=a+c;
    printf("%f",arr[1]);
    return 0;
}       

#include <stdio.h>
typedef enum color{a,b,c,d,e}co;
enum color eee(){
    static co x;
    x=b+c/2;
    return x;
}

int main(){
    int num;
    num=eee();
    printf("%#d",num);
    return 0;
}

Output: 2

‹‹ previous123

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: