-1

I want to convert an int to a string without printing anything on my screen. For now I used sprintf, but this also printed the int to my screen. Also itoa is not supported by my compiler so I can't use that either.

cpu20
  • 1
  • 1
  • 2
    Please show your code. Usually `sprintf` does *not* "print to screen", so there must be something you added. It would also help if you add a tag for your programming language. – Jongware May 04 '14 at 12:21

1 Answers1

-1

I assume You use ANSI C.

You cannot use itoa because it's not a standard function.

sprintf or snprintf is dedicated to that.

Since You do not want to use sprintf, make your own itoa instead:

#include <stdio.h>

char* itoa(int i, char b[]){
    char const digit[] = "0123456789";
    char* p = b;
    if(i<0){
        *p++ = '-';
        i *= -1;
    }
    int shifter = i;
    do{ //Move to where representation ends
        ++p;
        shifter = shifter/10;
    }while(shifter);
    *p = '\0';
    do{ //Move back, inserting digits as u go
        *--p = digit[i%10];
        i = i/10;
    }while(i);
    return b;
}

original answer: here

Community
  • 1
  • 1
krzakov
  • 3,871
  • 11
  • 37
  • 52