What is the difference between %g and %f in C?

前端 未结 4 781
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 10:05

I was going through The C programming Language by K&R. Here in a statement to print a double variable it is written

printf(\"\\t%g\\n\", sum += atof(lin         


        
4条回答
  •  轮回少年
    2020-12-08 10:50

    They are both examples of floating point input/output.

    %g and %G are simplifiers of the scientific notation floats %e and %E.

    %g will take a number that could be represented as %f (a simple float or double) or %e (scientific notation) and return it as the shorter of the two.

    The output of your print statement will depend on the value of sum.

提交回复
热议问题