Using floats with sprintf() in embedded C

后端 未结 13 1879
别跟我提以往
别跟我提以往 2020-12-24 06:35

Guys, I want to know if float variables can be used in sprintf() function.

Like, if we write:

sprintf(str,\"adc_read = %d \         


        
13条回答
  •  无人及你
    2020-12-24 07:04

    Yes, and no. Despite what some other replies have said, the C compiler is required to perform conversions for sprintf(), and all other variadic functions, as follows:

    • char => int
    • short => int
    • float => double

    (and signed/unsigned variants of the above integral types)

    It does this precisely because sprintf() (and the other print()-family functions) would be unusable without it. (Of course, they're pretty unusable as it is.)

    But you cannot assume any other conversions, and your code will have undefined behaviour - read: crash! - if you do it.

提交回复
热议问题