Skip to content

Instantly share code, notes, and snippets.

@lesovsky
Created July 15, 2016 18:52
Show Gist options
  • Save lesovsky/d32a984f97cfcb991c54b27b5d6d3e0d to your computer and use it in GitHub Desktop.
Save lesovsky/d32a984f97cfcb991c54b27b5d6d3e0d to your computer and use it in GitHub Desktop.
How-to append strings using snprintf (without strcat/strncat).
#include <stdio.h>
#include <string.h>
#define LOC_MAXLEN 13
int main (void)
{
char dest[LOC_MAXLEN];
snprintf(dest, LOC_MAXLEN, "%s%s", "abc", "def");
printf("%s\n", dest);
/* append new string using length of previously added string */
snprintf(dest + strlen(dest), LOC_MAXLEN - strlen(dest), "%s", "ghi");
printf("%s\n", dest);
/* repeat that */
snprintf(dest + strlen(dest), LOC_MAXLEN - strlen(dest), "%s", "jkl");
printf("%s\n", dest);
return 0;
}
@serhatyuna
Copy link

Thanks!

@MartinMa28
Copy link

You save me! Thanks a lot!

@Jose-Carlos-Jimenez
Copy link

Thanks, it's amazing.

@iyssoft
Copy link

iyssoft commented Jun 1, 2023

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment