Assignment of an integer expression to a floating-point variable. : float Declaration « Data Type « C Tutorial






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








2.18.float Declaration
2.18.1.Floating Point variables
2.18.2.The form of a floating-point declaration
2.18.3.Define float variable and output to the console
2.18.4.Define float in In scientific notation (The E notation)
2.18.5.Define float constant value using Macro
2.18.6.Assignment of an integer expression to a floating-point variable.