Can the input and output strings to sprintf() be the same?

后端 未结 2 1794
生来不讨喜
生来不讨喜 2020-12-10 10:39

I have used this type of convention many times in my code in the past:

strcpy ( cTmpA, \"hello\" );
sprintf ( cTmpA, \"%s world\", cTmpA );

2条回答
  •  执笔经年
    2020-12-10 11:27

    While it is valid K&R C, you probably want to rather know if it's valid POSIX - see sprintf Specification. We read:

    If copying takes place between objects that overlap as a result of a call to sprintf() or snprintf(), the results are undefined.

提交回复
热议问题