1

I have below function in my application:

 void func(char *file, int line)
 {
    char stmt[150];
    sprintf(stmt, "Failed in file %s, at line number %d", file, line);
 }

Here I am taking char array of 150 bytes(rough guess), instead I need to find the length of the string that I gave in sprintf() and take the array of that size.

Pls suggest me if such facility in C to find the length of a formatted string. Thanks !

Srikanth
  • 517
  • 3
  • 10
  • 29

1 Answers1

7

What about snprintf? The function does not write more characters than given number, but returns the length of the string it would create if it had enough space.

 int len = snprintf(NULL, 0, "Failed in file %s, at line number %d", file, line);
Lubomír Sedlář
  • 1,580
  • 2
  • 17
  • 26