Joined: Sun May 25, 2008 5:34 pm Posts: 95 Has thanked: 2 time Have thanks: 1 time
Code:
/* * Type conversion games. */ #include <stdio.h>
main() { /* Meaningless numbers. */ short s = -41; int i = 9; double d = 4.27; double ld = 4983.22; long l = 17; long long ll = 170; unsigned ui = 48;
/* When using printf, you must specify the size for items longer than int or double. Many incorrect combinations work anyway, but it's a good idea to get it right. */ printf("A: %d %ld %lld %f %lf\n", i, l, ll, d, ld);
/* Operators generally convert to the longer type, but you can force other conversions with casts. */ printf("B: %f %d %d %f %f\n", i * d, i * (int)d, i / 12, i / 12.0, (double)i / 12);
/* Conversion will convert to the left type. */ i = d; d = s; printf("C: %d %f\n", i, d);
/* Unsigned values can be printed with the u conversion. It generally differs from %d only for large numbers. */ printf("D: %u\n", ui);
/* Constants can be appended with a letter to give them the associated type. This generally matters only when the constant you want won't fit into the unmodified length. */ l = 1598L; ll = 9081092830129ll; /* Maybe LL would be better than ll. */ ld = 49.3981273394L; printf("F: %ld %lld %.12lf\n", l, ll, ld);
/* Converting from a longer to a smaller size will also produce junk if the value does not fit. */ i = 8000000; s = i; printf("G: %d == %d (sort of)\n", i, s); }