This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

what will happen if printf() function is used ?

Dear All,

In my coding , by mistaken i used printf() function instead of sprintf()....
but my code compile ....but not running....

but after replacing sprintf() function ....my code is working fine.

Can any explain be what happen?

Thanks in Advance.

  • but my code compile

    That's hardly surprising. The C programming language enforces very few restrictions on the code, so there are plenty of ways to shoot yourself in the foot.

    ....but not running....

    You mean 'doesn't work as expected'? That's not surprising either, since you said that you made a typo in the source code. The fact that the compiler accepts your code doesn't mean that the code will behave as expected.

    Can any explain be what happen?

    Apparently, you expected something different to happen. What did you expect?

  • 1) You may not have configured your code to know where to send data from printf(). Does printf() work in other parts of your code?

    2) printf() takes a formatting string as first argument. sprintf() takes a destination buffer as first argument and the formatting string as second argument. How fun for printf() to get a buffer of random content as formatting string.

    3) printf() wants to emit data, which means that printf() can require a lot of time while waiting for a serial port to consume the data. This may break a program that needs the call to be quick. sprintf() don't need to synchronize with any I/O - it just leaves the result in a buffer.