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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:51:08+00:00 2026-05-12T21:51:08+00:00

I’m much less experienced in C than I am in higher-level languages. At Cisco,

  • 0

I’m much less experienced in C than I am in higher-level languages. At Cisco, we use C, and I sometimes run into something that would be easy to do in Java or Python, but very difficult to do in C. Now is one of those times.

I have a dynamically-allocated array of unsigned integers which I need to convert to a comma-separated string for logging. While the integers are not likely to be very large, they could conceptually be anywhere from 0 to 4,294,967,295 In Python, that’s one short line.

my_str = ','.join(my_list)

How elegantly can people do this in C? I came up with a way, but it’s gross. If anyone knows a nice way to do it, please enlighten me.

  • 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-12T21:51:08+00:00Added an answer on May 12, 2026 at 9:51 pm

    Code now tested and builds under gcc.

    In contrast to other answers, does not mandate C99.

    The real problem here is not knowing the length of the string you’re going to need. Getting a number is as easy as sprintf("%u", *num) using num to walk your array of ints, but how much space are you going to need? To avoid overrunning a buffer, you have to keep track of a lot of integers.

    size_t join_integers(const unsigned int *num, size_t num_len, char *buf, size_t buf_len) {
        size_t i;
        unsigned int written = 0;
    
        for(i = 0; i < num_len; i++) {
            written += snprintf(buf + written, buf_len - written, (i != 0 ? ", %u" : "%u"),
                *(num + i));
            if(written == buf_len)
                break;
        }
    
        return written;
    }
    

    Notice, that I keep track of how much of the buffer I have used and use snprintf so I don’t overrun the end. snprintf will tack on a \0, but since I’m using buf + written I will start at the \0 of the previous snprintf.

    In use:

    int main() {
        size_t foo;
        char buf[512];
    
        unsigned int numbers[] = { 10, 20, 30, 40, 1024 };
    
        foo = join_integers(numbers, 5, buf, 512);
        printf("returned %u\n", foo);
        printf("numbers: %s\n", buf);
    }
    

    Outputs:

    returned 20
    numbers: 10, 20, 30, 40, 1024
    

    Forcing the limiting to kick in, instead of overrunning:

    char buf[15];    
    foo = join_integers(numbers, 5, buf, 14);
    buf[14] = '\0';
    

    Outputs, expectedly:

    returned 14
    numbers: 10, 20, 30, 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How much less libraries are there for Mono than for Java? I lack the
Our application takes significantly more time to launch after a reboot (cold start) than
I would like to try developing ASP.NET and ASP.NET MVC apps in F# .
Just recently I came over an idea called the Application Strangler Pattern . As
(Not to be confused with Xunit , a popular .Net unit testing library.) Today
I've never seen <base> HTML tag actually used anywhere before. Are there pitfalls to
I have a request for a contracting gig and one of the requirements in
I recently installed IE8, and found quirks using the old copies of IE I
This question is a spin-off from this one . Some history: when I first

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.