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

  • Home
  • SEARCH
  • 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 1061669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:30:27+00:00 2026-05-16T18:30:27+00:00

Whilst reading through K&R, I came across the integer to string function. I gave

  • 0

Whilst reading through K&R, I came across the integer to string function. I gave it a quick read, and decided to implement it myself, but instead of printing, it updates a character array.

Here is what I have

void inttostr(int number, char str[]) {
    static int i;  
    if (number / 10) {
        inttostr(number / 10, str); 
    }       
    str[i++] =  number % 10 + '0';      
}

It seemed to work for the few integers I gave it, but I have some questions.

  1. I haven’t explicitly included the nul byte \0 at the end, so why does the string work fine when printed with printf("%s\n", str);?
  2. I don’t think I’m very good at thinking recursively. When I try and step through the program in my mind, I lose track of what is still awaiting execution. Is there a better way of seeing what is happening internally, to help me learn?
  3. Any other suggestions on the code?

I’m using Xcode.

This is not homework. I’m just learning.

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-16T18:30:28+00:00Added an answer on May 16, 2026 at 6:30 pm

    You’re correct that you’re never writing NUL, which is a bug.

    In general, you don’t have to think through the entire solution. You just have to make sure every step is correct. So in this case, you say:

    1 . inttostr(number / 10, str);

    will take care of all but the last digit.

    2 . Then I will take care of the last one.

    You can trace what’s happening, though. For e.g. 54321 it looks like:

    inttostr(54321, str); // str = ...;
    inttostr(5432, str); // str = ...;
    inttostr(543, str); // str = ...;
    inttostr(54, str); // str = ...;
    inttostr(5, str); // str = ...;
    str[0] = '5'; // str = "5...";
    str[1] = '4'; // str = "54...";
    str[2] = '3'; // str = "543...";
    str[3] = '2'; // str = "5432...";
    str[4] = '1'; // str = "54321...";
    

    Note that when you don’t return from any of the functions until you write the first character, then you return in the opposite order from the calls.

    The … signifies that you haven’t NUL-terminated. Another issue is that you’re using a static variable, so your code isn’t reentrant; this means it breaks in certain scenarios, including multi-threading.

    To address the reentrancy and NUL issue, you can do something like the code below. This creates a helper function, and passes the current index to write.

    void inttostr_helper(int number, char str[], int *i)
    {
        if (number / 10) {
            inttostr_helper(number / 10, str, i); 
        }       
        str[(*i)++] =  number % 10 + '0';
        str[*i] = '\0';
    }
    
    void inttostr(int number, char str[])
    {
      int i = 0;
      inttostr_helper(number, str, &i);
    }
    

    EDIT: Fixed non-static solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whilst reading jQuery Cookbook (Oreilly) last night I came across an each function that
Whilst reading through the book The well grounded Rubyist, I came across some strange
Whilst reading through the DirectWrite source code I came across the following struct: ///
I came across this code today whilst reading Accelerated GWT (Gupta) - page 151
Whilst trawling through some old code I came across something similar to the following:
Whilst working on some generally horrible Javascript code this morning, I came across the
Whilst reviewing some Qt C++ code I came across this: class Foo { Q_OBJECT
I have been reading a lot about test-driven development and decided that I want
Whilst digging through the STL sources (DinkumWare, SGI, STLport, etc..) and trying to make
I'm refactoring some code in project whilst reading Object-Oriented Reengineering Patterns specifically the section

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.