warning C6273: Non-integer passed as parameter '4' when integer is required in call to 'sprintf_s':

来源:互联网 发布:孤岛惊魂4优化 编辑:程序博客网 时间:2024/04/28 23:57

char c_array[20] = {0};

double double_value = 3.5;

sprintf_s(c_array, MAX_PATH, "%ld", double_value);

出现警告: warning C6273: Non-integer passed as parameter '4' when integer is required in call to 'sprintf_s': if a pointer value is being passed, %p should be used

 

查msdn:

Warning 6273 - non-integer passed as parameter <number> when integer is required in call to <function>: if a pointer value is being passed, %p should be used

This warning indicates that the format string specifies an integer, for example, a%d, length or precedence specification for printf but a non-integer such as a float, string, orstruct is being passed as a parameter. This defect is likely to result in incorrect output.

Example

The following code generates this warning because an integer is required instead of afloat to sprintffunction:

#include <stdio.h>  #include <string.h>    void f_defective()  {    char buff[50];    float f=1.5;        sprintf(buff, "%d",f);  }  
#include <stdio.h>#include <string.h>void f_defective(){  char buff[50];  float f=1.5;    sprintf(buff, "%d",f);}

The following code uses an integer cast to correct this warning:

#include <stdio.h>  #include <string.h>    void f_corrected()  {    char buff[50];    float f=1.5;      sprintf(buff,"%d",(int)f);  }  
#include <stdio.h>#include <string.h>void f_corrected(){  char buff[50];  float f=1.5;  sprintf(buff,"%d",(int)f);}

The following code uses safe string manipulation function, sprintf_s, to correct this warning:

#include <stdio.h>  #include <string.h>    void f_safe()  {    char buff[50];    float f=1.5;      sprintf_s(buff,50,"%d",(int)f);  }  
#include <stdio.h>#include <string.h>void f_safe(){  char buff[50];  float f=1.5;  sprintf_s(buff,50,"%d",(int)f);}

This warning is not applicable on Windows 9x and Windows NT version 4 because %p is not supported on these platforms.

总结:字符串格式与输入的内容格式不一致 解决方法:保持一致:

char c_array[20] = {0};

double double_value = 3.5;

sprintf_s(c_array, MAX_PATH, "%lf", double_value);

 

原创粉丝点击