float f(float x)
{
return x*x;
}
|
|
Specifically:
|
int g(int a)
{
return a*a;
}
|
means:
|
|
Here is a C program that shows the binary representation of the values stored in int and float variables:
#include <stdio.h>
int main(int argc, char *argv[] )
{
int a = 2;
float b = 2;
printf("int %d representation in bits: ", a);
printBits(&a); // Ignore the definition of this function for now...
printf("\n\n");
b = 2;
printf("float %2.0f representation in bits: ", b);
printBits(&b);
printf("\n\n");
}
/* ------------------------------------------------------
Print bit representation
(Ignore this function for now;
it uses some advanced pointer stuff
this will be discussed later)
------------------------------------------------------- */
void printBits( void *ptr )
{
int x = *((int *)ptr);
int i;
for ( i = 31; i >= 0; i-- )
{
if ( (x & (1 << i)) != 0 )
printf("1");
else
printf("0");
}
}
|
Output:
int 2 representation in bits: 00000000000000000000000000000010 float 2 representation in bits: 01000000000000000000000000000000 |
This is exactly what you have learned in the first part of CS255 !!!
How to run the program:
|
float f(float x)
{
return x*x;
}
int main( int argc, char* argv[ ])
{
....
b = f(a);
}
|
This is what happens in the function call b = f(a):
|
I will also high light the difference in behavior between C and Java....