Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 979237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:11:15+00:00 2026-05-16T04:11:15+00:00

I was wondering if my implementation of an itoa function is correct. Maybe you

  • 0

I was wondering if my implementation of an “itoa” function is correct. Maybe you can help me getting it a bit more “correct”, I’m pretty sure I’m missing something. (Maybe there is already a library doing the conversion the way I want it to do, but… couldn’t find any)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char * itoa(int i) {
  char * res = malloc(8*sizeof(int));
  sprintf(res, "%d", i);
  return res;
}

int main(int argc, char *argv[]) {
 ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:11:16+00:00Added an answer on May 16, 2026 at 4:11 am

    The only actual error is that you don’t check the return value of malloc for null.

    The name itoa is kind of already taken for a function that’s non-standard, but not that uncommon. It doesn’t allocate memory, rather it writes to a buffer provided by the caller:

    char *itoa(int value, char * str, int base);
    

    If you don’t want to rely on your platform having that, I would still advise following the pattern. String-handling functions which return newly allocated memory in C are generally more trouble than they’re worth in the long run, because most of the time you end up doing further manipulation, and so you have to free lots of intermediate results. For example, compare:

    void delete_temp_files() {
        char filename[20];
        strcpy(filename, "tmp_");
        char *endptr = filename + strlen(filename);
        for (int i = 0; i < 10; ++i) {
            itoa(endptr, i, 10); // itoa doesn't allocate memory
            unlink(filename);
        }
    }
    

    vs.

    void delete_temp_files() {
        char filename[20];
        strcpy(filename, "tmp_");
        char *endptr = filename + strlen(filename);
        for (int i = 0; i < 10; ++i) {
            char *number = itoa(i, 10); // itoa allocates memory
            strcpy(endptr, number);
            free(number);
            unlink(filename);
        }
    }
    

    If you had reason to be especially concerned about performance (for instance if you’re implementing a stdlib-style library including itoa), or if you were implementing bases that sprintf doesn’t support, then you might consider not calling sprintf. But if you want a base 10 string, then your first instinct was right. There’s absolutely nothing “incorrect” about the %d format specifier.

    Here’s a possible implementation of itoa, for base 10 only:

    char *itobase10(char *buf, int value) {
        sprintf(buf, "%d", value);
        return buf;
    }
    

    Here’s one which incorporates the snprintf-style approach to buffer lengths:

    int itobase10n(char *buf, size_t sz, int value) {
        return snprintf(buf, sz, "%d", value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if anyone can recommend a good C++ tree implementation, hopefully one that
I was just wondering if there is a clean implementation to getting elements before
I'm just wondering if there is a ready implementation of zip-function in the standart
I'm just wondering about an implementation detail of Scala generics. In C# one can
I've been wondering about the implementation of charAt function for String/StringBuilder/StringBuffer in java what
I've been writing an implementation of malloc and was wondering if someone could help
I know mutex can be an implementation, however I'm wondering there would be a
This is more like a knowledge question than actual implementation. I was wondering if
just wondering if anyone knows of a truly restful Put/delete implementation asp.net mvc preview
I've written a DES implementation as an exercice and am now wondering if and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.