Why does %f print large values when floating point constants are passed instead of variables?Is floating point math broken?Correct format specifier for double in printf'float' vs. 'double' precisionWhy does printf() promote a float to a double?How does printf and co differentiate between float and doubleC read file line by lineWhat happens to a float variable when %d is used in a printf?Hexadecimal Floating-Point ConstantWhy are elementwise additions much faster in separate loops than in a combined loop?How dangerous is it to compare floating point values?Why does the C preprocessor interpret the word “linux” as the constant “1”?Why does GCC generate 15-20% faster code if I optimize for size instead of speed?Why the same variable print the different output?Why are the int and float passed in printf going to the wrong positions in the format string?Float and int members in a union does not show values

Why is matter-antimatter asymmetry surprising, if asymmetry can be generated by a random walk in which particles go into black holes?

Transiting through Switzerland by coach with lots of cash

Can I use I2C over 2m cables?

Find the percentage

Solving a Certainty Equivalent (Decision Analysis) problem

Creating chess engine, machine learning vs. traditional engine?

How to ride a fish?

How acceptable is an ellipsis "..." in formal mathematics?

Low-magic medieval fantasy clothes that allow the wearer to grow?

What is the next number in the series: 21, 21, 23, 20, 5, 25, 31, 24,?

Can I color text by using an image, so that the color isn't flat?

How to copy the path of current directory in ubuntu 18.04

Does "Op. cit." stand for "opus citatum" or "opere citato"?

Tikz – Box/frame arround Text with interruption

There is any way today to recover/dump 2M disks?

Noun phrase and is

What is the meaning of "log" in this sentence?

how do you value what your leisure time is worth?

In Men at Arms, why announce Edward was caught?

My first random password generator

Proving roots of a function cannot all be real

grep pairs of patterns and file

How to find out which object is taking space?

What can I do to avoid potential charges for bribery?



Why does %f print large values when floating point constants are passed instead of variables?


Is floating point math broken?Correct format specifier for double in printf'float' vs. 'double' precisionWhy does printf() promote a float to a double?How does printf and co differentiate between float and doubleC read file line by lineWhat happens to a float variable when %d is used in a printf?Hexadecimal Floating-Point ConstantWhy are elementwise additions much faster in separate loops than in a combined loop?How dangerous is it to compare floating point values?Why does the C preprocessor interpret the word “linux” as the constant “1”?Why does GCC generate 15-20% faster code if I optimize for size instead of speed?Why the same variable print the different output?Why are the int and float passed in printf going to the wrong positions in the format string?Float and int members in a union does not show values






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









8















In the given program why did I get different results for each of the printfs?



#include <stdio.h>
int main()

float c = 4.4e10;
printf("%fn", c);
printf("%fn", 4.4e10);
return 0;



And it shows the following output:



44000002048.000000
44000000000.000000









share|improve this question





















  • 4





    The answers so far explain that 4.4e10 is a double constant which is converted to float in the initialization of c but kept as a double when passed to printf. However, you might also like to know that adding an f suffix makes it a float constant: Printing 4.4e10f will show the same value that results from initializing c to 4.4e10f. Distinguishing float constants from double constants can be important to doing quality work with floating-point arithmetic.

    – Eric Postpischil
    8 hours ago











  • Is this conversion method named anything? I want to read about it.

    – user10056563
    8 hours ago











  • Do you want to know when the conversion from double to float occurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?

    – Eric Postpischil
    8 hours ago











  • I'm questioning neither the replies here nor the standard but, when I was young and learning C we used printf("%f",x) for a float and printf("%lf",x) for a double. When did things change? And how would one explicitly printf a (single) float - printf("%hf",x)??

    – Adrian
    8 hours ago






  • 2





    @Adrian %lf in printf is the same thing as %f. A float in a variable argument is converted into a double by the compiler, just like a short gets converted into an int.

    – JL2210
    7 hours ago


















8















In the given program why did I get different results for each of the printfs?



#include <stdio.h>
int main()

float c = 4.4e10;
printf("%fn", c);
printf("%fn", 4.4e10);
return 0;



And it shows the following output:



44000002048.000000
44000000000.000000









share|improve this question





















  • 4





    The answers so far explain that 4.4e10 is a double constant which is converted to float in the initialization of c but kept as a double when passed to printf. However, you might also like to know that adding an f suffix makes it a float constant: Printing 4.4e10f will show the same value that results from initializing c to 4.4e10f. Distinguishing float constants from double constants can be important to doing quality work with floating-point arithmetic.

    – Eric Postpischil
    8 hours ago











  • Is this conversion method named anything? I want to read about it.

    – user10056563
    8 hours ago











  • Do you want to know when the conversion from double to float occurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?

    – Eric Postpischil
    8 hours ago











  • I'm questioning neither the replies here nor the standard but, when I was young and learning C we used printf("%f",x) for a float and printf("%lf",x) for a double. When did things change? And how would one explicitly printf a (single) float - printf("%hf",x)??

    – Adrian
    8 hours ago






  • 2





    @Adrian %lf in printf is the same thing as %f. A float in a variable argument is converted into a double by the compiler, just like a short gets converted into an int.

    – JL2210
    7 hours ago














8












8








8


1






In the given program why did I get different results for each of the printfs?



#include <stdio.h>
int main()

float c = 4.4e10;
printf("%fn", c);
printf("%fn", 4.4e10);
return 0;



And it shows the following output:



44000002048.000000
44000000000.000000









share|improve this question
















In the given program why did I get different results for each of the printfs?



#include <stdio.h>
int main()

float c = 4.4e10;
printf("%fn", c);
printf("%fn", 4.4e10);
return 0;



And it shows the following output:



44000002048.000000
44000000000.000000






c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 hours ago









JL2210

4,5664 gold badges12 silver badges42 bronze badges




4,5664 gold badges12 silver badges42 bronze badges










asked 8 hours ago









user10056563user10056563

443 bronze badges




443 bronze badges










  • 4





    The answers so far explain that 4.4e10 is a double constant which is converted to float in the initialization of c but kept as a double when passed to printf. However, you might also like to know that adding an f suffix makes it a float constant: Printing 4.4e10f will show the same value that results from initializing c to 4.4e10f. Distinguishing float constants from double constants can be important to doing quality work with floating-point arithmetic.

    – Eric Postpischil
    8 hours ago











  • Is this conversion method named anything? I want to read about it.

    – user10056563
    8 hours ago











  • Do you want to know when the conversion from double to float occurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?

    – Eric Postpischil
    8 hours ago











  • I'm questioning neither the replies here nor the standard but, when I was young and learning C we used printf("%f",x) for a float and printf("%lf",x) for a double. When did things change? And how would one explicitly printf a (single) float - printf("%hf",x)??

    – Adrian
    8 hours ago






  • 2





    @Adrian %lf in printf is the same thing as %f. A float in a variable argument is converted into a double by the compiler, just like a short gets converted into an int.

    – JL2210
    7 hours ago













  • 4





    The answers so far explain that 4.4e10 is a double constant which is converted to float in the initialization of c but kept as a double when passed to printf. However, you might also like to know that adding an f suffix makes it a float constant: Printing 4.4e10f will show the same value that results from initializing c to 4.4e10f. Distinguishing float constants from double constants can be important to doing quality work with floating-point arithmetic.

    – Eric Postpischil
    8 hours ago











  • Is this conversion method named anything? I want to read about it.

    – user10056563
    8 hours ago











  • Do you want to know when the conversion from double to float occurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?

    – Eric Postpischil
    8 hours ago











  • I'm questioning neither the replies here nor the standard but, when I was young and learning C we used printf("%f",x) for a float and printf("%lf",x) for a double. When did things change? And how would one explicitly printf a (single) float - printf("%hf",x)??

    – Adrian
    8 hours ago






  • 2





    @Adrian %lf in printf is the same thing as %f. A float in a variable argument is converted into a double by the compiler, just like a short gets converted into an int.

    – JL2210
    7 hours ago








4




4





The answers so far explain that 4.4e10 is a double constant which is converted to float in the initialization of c but kept as a double when passed to printf. However, you might also like to know that adding an f suffix makes it a float constant: Printing 4.4e10f will show the same value that results from initializing c to 4.4e10f. Distinguishing float constants from double constants can be important to doing quality work with floating-point arithmetic.

– Eric Postpischil
8 hours ago





The answers so far explain that 4.4e10 is a double constant which is converted to float in the initialization of c but kept as a double when passed to printf. However, you might also like to know that adding an f suffix makes it a float constant: Printing 4.4e10f will show the same value that results from initializing c to 4.4e10f. Distinguishing float constants from double constants can be important to doing quality work with floating-point arithmetic.

– Eric Postpischil
8 hours ago













Is this conversion method named anything? I want to read about it.

– user10056563
8 hours ago





Is this conversion method named anything? I want to read about it.

– user10056563
8 hours ago













Do you want to know when the conversion from double to float occurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?

– Eric Postpischil
8 hours ago





Do you want to know when the conversion from double to float occurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?

– Eric Postpischil
8 hours ago













I'm questioning neither the replies here nor the standard but, when I was young and learning C we used printf("%f",x) for a float and printf("%lf",x) for a double. When did things change? And how would one explicitly printf a (single) float - printf("%hf",x)??

– Adrian
8 hours ago





I'm questioning neither the replies here nor the standard but, when I was young and learning C we used printf("%f",x) for a float and printf("%lf",x) for a double. When did things change? And how would one explicitly printf a (single) float - printf("%hf",x)??

– Adrian
8 hours ago




2




2





@Adrian %lf in printf is the same thing as %f. A float in a variable argument is converted into a double by the compiler, just like a short gets converted into an int.

– JL2210
7 hours ago






@Adrian %lf in printf is the same thing as %f. A float in a variable argument is converted into a double by the compiler, just like a short gets converted into an int.

– JL2210
7 hours ago













2 Answers
2






active

oldest

votes


















8
















A float is a type that holds a 32-bit floating point number, while the constant 4.4e10 represents a double, which holds a 64-bit floating point number (i.e. a double-precision floating point number)



When you assign 4.4e10 to c, the value 4.4e10 cannot be represented precisely (a rounding error in a parameter called the mantissa), and the closest possible value (44000002048) is stored. When it is passed to printf, it is promoted back to double, including the rounding error.



In the second case, the value is a double the whole time, without narrowing and widening, and it happens to be the case that a double can represent the value exactly.



If this is undesirable behavior, you can declare c as a double for a bit more precision (but beware that you'll still hit precision limits eventually).






share|improve this answer


































    3
















    You're actually printing the values of two different types here.



    In the first case you're assigning a value to a variable of type float. The precision of a float is roughly 6 or 7 decimal digits, so unless the value can be represented exactly you'll see the closest value that can be represented by that type.



    In the second case you're passing the constant 4.4e10 which has type double. This type has around 16 decimal digits of precision, and the value is within that range, so the exact value is printed.






    share|improve this answer



























    • Why it specifically prints 2048 at the end ?

      – user10056563
      8 hours ago











    • @user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.

      – dbush
      8 hours ago












    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f58187573%2fwhy-does-f-print-large-values-when-floating-point-constants-are-passed-instead%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    8
















    A float is a type that holds a 32-bit floating point number, while the constant 4.4e10 represents a double, which holds a 64-bit floating point number (i.e. a double-precision floating point number)



    When you assign 4.4e10 to c, the value 4.4e10 cannot be represented precisely (a rounding error in a parameter called the mantissa), and the closest possible value (44000002048) is stored. When it is passed to printf, it is promoted back to double, including the rounding error.



    In the second case, the value is a double the whole time, without narrowing and widening, and it happens to be the case that a double can represent the value exactly.



    If this is undesirable behavior, you can declare c as a double for a bit more precision (but beware that you'll still hit precision limits eventually).






    share|improve this answer































      8
















      A float is a type that holds a 32-bit floating point number, while the constant 4.4e10 represents a double, which holds a 64-bit floating point number (i.e. a double-precision floating point number)



      When you assign 4.4e10 to c, the value 4.4e10 cannot be represented precisely (a rounding error in a parameter called the mantissa), and the closest possible value (44000002048) is stored. When it is passed to printf, it is promoted back to double, including the rounding error.



      In the second case, the value is a double the whole time, without narrowing and widening, and it happens to be the case that a double can represent the value exactly.



      If this is undesirable behavior, you can declare c as a double for a bit more precision (but beware that you'll still hit precision limits eventually).






      share|improve this answer





























        8














        8










        8









        A float is a type that holds a 32-bit floating point number, while the constant 4.4e10 represents a double, which holds a 64-bit floating point number (i.e. a double-precision floating point number)



        When you assign 4.4e10 to c, the value 4.4e10 cannot be represented precisely (a rounding error in a parameter called the mantissa), and the closest possible value (44000002048) is stored. When it is passed to printf, it is promoted back to double, including the rounding error.



        In the second case, the value is a double the whole time, without narrowing and widening, and it happens to be the case that a double can represent the value exactly.



        If this is undesirable behavior, you can declare c as a double for a bit more precision (but beware that you'll still hit precision limits eventually).






        share|improve this answer















        A float is a type that holds a 32-bit floating point number, while the constant 4.4e10 represents a double, which holds a 64-bit floating point number (i.e. a double-precision floating point number)



        When you assign 4.4e10 to c, the value 4.4e10 cannot be represented precisely (a rounding error in a parameter called the mantissa), and the closest possible value (44000002048) is stored. When it is passed to printf, it is promoted back to double, including the rounding error.



        In the second case, the value is a double the whole time, without narrowing and widening, and it happens to be the case that a double can represent the value exactly.



        If this is undesirable behavior, you can declare c as a double for a bit more precision (but beware that you'll still hit precision limits eventually).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 7 hours ago









        JL2210

        4,5664 gold badges12 silver badges42 bronze badges




        4,5664 gold badges12 silver badges42 bronze badges










        answered 8 hours ago









        ζ--ζ--

        31.5k4 gold badges66 silver badges91 bronze badges




        31.5k4 gold badges66 silver badges91 bronze badges


























            3
















            You're actually printing the values of two different types here.



            In the first case you're assigning a value to a variable of type float. The precision of a float is roughly 6 or 7 decimal digits, so unless the value can be represented exactly you'll see the closest value that can be represented by that type.



            In the second case you're passing the constant 4.4e10 which has type double. This type has around 16 decimal digits of precision, and the value is within that range, so the exact value is printed.






            share|improve this answer



























            • Why it specifically prints 2048 at the end ?

              – user10056563
              8 hours ago











            • @user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.

              – dbush
              8 hours ago















            3
















            You're actually printing the values of two different types here.



            In the first case you're assigning a value to a variable of type float. The precision of a float is roughly 6 or 7 decimal digits, so unless the value can be represented exactly you'll see the closest value that can be represented by that type.



            In the second case you're passing the constant 4.4e10 which has type double. This type has around 16 decimal digits of precision, and the value is within that range, so the exact value is printed.






            share|improve this answer



























            • Why it specifically prints 2048 at the end ?

              – user10056563
              8 hours ago











            • @user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.

              – dbush
              8 hours ago













            3














            3










            3









            You're actually printing the values of two different types here.



            In the first case you're assigning a value to a variable of type float. The precision of a float is roughly 6 or 7 decimal digits, so unless the value can be represented exactly you'll see the closest value that can be represented by that type.



            In the second case you're passing the constant 4.4e10 which has type double. This type has around 16 decimal digits of precision, and the value is within that range, so the exact value is printed.






            share|improve this answer















            You're actually printing the values of two different types here.



            In the first case you're assigning a value to a variable of type float. The precision of a float is roughly 6 or 7 decimal digits, so unless the value can be represented exactly you'll see the closest value that can be represented by that type.



            In the second case you're passing the constant 4.4e10 which has type double. This type has around 16 decimal digits of precision, and the value is within that range, so the exact value is printed.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 7 hours ago









            JL2210

            4,5664 gold badges12 silver badges42 bronze badges




            4,5664 gold badges12 silver badges42 bronze badges










            answered 8 hours ago









            dbushdbush

            114k15 gold badges129 silver badges162 bronze badges




            114k15 gold badges129 silver badges162 bronze badges















            • Why it specifically prints 2048 at the end ?

              – user10056563
              8 hours ago











            • @user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.

              – dbush
              8 hours ago

















            • Why it specifically prints 2048 at the end ?

              – user10056563
              8 hours ago











            • @user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.

              – dbush
              8 hours ago
















            Why it specifically prints 2048 at the end ?

            – user10056563
            8 hours ago





            Why it specifically prints 2048 at the end ?

            – user10056563
            8 hours ago













            @user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.

            – dbush
            8 hours ago





            @user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.

            – dbush
            8 hours ago


















            draft saved

            draft discarded















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f58187573%2fwhy-does-f-print-large-values-when-floating-point-constants-are-passed-instead%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480