Int and floats

From Teknologisk videncenter
Revision as of 07:54, 4 October 2018 by Heth (talk | contribs) (Created page with "=C programming= Be careful when mixing floats and ints. Consider this code. <source lang=c> #include <stdio.h> int main( void ) { float f1; float f2;...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

C programming

Be careful when mixing floats and ints. Consider this code.

#include <stdio.h>

int main( void ) {
        float f1;
        float f2;
        signed int si1;
        unsigned int ui1;

        si1 = -10;
        ui1 = 100;

        f1 = si1 * ui1;
        printf("f1 = %f\n",f1);
        f2 = (float) si1 * ui1;
        printf("f2 = %f\n",f2);
        return(0);

}

Output from this codesnippet is:

 f1 = 4294966272.000000
 f2 = -1000.000000



Links