C6272

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

For the latest documentation on Visual Studio 2017, see C6272 on docs.microsoft.com. warning C6272: non-float passed as argument <number> when float is required in call to <function>

This warning indicates that the format string specifies that a float is required, for example, a %f or %g specification for printf, but a non-float such as an integer or string is being passed. This defect is likely to result in incorrect output; however, in certain circumstances it could result in a crash.

The following code generates this warning:

#include <stdio.h>  
#include <string.h>  
  
void f()  
{  
   char buff[5];  
   int i=5;  
  
   sprintf(buff,"%s %f","a",i);   
}  
  

To correct this warning, use %i instead of %f specification as shown in the following code:

#include <stdio.h>  
#include <string.h>  
  
void f()  
{  
   char buff[5];  
   int i=5;  
  
   sprintf(buff,"%s %i","a",i);    
}  

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

#include <stdio.h>  
#include <string.h>  
  
void f()  
{  
   char buff[5];  
   int i=5;  
  
   sprintf_s(buff,5,"%s %i","a",i); // safe version  
}  

sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l

Show: