Edit

Share via


Warning C6273

Non-integer passed as parameter 'number' when an integer is required in call to 'function'

Remarks

This warning indicates that the format string specifier in a printf like function is expecting an integer type, but a non-integer such as a float, string, or struct is being passed instead. This warning checks integer type specifiers like %d, and width/precision specifier that use integers like %*.*f. This defect is likely to result in incorrect output.

Code analysis name: NON_INTEGER_ARGUMENT_TO_FORMAT_FUNCTION

Example

The following code generates this warning because an integer is required instead of a float in the sprintf function:

#include <stdio.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. Alternatively it could have corrected the warning by modifying the format specifier to match the type.

#include <stdio.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>

void f_safe()
{
  char buff[50];
  float f=1.5;

  sprintf_s(buff,50,"%d",(int)f);
}

This warning isn't applicable on Windows 9x and Windows NT version 4 because %p isn't supported on these platforms.

See also

Format specification syntax: printf and wprintf functions
sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
C4477