Does C have any tools for doing string addition?

If your C standard library is GNU or *BSD, then you probably have asprintf available. You may need to enable a feature test macro to use it, though. If you don’t have asprintf available, it can easily be defined in terms of the C standard vsnprintf function.

asprintf returns the result of the format as a newly-allocated string (which it is your responsibility to free). So you could write, for example:

char* buff;
int n = asprintf(&buff, "%s%c%s", dgdx, thisop, dhdx);

I usually use a wrapper function, which returns the string rather than the length, so you can write:

char* buff = concatf("%s%c%s", dgdx, thisop, dhdx);

Here are three simple implementations; the first will work on systems with vasprintf; the second on systems with Posix vsnprintf; and the third for Windows, which apparently implements a different snprintf interface.

// Version 1, systems which have vasprintf:
char* concatf(const char* fmt, ...) {
  va_list args;
  char* buf = NULL;
  va_start(args, fmt);
  int n = vasprintf(&buf, fmt, args);
  va_end(args);
  if (n < 0) { free(buf); buf = NULL; }
  return buf;
}

// Version 2: Systems without vasprintf but with vsnprintf
char* concatf(const char* fmt, ...) {
  va_list args;
  va_start(args, fmt);
  char* buf = NULL;
  int n = vsnprintf(NULL, 0, fmt, args);
  va_end(args);
  if (n >= 0) {
    va_start(args, fmt);
    buf = malloc(n+1);
    if (buf) vsnprintf(buf, n+1, fmt, args);
    va_end(args);
  }
  return buf;
}

// Version 3: Windows
// Apparently, the implementation of vsnprintf on Windows returns -1
// if not enough space has been provided. So here is the above code
// rewritten according to the documentation I found in
//  https://msdn.microsoft.com/en-us/library/w05tbk72%28VS.71%29.aspx
// and
//  https://msdn.microsoft.com/en-us/library/1kt27hek%28v=vs.71%29.aspx
// but totally untested. (If you try it, let me know)
char* concatf(const char* fmt, ...) {
  char* buf = NULL;
  va_list args;
  va_start(args, fmt);
  int n = _vscprintf(fmt, args);
  va_end(args);
  if (n >= 0) {
    va_start(args, fmt);
    buf = malloc(n+1);
    if (buf) _vsnprintf(buf, n+1, fmt, args);
    va_end(args);
  }
  return buf;
}

That’s the most concise equivalent I know of to string concatenation operators in other languages. (It’s not necessarily the most efficient in execution time, but it probably is in programmer time.)

Leave a Comment