Monday, April 1, 2013

Integer division and explicit conversions

A program showing a few examples of the casting operator.


/* Integer division */
#include <stdio.h> int main (void) { int a, b, c; double x, y, z, w; a = 10; b = 20; /* dividing two integers */ z = a / b; c = a / b; printf ("The value of z is %5.3lf ", z); printf ("and the value of c is %d\n", c); /* converting (casting) one operand before the division*/ x = (double)a / b; printf ("The value of x is %5.3lf\n", x); /* casting the quotient after the division*/ y = (double) (a / b); printf ("The value of y is %5.3lf\n", y); /* casting both operands before the division*/ w = (double)a / (double)b; printf ("The value of w is %5.3lf\n", w); return (0); }
Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
The value of z is 0.000 and the value of c is 0
The value of x is 0.500
The value of y is 0.000
The value of w is 0.500

No comments:

Post a Comment