-1

I have

int main ()
{
    int x=69057;
    int y=23
    printf("%d", x);
    printf("%d", y);
    return 0;
}

And it prints 6905723. How can I convert the printed number into an integer? I can't do

int z=6905723

since in the original program I don't know what value x and y have.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • Have you tried this? If so what was the result. If not - give it a go – Ed Heal May 30 '16 at 19:29
  • 1
    `char buffer[SUFFICIENT_SIZE]; sprintf(buffer, "%d%d", x,y);int number = strtol(buffer, NULL, 0);`? – EOF May 30 '16 at 19:30
  • 2
    Why do you want to convert the printed output to anything? Just do a simple arithmetic on the numbers. – Eugene Sh. May 30 '16 at 19:36
  • And be sure to look at *man strtol* for proper methods to validate the conversion. An alternative following the `sprintf` call is to call `sscanf` on `buffer` (e.g. `int z; if (sscanf (buffer, "%d", &z) == 1) printf ("z : %d\n", z);` Also agree with Eugene `int z = x * 100 + y;` works as well. – David C. Rankin May 30 '16 at 19:37
  • 1
    If you have the number printed, you apparently have the "original" integer variable, so why not use that one? Sorry, but do you know yourself what you actually want to accomplish? If yes, elaborate in your question. (And the tag makes clear already you don't use Plankalkül). – too honest for this site May 30 '16 at 19:40
  • Ok, your pair of `printf()` prints as if the first number was multiplied by 100 (right shifted in decimal system), than the second number was added. You can do this by printing to a string and converting to `long`, see previous comment. – user3078414 May 30 '16 at 19:40

4 Answers4

0

What you essentially want to do here is multiply x by the smallest power of 10 that's larger than y, and then add y to it.

Assuming x and y are small enough to not overflow the long result, you could do something like this:

int x = 69057;
int y = 23
int temp = y;
long result = x;
while (temp > 0) {
    result *= 10;
    temp /= 10;
}
result += y;

printf("%ld\n", result);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Here's how you can get the answer into int z. It works even if you don't know x and y before. You just need to make sure that the answer will actually fit into an int.

int main ()
{
    int x=69057;
    int y=23
    char buff[512];
    sprintf(buff, "%d%d", x, y);
    int z = atoi(buff); /* Now z is equal to 6905723 */
    return 0;
}

You will run into a problem if y is a negative number, but it isn't clear from your question what you'd like to happen in that situation.

sunwo
  • 11
  • 3
  • I'm new here. Why am I being downvoted? I was just trying to help. – sunwo May 30 '16 at 19:55
  • I don't know. But you should really consider avoiding atoi in favor of strtol, because you can check if it failed. https://stackoverflow.com/questions/17710018/why-shouldnt-i-use-atoi Don't forget that long type should be used here to compute the result. – 2501 May 31 '16 at 07:50
0

try this. very easy and simple

#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
    int x,y,z,a,b;
    x=69057;
    y=23;
    printf("x=%d y=%d\n",x,y);
    a = log10(y) + 1;
    for (b=0;b<a;b++)
    {
        x*=10;
    }
    z=x+y;
    printf("z=%d",z);
    return 0;
}
mssirvi
  • 147
  • 3
  • 14
-1

You can write a function to do it for you:

#include <stdio.h>

int my_function(int x, int y);

int main()
{
    int x = 69057;
    int y = 23;
    int z = my_function(x, y);
    printf("%d\n", z);
    return 0;
}

int my_function(int x, int y)
{
    int tmp = y;
    do {
        x *= 10;
    } while (tmp /= 10);
    return x + y;
}
machine_1
  • 4,266
  • 2
  • 21
  • 42