convert int to char* in standard C (without itoa)

22,545

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.

Share:
22,545
Alexandru N. Onea
Author by

Alexandru N. Onea

I have always loved technology and the way it helps creating a better world, and I have always focused my passion on the study of Artificial Intelligence and electronics (mostly because I like the idea of these two going together well). However, my true passions is not related to any particular field, but rather to an idea, a dream of mine, of a world where each and every person contributes with their skills and knowledge to the advancement of the human kind, a world where each and every city is a truly democratic city and their citizens are part of the decision making processes, of an educational system where students are taught not to remember, but to discover, to explore and to create, where students are invited to collaborate and innovate. My passion is to make every small step, to meet every brilliant mind, and together to try and fail until all this puzzle is finally completed.

Updated on July 09, 2022

Comments

  • Alexandru N. Onea
    Alexandru N. Onea almost 2 years

    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

  • Fred Foo
    Fred Foo over 12 years
    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.
  • ThiefMaster
    ThiefMaster over 12 years
    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.
  • Fred Foo
    Fred Foo over 12 years
    fair enough, but given that you were making corrections already, I though you might want to add these in as well :)
  • Daniel Fischer
    Daniel Fischer over 12 years
    15 characters is not enough for long on most 64-bit platforms, it might be good to drop the habit of thinking long == int.
  • ThiefMaster
    ThiefMaster over 12 years
    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.
  • ugoren
    ugoren over 12 years
    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?
  • user411313
    user411313 over 12 years
    snprintf is only C99, not ANSI C, str = malloc(BUFSIZE) is wrong, right is str = malloc(BUFSIZ)
  • ThiefMaster
    ThiefMaster over 12 years
    @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.
  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE over 12 years
    ANSI C is^H^Hwas C99. Now it's C11.