Determining sprintf buffer size - what's the standard?

前端 未结 7 1904
臣服心动
臣服心动 2020-11-28 06:51

When converting an int like so:

char a[256];
sprintf(a, \"%d\", 132);

what\'s the best way to determine how large a should be? I a

7条回答
  •  生来不讨喜
    2020-11-28 07:28

    Its good that you are worried about buffer size. To apply that thought in code, I would use snprintf

    snprintf( a, 256, "%d", 132 );
    

    or

    snprintf( a, sizeof( a ), "%d", 132 );  // when a is array, not pointer
    

提交回复
热议问题