float values can b rounded off?

S

sushant

hi;

the code:

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

Richard Bos

sushant said:
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
 
J

Jonathan Burd

sushant said:
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.
 
J

john_bode

sushant said:
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.
 
M

Martin Ambuhl

sushant said:
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;
}
 
C

CBFalconer

sushant said:
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.
 
M

Mike Wahler

sushant said:
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
 
K

Keith Thompson

CBFalconer said:
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.
 
M

Mabden

Keith Thompson said:
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)
 
R

Richard Bos

Jonathan Burd said:
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
 
R

Richard Bos

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


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
 
M

Michael Mair

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



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

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

-Michael
 
J

Jonathan Burd

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

Ask HOMER. ;)
Is that when a boy and a girl have ercion together?




I think he conceived it while having ercion.

-Mike

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

Regards,
Jonathan.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top