How to concatenate two integers in C

unsigned concatenate(unsigned x, unsigned y) {
    unsigned pow = 10;
    while(y >= pow)
        pow *= 10;
    return x * pow + y;        
}

Proof of compilation/correctness/speed

I avoid the log10 and pow functions, because I’m pretty sure they use floating point and are slowish, so this might be faster on your machine. Maybe. Profile.

Leave a Comment