Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about declaring Floating Point Variable in a Structure

Reply
Thread Tools

Question about declaring Floating Point Variable in a Structure

 
 
Nasir Khan
Guest
Posts: n/a
 
      09-15-2004
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
 
Reply With Quote
 
 
 
 
LM
Guest
Posts: n/a
 
      09-15-2004
> 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;
}


 
Reply With Quote
 
 
 
 
Gordon Burditt
Guest
Posts: n/a
 
      09-15-2004
>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
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      09-15-2004
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.
 
Reply With Quote
 
Tim Hagan
Guest
Posts: n/a
 
      09-15-2004
"Nasir Khan" <> wrote in message
news: m...
> 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



 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-16-2004
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/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
converting floating point to fixed point H aka N VHDL 15 03-02-2006 02:26 PM
floating point to fixed point conversion riya1012@gmail.com C Programming 4 02-22-2006 05:56 PM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57