473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

float values can b rounded off?

hi;

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????

Nov 14 '05 #1
13 1299
"sushant" <th********@rediffmail.com> wrote:
int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


Because you're lying to the compiler. Why should it magically figure out
which of the several possible interpretations of this undefined
behaviour you want?

Richard
Nov 14 '05 #2
sushant wrote:
hi;

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


1. No proper indentation (stop using Google to post;
get a real NG client).
2. "I MUST NOT FORGET TO INCLUDE STDIO.H" - Homer (well, not really).
3. Ever heard of coercion?

Regards,
Jonathan.

--
"I'm learning to program because then I can write
programs to do my homework faster." - Andy Anfilofieff
Nov 14 '05 #3

sushant wrote:
hi;

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
You're using the wrong conversion specifier in printf(). "%d" expects
an int argument. Use "%f" instead.
return 0;
}
is giving the o/p zero. why??????

Using the wrong conversion specifier leads to unpredictable results.

Nov 14 '05 #4
sushant wrote:
hi;

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


/* mha: After fixing
* 1) the failure to include <stdio.h>
* 2) the incorrect specifier for a floating-point argument, and
* 3) the failure to end the last line of output with an end-of-line
* character
* we get the working program below
*/
#include <stdio.h>

int main(void)
{
float i = 6.10203;
printf("%g\n", i);
return 0;
}
Nov 14 '05 #5
sushant wrote:

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


Screw ups:
1. Posting via google and losing all indentation. See sig.
2. Failure to #include <stdio.h>
3. Lying to the compiler about the type of i in the printf.
4. lack of blanks within the code (foolish style).

Either of 2 or 3 result in undefined behavior.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #6

"sushant" <th********@rediffmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
hi;

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


The result could be anything or nothing. The behavior
is undefined.

#include <stdio.h>

int main(void)
{
float i = 6.10203;
printf("%f\n", i);
return 0;
}

Tip: Prefer type 'double' over 'float'. It has
a much greater precision and range of values.

-Mike
Nov 14 '05 #7
CBFalconer <cb********@yahoo.com> writes:
sushant wrote:

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


Screw ups:
1. Posting via google and losing all indentation. See sig.
2. Failure to #include <stdio.h>
3. Lying to the compiler about the type of i in the printf.
4. lack of blanks within the code (foolish style).

Either of 2 or 3 result in undefined behavior.


As well as excessive abbreviation. In the subject, it's "be" not "b".
In the body of the article, "output" is much clearer than "o/p".

I understand there's a common writing style that uses very short
abbreviations like "4" for "for", "2" for "to", and so forth. Please
don't use that style here in comp.lang.c.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
As well as excessive abbreviation. In the subject, it's "be" not "b".
In the body of the article, "output" is much clearer than "o/p".

I understand there's a common writing style that uses very short
abbreviations like "4" for "for", "2" for "to", and so forth. Please
don't use that style here in comp.lang.c.


KT keepin' it real 4 the OG's! You my nigga, dawg!

(Uuuuhhh... that's a good thing, AFAICT)

--
Mabden
Nov 14 '05 #9
Jonathan Burd <jo***********@REMOVEMEgmail.com> wrote:
sushant wrote:
float i=6.10203;
printf("%d",i);
1. No proper indentation (stop using Google to post;
get a real NG client).
2. "I MUST NOT FORGET TO INCLUDE STDIO.H" - Homer (well, not really).


Ahem. That's stdio.h, please. C is case-sensitive.
3. Ever heard of coercion?


Yes, but what has it to do with this code?

Richard
Nov 14 '05 #10

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:41***************@news.individual.net...
Jonathan Burd <jo***********@REMOVEMEgmail.com> wrote:
sushant wrote:
float i=6.10203;
printf("%d",i);
1. No proper indentation (stop using Google to post;
get a real NG client).
2. "I MUST NOT FORGET TO INCLUDE STDIO.H" - Homer (well, not really).


Ahem. That's stdio.h, please. C is case-sensitive.


Ahem, ahem. That's <stdio.h> please.
C is spelling sensitive. :-)
3. Ever heard of coercion?


Is that when a boy and a girl have ercion together?
Yes, but what has it to do with this code?


I think he conceived it while having ercion.

-Mike
Nov 14 '05 #11
"Mike Wahler" <mk******@mkwahler.net> wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:41***************@news.individual.net...
Jonathan Burd <jo***********@REMOVEMEgmail.com> wrote:
sushant wrote:
> float i=6.10203;
> printf("%d",i);

1. No proper indentation (stop using Google to post;
get a real NG client).
2. "I MUST NOT FORGET TO INCLUDE STDIO.H" - Homer (well, not really).


Ahem. That's stdio.h, please. C is case-sensitive.


Ahem, ahem. That's <stdio.h> please.
C is spelling sensitive. :-)


Well... it's either "I must not forget to #include <stdio.h>", or "I
must not forget to include stdio.h", IYAM.
3. Ever heard of coercion?


Is that when a boy and a girl have ercion together?


Si puer cum puellula coercetur in cellula, felix double complex
conj(double complex z)?

Richard
Nov 14 '05 #12

Richard Bos wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:41***************@news.individual.net...
Jonathan Burd <jo***********@REMOVEMEgmail.com> wrote:
sushant wrote:

>float i=6.10203;
>printf("%d",i);

1. No proper indentation (stop using Google to post;
get a real NG client).
2. "I MUST NOT FORGET TO INCLUDE STDIO.H" - Homer (well, not really).

Ahem. That's stdio.h, please. C is case-sensitive.


Ahem, ahem. That's <stdio.h> please.
C is spelling sensitive. :-)

Well... it's either "I must not forget to #include <stdio.h>", or "I
must not forget to include stdio.h", IYAM.

3. Ever heard of coercion?


Is that when a boy and a girl have ercion together?


Si puer cum puellula coercetur in cellula, felix double complex
conj(double complex z)?


Vagantes Burani delectantur; excruciantur ad "restrict"ionem :-)

-Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #13
Mike Wahler wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:41***************@news.individual.net...
Jonathan Burd <jo***********@REMOVEMEgmail.com> wrote:

sushant wrote:

float i=6.10203;
printf("%d",i);

1. No proper indentation (stop using Google to post;
get a real NG client).
2. "I MUST NOT FORGET TO INCLUDE STDIO.H" - Homer (well, not really).
Ahem. That's stdio.h, please. C is case-sensitive.

Ahem, ahem. That's <stdio.h> please.
C is spelling sensitive. :-)


Ask HOMER. ;)

3. Ever heard of coercion?

Is that when a boy and a girl have ercion together?

Yes, but what has it to do with this code?

I think he conceived it while having ercion.

-Mike


"Oh yeah, baby!" ;)
</:P>

Regards,
Jonathan.

--
"Women should come with documentation." - Dave
Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Benny | last post by:
Hi, Just wonder why PRINT CAST(0.0573542567654 AS float) will give the rounded reult 0.0573543
5
by: Peter Scheurer | last post by:
Hi, we found some strange behavior when operating with floats and round(). The following simplified statement reproduces the problem. select 6.56 - round(convert(float, 6.56), 2) from...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
14
by: ziller | last post by:
Why is it that FLT_DIG (from <float.h>) is 6 while DBL_DIB is 15? Doing the math, the mantissa for floats is 24 bits = 2^24-1 max value = 16,777,215.0f. Anything 8-digit odd # greater than that...
5
by: Gustaf Liljegren | last post by:
Values altered when I convert from string to float and back again: using System; class FloatTest { static void Main(string args) { string var1 = "800856.22"; Console.WriteLine(var1);
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
11
by: Christian | last post by:
Hi, I have a small problem about distant compilation and float literals. Here is my program : #include <stdio.h> void main() { float f; double d; f = 0.5;
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
16
by: Dom Fulton | last post by:
Has anyone got a mechanism for finding the number of bits in floats, doubles, and long doubles? I need this to communicate with some hardware. I guess I could try to deduce this from float.h,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.