1,189 views
5 votes
5 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 votes
1 votes

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

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked 3 days ago
50 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
103 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
252 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how