3

I have declared and initialized two variables as shown below:

int a=5;
char* str;
str = (char*)calloc(255, sizeof(char));

I want to convert the int to char* in standard C. I cannot use any conversion function from C++ such itoa.

I am using Ubuntu 11.10

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Alexandru N. Onea
  • 423
  • 2
  • 6
  • 18
  • 1
    @rogelware: `itoa` is not a standard C function and is not available on Linux. – Fred Foo Jan 07 '12 at 14:37
  • What do you mean by standard C function? It's is present at .. Do you mean you can't use includes? (just checked, yes, itoa is not standard) – Rogel Garcia Jan 07 '12 at 14:39
  • @rogelware: it's not present in `` on all platforms and it's not present in the C standard. – Fred Foo Jan 07 '12 at 14:39
  • Checking the reference, the function to be used is `sprintf` like [ThiefMaster](http://stackoverflow.com/users/298479/thiefmaster) suggested. Thanks for the explanation. – Rogel Garcia Jan 07 '12 at 14:42

1 Answers1

16

First of all, itoa is not a C++ thing.

You can simply use sprintf:

sprintf(str, "%d", a)

In a real application you'll want to use snprintf though to remove the risk of a buffer overflow:

str = malloc(16);
snprintf(str, 16, "%d", a);

And 15 characters are way enough to store an integer.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 3
    `sizeof(char)` is per definition equal to 1, and `calloc` is overkill. Also, casting the result from `calloc` is useless. Just `str = malloc(BUFSIZE)` would suffice. – Fred Foo Jan 07 '12 at 14:37
  • Yeah, just copied it from his question. However, there are many people who always use calloc instead of malloc even if one of the arguments is `1` since it nulls the memory and saves the hassle of doing a memset after the allocation. – ThiefMaster Jan 07 '12 at 14:38
  • fair enough, but given that you were making corrections already, I though you might want to add these in as well :) – Fred Foo Jan 07 '12 at 14:39
  • 1
    15 characters is not enough for long on most 64-bit platforms, it might be good to drop the habit of thinking long == int. – Daniel Fischer Jan 07 '12 at 14:46
  • If it's `int`, 15 chars should be sufficient. But you are right if it can be a `long` or `long long`. Then he needs a 21+ char buffer. – ThiefMaster Jan 07 '12 at 14:49
  • 2
    The answer is correct, but I wouldn't mention the existence of `sprintf`. It's good that you suggest `snprintf` for a real application, but why not use it for "unreal" ones as well? – ugoren Jan 07 '12 at 15:36
  • snprintf is only C99, not ANSI C, `str = malloc(BUFSIZE)` is wrong, right is `str = malloc(BUFSIZ)` – user411313 Jan 07 '12 at 19:05
  • @user411313: 1) The OP did not ask for ANSI C. 2) `BUFSIZE` is perfectly fine when defined by yourself - and I did define `BUFSIZE` in my answer. 3) Please get a nickname, the default user12345 nicks are annoying. – ThiefMaster Jan 07 '12 at 19:47
  • ANSI C is^H^Hwas C99. Now it's C11. – R.. GitHub STOP HELPING ICE Jan 07 '12 at 21:33