C Tutorial/Data Type/float Declaration
Содержание
Assignment of an integer expression to a floating-point variable.
C will automatically perform the conversion from integer to floating point. A similar conversion is performed when a floating-point number is assigned to an integer. For example:
#include<stdio.h>
int main(){
int integer; /* an integer */
float floating; /* a floating-point number */
floating = 1.0 / 2.0; /* "floating" 0.5 */
printf("%f\n",floating);
integer = 1 / 3; /* integer 0 */
printf("%d\n",integer);
floating = (1 / 2) + (1 / 2); /* floating 0.0 */
printf("%f\n",floating);
floating = 3.0 / 2.0; /* floating 1.5 */
printf("%f\n",floating);
integer = floating; /* integer 1 */
return (0);
}
0.500000 0 0.000000 1.500000
Define float constant value using Macro
#include <stdio.h>
#define PI 3.14159f
int main(void)
{
float radius = 0.0f;
float diameter = 0.0f;
float circumference = 0.0f;
float area = 0.0f;
printf("Input the diameter of a table:");
scanf("%f", &diameter);
radius = diameter/2.0f;
circumference = 2.0f*PI*radius;
area = PI*radius*radius;
printf("\nThe circumference is %.2f", circumference);
printf("\nThe area is %.2f", area);
return 0;
}
Input the diameter of a table:123 The circumference is 386.42 The area is 11882.28
Define float in In scientific notation (The E notation)
#include <stdio.h>
int main()
{
float lightyear=5.878E12;
float jupiter=483400000;
float distance;
distance=jupiter/lightyear;
printf("Jupiter is %f light years from the sun.\n",distance);
return(0);
}
Jupiter is 0.000082 light years from the sun.
Define float variable and output to the console
#include <stdio.h>
int main()
{
int pints=1;
float price = 1.45;
printf("You want %d pint.\n",pints);
printf("That be $%f, please.\n",price);
return(0);
}
You want 1 pint. That be $1.450000, please.
Floating Point variables
The numbers 5.5, 8.3, and -12.6 are all floating-point numbers.
Floating-point numbers must contain a decimal point.
So 5.0 is a floating-point number, while 5 is an integer.
You could omit digits before the decimal point and specify a number as .5 instead of 0.5
A floating-point zero should be written as 0.0.
The floating-point number may include an exponent specification of the form:e + exp
For example, 1.2e34 is a shorthand version of 1.2*10^34.
2.18.float Declaration 2.18.1. Floating Point variables 2.18.2. <A href="/Tutorial/C/0040__Data-Type/Theformofafloatingpointdeclaration.htm">The form of a floating-point declaration</a> 2.18.3. <A href="/Tutorial/C/0040__Data-Type/Definefloatvariableandoutputtotheconsole.htm">Define float variable and output to the console</a> 2.18.4. <A href="/Tutorial/C/0040__Data-Type/DefinefloatinInscientificnotationTheEnotation.htm">Define float in In scientific notation (The E notation)</a> 2.18.5. <A href="/Tutorial/C/0040__Data-Type/DefinefloatconstantvalueusingMacro.htm">Define float constant value using Macro</a> 2.18.6. <A href="/Tutorial/C/0040__Data-Type/Assignmentofanintegerexpressiontoafloatingpointvariable.htm">Assignment of an integer expression to a floating-point variable.</a>
The form of a floating-point declaration
float variable; /* comment */