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 83183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:44:01+00:00 2026-05-10T21:44:01+00:00

The Daily WTF for 2008-11-28 pillories the following code: static char *nice_num(long n) {

  • 0

The Daily WTF for 2008-11-28 pillories the following code:

static char *nice_num(long n) {     int neg = 0, d = 3;     char *buffer = prtbuf;     int bufsize = 20;      if (n < 0)     {         neg = 1;         n = -n;     }     buffer += bufsize;     *--buffer = '\0';      do     {         *--buffer = '0' + (n % 10);         n /= 10;         if (--d == 0)         {             d = 3;             *--buffer = ',';         }     }     while (n);      if (*buffer == ',') ++buffer;     if (neg) *--buffer = '-';     return buffer; } 

How would you write it?

  • 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. 2026-05-10T21:44:02+00:00Added an answer on May 10, 2026 at 9:44 pm

    If you’re a seasoned C programmer, you’ll realize this code isn’t actually that bad. It’s relatively straightforward (for C), and it’s blazingly fast. It has three problems:

    1. It fails on the edge case of LONG_MIN (-2,147,483,648), since negating this number produces itself in twos-complement
      • It assumes 32-bit integers – for 64-bit longs, a 20-byte buffer is not big enough
      • It’s not thread-safe – it uses a global static buffer, so multiple threads calling it at the same time will result in a race condition

    Problem #1 is easily solved with a special case. To address #2, I’d separate the code into two functions, one for 32-bit integers and one for 64-bit integers. #3 is a little harder – we have to change the interface to make completely thread-safe.

    Here is my solution, based on this code but modified to address these problems:

    static int nice_num(char *buffer, size_t len, int32_t n) {   int neg = 0, d = 3;   char buf[16];   size_t bufsize = sizeof(buf);   char *pbuf = buf + bufsize;    if(n < 0)   {     if(n == INT32_MIN)     {       strncpy(buffer, '-2,147,483,648', len);       return len <= 14;     }      neg = 1;     n = -n;   }    *--pbuf = '\0';    do   {     *--pbuf = '0' + (n % 10);     n /= 10;     if(--d == 0)     {       d = 3;       *--pbuf = ',';     }   }   while(n > 0);    if(*pbuf == ',') ++pbuf;   if(neg) *--pbuf = '-';    strncpy(buffer, pbuf, len);   return len <= strlen(pbuf); } 

    Explanation: it creates a local buffer on the stack and then fills that in in the same method as the initial code. Then, it copies it into a parameter passed into the function, making sure not to overflow the buffer. It also has a special case for INT32_MIN. The return value is 0 if the original buffer was large enough, or 1 if the buffer was too small and the resulting string was truncated.

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

Sidebar

Related Questions

Almost daily I come across code of the following form: string x = foo
You often see, on sites like The Daily WTF , examples of overengineered code
Daily there are many questions of the following type on SO: How do I
I want to take daily backup of my DB for that I use following
I stumbled upon this code: static void Main() { typeof(string).GetField(Empty).SetValue(null, evil);//from DailyWTF Console.WriteLine(String.Empty);//check //how
I have the following file dumped daily into one of our online directories: dat-part2-489359-43535-toward.txt
I'm considering a daily script to do the following, in order to account for
I was recently on The Daily WTF when I came across this old post
Today's Daily Vim says this: Assuming you're using the bash shell, the following can
I have a MySql table with daily stock market data in the following order:

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.