0

Can anyone explain the different number of chars returned for the exponent between sprintf_s and sprintf family?

I'm using an Eclipse environment with mingw-w64 and mys64. The version returned is g++ (Rev5, Built by MSYS2 project) 10.3.0. I have tried a number of C++ dialects.

n.b. The results are consistent in a Visual Studio 2019 Community console app.

source:

#include <cstdio>

int main() {
  float fv;
  char szWkg1[20];
  char szWkg2[20];

  fv = 0.005f;
  sprintf_s(szWkg1, sizeof(szWkg1), "%6.2E", fv);
  sprintf(szWkg2, "%6.2E", fv);
  printf("sprintf_s <%s> sprintf <%s> printf <%6.2E>", szWkg1, szWkg2, fv);
  fprintf(stdout, "  fprintf <%6.2E>\n", fv);

  fv = 1000.0f;
  sprintf_s(szWkg1, sizeof(szWkg1), "%6.2E", fv);
  sprintf(szWkg2, "%6.2E", fv);
  printf("sprintf_s <%s> sprintf <%s> printf <%6.2E>", szWkg1, szWkg2, fv);
  fprintf(stdout, "  fprintf <%6.2E>\n", fv);
  return 0;
}

Output:
sprintf_s <5.00E-003> sprintf <5.00E-03> printf <5.00E-03>  fprintf <5.00E-03>
sprintf_s <1.00E+003> sprintf <1.00E+03> printf <1.00E+03>  fprintf <1.00E+03>
  • [Related/Duplicate](https://stackoverflow.com/q/41844636/10871073). Not sure if your MinGW is the latest version, or an old one. Maybe the GCC people haven't kept up with the changes to "compliance" that MSVC made in VS-2015 (and later). – Adrian Mole May 05 '22 at 11:19

0 Answers0