1,675 views
4 4 votes
 

Point out the error in the following program.

#include<stdio.h>
int main()
{
    struct emp
    {
        char name[20];
        float sal;
    };
    struct emp e[10];
    int i;
    for(i=0; i<=9; i++)
        scanf("%s %f", e[i].name, &e[i].sal);
    return 0;
}
  A. Suspicious pointer conversion
  B.
Floating point formats not linked (Run time error)
  C.
Cannot use scanf() for structures
  D.
Strings cannot be nested inside structures

1 Answer

1 1 vote

Answer is option B

In GCC compiler it's works perfectly. There is no error.

But this question is obsolete because while running this code in Turbo c and Borland compiler, it throws the error i.e floating point formats not linked

Abnormal program termination

The program terminate abnormally at the time while entering the float value for  e[i].sal

This is because Turbo and Borland C/ C++ compilers sometimes leave out floating point support and use non-floating-point version of printf() and scanf() to save space on smaller systems. When the compiler encounters a reference to the address of float, it sets a flag to have the linker link in the floating point emulator. There are some cases in which the reference to a float is a bit obscure and the compiler does not detect the need for emulator.

Solution:

Just add the following function in your program. It will force the compiler to include required libraries for handling floating point linkages.

static void force_fpf() /* A dummy function */
{
    float x, *y; /* Just declares two variables */
    y = &x;      /* Forces linkage of FP formats */
    x = *y;      /* Suppress warning message about x */
}

The dummy call to a floating-point function will force the compiler to load the floating-point support and solve the original problem.

• edited by
Position:
Show:

Related questions

1 1 vote
1 1 answer
331
331 views
khanshahima4 asked Sep 24
331 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...