Question about declaring Floating Point Variable in a Structure

Discussion in 'C Programming' started by Nasir Khan, Sep 15, 2004.

  1. Nasir Khan

    Nasir Khan Guest

    Dear Sir,

    I am a newbie in C. I am trying to run the following code.
    main()
    {
    struct emp
    {
    float sal;
    };
    struct emp empl;
    scanf("%f",&empl.sal);
    printf("%f",empl.sal);
    }


    This program compiles fine but at run time I get error. I am using
    Borland C on windows.

    IF any one has got similar problem and knows a fix, please help me.

    Kind Regards,
    Nasir Khan
    Nasir Khan, Sep 15, 2004
    #1
    1. Advertising

  2. Nasir Khan

    LM Guest

    > I am a newbie in C. I am trying to run the following code.
    > main()
    > {
    > struct emp
    > {
    > float sal;
    > };
    > struct emp empl;
    > scanf("%f",&empl.sal);
    > printf("%f",empl.sal);
    > }


    I have no problems with this code in Turbo C 2.0
    You can try to write in such way:

    main()
    {
    struct emp
    {
    float sal;
    } empl; /* another way of declaration*/
    scanf("%f",&empl.sal);
    printf("%f",empl.sal);
    return 0;
    }
    LM, Sep 15, 2004
    #2
    1. Advertising

  3. >I am a newbie in C. I am trying to run the following code.
    >main()
    >{
    > struct emp
    > {
    > float sal;
    > };
    > struct emp empl;
    > scanf("%f",&empl.sal);
    > printf("%f",empl.sal);
    >}
    >
    >
    >This program compiles fine but at run time I get error. I am using
    >Borland C on windows.


    You have a virus in your left thumb.

    *WHAT* is the error? Describe it. Presenting the exact wording
    wouldn't hurt. How do you know it is an error?

    If you get an error when you try to use your car, there's a large
    difference if the key won't turn vs. you get a letter in the mail
    from the finance company.

    Gordon L. Burditt
    Gordon Burditt, Sep 15, 2004
    #3
  4. Nasir Khan <> spoke thus:

    > main()
    > {
    > struct emp
    > {
    > float sal;
    > };
    > struct emp empl;
    > scanf("%f",&empl.sal);
    > printf("%f",empl.sal);
    > }


    First, note the differences between your code and

    #include <stdio.h>
    #include <stdlib.h>

    int main( void )
    {
    struct emp
    {
    float sal;
    };
    struct emp empl;
    scanf("%f",&empl.sal);
    printf("%f\n",empl.sal);
    return( EXIT_SUCCESS );
    }

    1) You included no header files. All bets are immediately off.
    2) main() returns int, although C89 will let you get away without
    saying so.
    3) Omitting the trailing newline may cause problems depending on your
    implementation.

    > This program compiles fine but at run time I get error. I am using
    > Borland C on windows.


    What error? Neither bcc32 5.4 nor gcc 2.95.3 complains at compile
    time or run time; bcc32 5.4 even compiles your original broken code
    and runs it cleanly. You'll have to be more specific about what
    you're doing and what's going wrong; and before you do, read the
    following links:

    http://www.ungerhu.com/jxh/clc.welcome.txt
    http://www.eskimo.com/~scs/C-faq/top.html
    http://benpfaff.org/writings/clc/off-topic.html

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Sep 15, 2004
    #4
  5. Nasir Khan

    Tim Hagan Guest

    "Nasir Khan" <> wrote in message
    news:...
    > Dear Sir,
    >
    > I am a newbie in C. I am trying to run the following code.
    > main()
    > {
    > struct emp
    > {
    > float sal;
    > };
    > struct emp empl;
    > scanf("%f",&empl.sal);
    > printf("%f",empl.sal);
    > }
    >
    >
    > This program compiles fine but at run time I get error.


    What error do you get?

    > I am using
    > Borland C on windows.
    >
    > IF any one has got similar problem and knows a fix, please help me.


    Actually, there are numerous errors in your code. Try this:

    #include <stdio.h> /* added header */

    int main(void) /* added return type */
    {
    struct emp
    {
    float sal;
    } empl; /* simplified */
    printf("Enter sal: "); /* added prompt */
    fflush(stdout); /* added */
    scanf("%f", &empl.sal);
    printf("%f\n", empl.sal); /* added newline */
    return 0; /* added */
    }

    --
    Tim Hagan
    Tim Hagan, Sep 15, 2004
    #5
  6. Nasir Khan

    Jack Klein Guest

    On 15 Sep 2004 05:31:04 -0700, (Nasir Khan) wrote in
    comp.lang.c:

    > Dear Sir,
    >
    > I am a newbie in C. I am trying to run the following code.
    > main()
    > {
    > struct emp
    > {
    > float sal;
    > };
    > struct emp empl;
    > scanf("%f",&empl.sal);
    > printf("%f",empl.sal);
    > }
    >
    >
    > This program compiles fine but at run time I get error. I am using
    > Borland C on windows.
    >
    > IF any one has got similar problem and knows a fix, please help me.
    >
    > Kind Regards,
    > Nasir Khan


    This is a FAQ (a Frequently Asked Question). Always check a
    newsgroup's FAQ before posting.

    See this question:

    "14.13 I'm having trouble with a Turbo C program which crashes and
    says something like ``floating point formats not linked.''", and its
    answer, at http://www.eskimo.com/~scs/C-faq/q14.13.html

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Sep 16, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,650
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,484
  3. Replies:
    4
    Views:
    1,288
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,322
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    254
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page