Showing entries with tag "sprintf".

Found 1 entries

C/C++: Appending to a string with sprintf()

I'm a big fan of sprintf() and use it in a lot of projects. Often I will want to append to a string instead of creating a new one. This solution will create an "end of string" function named eos() that returns a pointer to the end of a given string. If you feed that to sprintf() it will effectively append to the existing string.

char *eos(char str[]) {
    return (str) + strlen(str);
}

int main(int argc, char *argv[]) {
    char tmp_str[50] = "";

    sprintf(eos(tmp_str), "Weird");
    sprintf(eos(tmp_str), " Al");
    sprintf(eos(tmp_str), " Yankovic");
}
Leave A Reply