-4

I'm a beginner using C. I want to combine two integers into 1. See example code.

#include <stdio.h>

int main()
{
    int age[4];
    int mouse[4];
    mouse[0]=50;
    age[0]=23;

    age[1]=mouse[0]age[0];

    printf("%d",age[1]);
    return 0;
}

As you can see, I have 50 in mouse[0], 23 in age[0], and i want age[1] to be 5023.

What is the simplest way to do this? This will be a small part of a large code so I really want to keep it simple. And excuse the silly variable names!

Thanks all!

5 Answers5

2

You cannot concatenate integers like that: you need to do it through some elementary math.

Think of "combining" as multiplication followed by addition:

age[1] = mouse[0]*100 + age[0];

Here is how it works:

A = 23
B = 50, B*100 = 5000

    23
+ 5000
------
  5023

Note that this trick works only when the second number has exactly two digits. When the second number has N digits, you need to multiply the first number by the N-th power of ten before performing the addition.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

age[1] = 1000*mouse[0] + age[0]

If you want a more general solution, one that doesn't depend on mouse[0] being two digits, you could define a function to get the number of digits:

int digits(int num) {
    return ceil(log10(num));
}

Then you could combine the integers like so:

age[1] = ((int)pow(10, digits(mouse[0])))*mouse[0] + age[0];
tlehman
  • 5,125
  • 2
  • 33
  • 51
0

I think you meant age[1]=mouse[0]age[0]; to be age[1]=mouse[0]+age[0]; If you can always be sure of the magnitude of the two numbers (e.g. two digits) then just multiply by 100 to shift the digits over age[1]=mouse[0] * 100 + age[0];

If you don't know the exact digits, then you can calculate how much you have to shift the value over by using log10:

age[1] = mouse[0] * (int)pow(10, (int)log10(age[0])) + age[0];

Constablebrew
  • 816
  • 1
  • 7
  • 23
0

Since OP is doing a string concatenation, covert to text, concatenate and then back to int.

char buf1[64+64];
char buf2[64];
sprintf(buf1, "%d", mouse[0]);
sprintf(buf2, "%d", age[0]);
strcat(buf1, buf2);
age[1] = atoi(buf1);

There are a number of ways to convert int into text, sprintf() is simple one handy one. Also a number of wasy to convert text to an int. You have to watch out for the - sign and buffer overflow.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

It seems you are interested in concatenating the textual representation of two ints.

int mouse = 50;
int age = 23;

char combi[BUFSIZ];/*char array to contain the "combination"*/

/*print two ints together in one c-string*/
snprintf(combi, BUFSIZ, "%d%d", mouse,age);
printf("value = %s\n", combi); /*print as string*/

This would print value = 5023 to your terminal, in some interpreted languages one might concatenate strings more easily and the textual and numerical representation are converted automagically, however in C this needs to be done very carefully.

hetepeperfan
  • 4,292
  • 1
  • 29
  • 47