Simple use of sprintf - C

前端 未结 4 1363
夕颜
夕颜 2020-12-13 20:16

I\'m trying to work out why a larger problem is occurring, using a smaller program as an example. This smaller program does not work, leading me to believe it is my understa

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 20:39

    This is because 5 is an integer (int), and you're telling sprintf to pretend that it's a double-precision floating-point number (double). You need to change this:

    sprintf(word,"%.9g", 5);
    

    to either of these:

    sprintf(word,"%.9g", 5.0);
    sprintf(word,"%.9g", (double) 5);
    

提交回复
热议问题