Guys, I want to know if float
variables can be used in sprintf()
function.
Like, if we write:
sprintf(str,"adc_read = %d \n",adc_read);
where adc_read
is an integer variable, it will store the string
"adc_read = 1023 \n"
in str
(assuming that adc_read = 1023
)
How can I use a float variable in place of integer?
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 otherprint()
-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.