Notes on the float and double data type in C

Synopsis

Values must contain a floating-point. You can omit digits before the decimal point or digits after the decimal point, but not both. Floats cannot be signed or unsigned. Meaning they are essentially signed by default.

[float|double] = <value>

float

float myFloat = 125.8f;
float myFloat2 = 3.0f;
float myFloat3 = -.001f;
printf("Value of myFloat is: %g \n", myFloat);
printf("Value of myFloat2 is: %g \n", myFloat2);
printf("Value of myFloat3 is: %g \n", myFloat3);

The reason you need to add the character f (or F) to the declaration is to explicitly tell the compiler to treat the variable as a float. As by default floating-point constants are taken as a double by the C compiler.

double

double myDouble = 125.8;
double myDouble2 = 3.;
double myDouble3 = -.001;
printf("Value of myDouble is: %g \n", myDouble);
printf("Value of myDouble2 is: %g \n", myDouble2);
printf("Value of myDouble3 is: %g \n", myDouble3);

Ranges

float and double cannot have range specifiers like long and short.

float is generally 4 bytes in size (32 bits wide).
double is generally 8 bytes in size (64 bits wide).

Leave a Reply

Your email address will not be published. Required fields are marked *