/** * float - read a float from the command line. Intended mostly as * sample code. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // For printf #include // For atof // +---------+-------------------------------------------------------- // | Helpers | // +---------+ int f2i (float f) { return *((int *) &f); } // f2i void print_bits (int n) { unsigned int pos = 1; for (pos = pos << 31; pos != 0; pos = pos >> 1) { if (pos & n) putchar ('1'); else putchar ('0'); } // while } // print_bits // +------+----------------------------------------------------------- // | Main | // +------+ int main (int argc, char *argv[]) { float f; int i; // Error checking? We don't need any error checking! // Read the command-line argument and convert it to a float. f = (float) atof (argv[1]); // Do a bitwise conversion to an int. i = f2i (f); // Print it out printf ("f = %f\n", f); printf ("i = %d\n", i); printf ("bits: "); print_bits (i); printf ("\n"); // And we're done. return 0; } // main