Question about declaring Floating Point Variable in a Structure

N

Nasir Khan

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
 
L

LM

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;
}
 
G

Gordon Burditt

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
 
C

Christopher Benson-Manica

Nasir Khan said:
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
 
T

Tim Hagan

Nasir Khan said:
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 */
}
 
J

Jack Klein

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top