为sprintf函数提供正确的参数类型/数量
示例代码:
假设我们有以下代码:
int num = 10; char buf[20];
sprintf(buf, "The number is %s", num);
当编译时,出现sprintf格式警告:
format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘int’
这是因为我们使用了%s格式代码,但是num是一个整数,而不是字符串。因此,我们需要使用正确的格式代码来匹配num的数据类型。正确的代码是%d:
sprintf(buf, "The number is %d", num);
现在,编译将不会出现任何sprintf格式警告。