Concatenate String and Int in C

  1. Use asprintf, strcat and strcpy Functions to Concatenate String and Int in C
  2. Use asprintf and memccpy Functions to Concatenate String and Int in C

This article will demonstrate multiple methods for concatenating string and int in C.

Use asprintf, strcat and strcpy Functions to Concatenate String and Int in C

The first step to concatenating int variable and the character string is to convert an integer to string. We utilize asprintf function to store the passed integer as a character string. asprintf is part of the GNU C library extension and may not be available in other implementations. It works similarly to sprintf except that the destination character string buffer is allocated dynamically using the malloc function call internally, and the returned pointer should be freed before the program exit. Once the integer is converted, we chain strcpy and strcat calls to concatenate two given character strings in the buffer that the user allocates. In this case, we arbitrarily defined MAX macro to denote the destination buffer’s size, but in real scenarios, it would be more flexible to use malloc.

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifndef MAX
#define MAX 100
#endif

int main(int argc, char *argv[]) {
    const char* str1 = "hello there";
    int n1 = 1234;

    char *num;
    char buffer[MAX];

    if (asprintf(&num, "%d", n1) == -1) {
        perror("asprintf");
    } else {
        strcat(strcpy(buffer, str1), num);
        printf("%s\n", buffer);
        free(num);
    }

    exit(EXIT_SUCCESS);
}

Output:

hello there1234

Alternatively, the chained calls can be followed by another invocation of the strcat function and append other strings to the given char buffer. Note that we check the asprintf function for the successful return value, and if it fails, then we continue with the concatenation process.

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifndef MAX
#define MAX 100
#endif

int main(int argc, char *argv[]) {
    const char* str1 = "hello there";
    int n1 = 1234;

    char *num;
    char buffer[MAX];

    if (asprintf(&num, "%d", n1) == -1) {
        perror("asprintf");
    } else {
        strcat(strcpy(buffer, "Hello there, "), num);
        strcat(buffer, "! continued");
        printf("%s\n", buffer);
        free(num);
    }

    exit(EXIT_SUCCESS);
}

Output:

Hello there, 1234! continued

Use asprintf and memccpy Functions to Concatenate String and Int in C

Alternatively, asprintf can be used in conjunction with memccpy to concatenate character string, and int. memccpy is part of the C standard library string utilities defined in the <string.h> header file. It takes two pointers denoting the source and destination buffers. Note that these buffer memory areas should not overlap; otherwise, the results are undefined. The last two arguments represent the character at which to stop copying and the maximum number of bytes to be taken from the source location. We call the free function in the else scope since otherwise, we can’t be sure the num pointer is the valid pointer.

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifndef MAX
#define MAX 100
#endif

int main(int argc, char *argv[]) {
    const char* str1 = "hello there";
    int n1 = 1234;

    char *num;
    char buffer[MAX];

    if (asprintf(&num, "%d", n1) == -1) {
        perror("asprintf");
    } else {
        memccpy(memccpy(buffer, str1, '\0', MAX) - 1, num, '\0', MAX);
        printf("%s\n", buffer);
        free(num);
    }

    exit(EXIT_SUCCESS);
}

Output:

hello there1234
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C String

  • Use the strsep Function in C
  • Convert a String to Integer in C