How To Concatenate Two Integers in C


Improve your writing skills in 5 minutes a day with the Daily Writing Tips email newsletter.

This might not be the most efficient way, but it’s certainly one of the easiest. You simply need to convert both integers into strings using sprintf, concatenate those using strcat, and then reverse the combined string to an integer using the atoi function.

int concat(int x, int y){
    char str1[20];
    char str2[20];

    sprintf(str1,"%d",x);
    sprintf(str2,"%d",y);

    strcat(str1,str2);

    return atoi(str1);
}

Notice that there’s a function called itoa which converts integers into strings, but it’s not part of C’s standard library, so many compilers won’t recognize it, that’s why sprintf is a safer bet.

3 thoughts on “How To Concatenate Two Integers in C

  1. Stephen

    I honestly can’t think of a situation that might require you to do this but it’s probably better to return a string instead to avoid overflow

    Otherwise, you would probably have to multiple x by y’s largest power of 10, do an overflow check, and then add them together

    Reply
    1. Daniel Scocco Post author

      Good point, and I agree.

      The problem I was working on had small integers only, so this was not a concern, but for other applications it might be a good idea.

      Reply
  2. chandravathi

    how to concatinate two integer numbers with out using ny arthimetic and string operations.
    ex:a=12,b=36
    output c=3612

    all a,b,c are integer

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *