Slightly confused about output

+1 Compodulator Compodulator · January 8, 2015
Hello.
It took me a while, admittedly, longer then I'd like, to understand the whole connection about scanf(); and &s.
Specifically, that I need to put the & in the SCANF() and not the friggin PRINTF(), but that's not what I'm here to discuss.
I'm here to discuss either a bug, or me not understanding C properly.

Behold, me, having a eureka moment:

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

int main()
{
    int x;
    char arr[20];
    float y;


    printf("enter test integer\n");
    scanf("%d", &x);
    printf("x=%d\n",x);
    printf("enter test float\n");
    scanf("%f",&y);
    printf("y=%f\n",y);
    //printf("enter test string\n");
    //printf("enter test array\n");

    return 0;
}

Now, if you were to run this bad boy, you would most likely be impressed, ask for my information and possibly demand I work for your company and/or corporation. Alas, I would have to decline as I feel I am yet unworthy. The weird thing about this, however, is if you enter, in the float bit, say 1.0, it works fine.
but if you enter something like 4378.9... well... this happens...

http://www.pasteall.org/pic/82127

I'm not quite sure how to react... anyone help?

Post a Reply

Replies

Oldest  Newest  Rating
-1 Pere Garau Burguera · January 8, 2015
You probably wanna watch this:

0 c student · January 9, 2015
that's a mathematical rounding issue as discussed in c student's response.  what you can do is use the double data type or limit the output's decimal rounding, for example in your case:
printf ("%.1f\n", y);
0 Compodulator Compodulator · January 9, 2015
@c student

As a more in depth supplement to this video, you might be interested in this:http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html



this... this started nice and understandable... and then when the weirdly shaped B's and the P's and all the roman math came in, i became scared. I think americans call this "calculus"... I have
0 Compodulator Compodulator · January 9, 2015
After watching computerphile's video, i got a clearer understanding on how the computer looks at the math. well, not all of the math, but at least i think i have... some... measly understanding of  the subject.
doesn't mean i have anything close to a solution, nor does it mean i have any idea how to use this GMP arithmetic library you suggested, but i'll learn in the future, using bucky or something... hopefully...
  • 1

C

107,178 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator