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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:56:08+00:00 2026-05-28T22:56:08+00:00

I try to convert a unsigned long into a character string, appending a comma

  • 0

I try to convert a unsigned long into a character string, appending a comma at the end of the it. When compiling and running the test code you can find below, I get the following output:

"1234," "1234"
"1234"

The test code is:

#include <cstdio>
#include <iostream>

int main () {

    unsigned long c = 1234;
    char ch[50];
    char ch1[50];

    sprintf(ch, "%lu,", c);
    std::cout << "\"" << ch << "\"" << " \"" << c << "\"" << std::endl;

    snprintf(ch1, 5, "%s", ch); 
    std::cout << "\"" << ch1 << "\"" << std::endl;

    return 1;
}

As far as I understand, ch should be of length 5, 4 digits plus 1 for the comma.

Do I miss an extra plus one for the termination character?

Cheers!

  • 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-28T22:56:08+00:00Added an answer on May 28, 2026 at 10:56 pm

    The size that is passed to snprintf includes the terminating null character. Although it is not printed, it still takes space in the buffer.

    You should pass strlen(ch) + 1 instead. Or even better, just sizeof(ch1) will suffice since you want to accommodate the entire result before filling the buffer.

    Also make sure that the destination buffer is always of a sufficient size, equal to or greater than the size you pass to snprintf. In your particular case it can hardly happen, but in general you should keep that in mind.

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

Sidebar

Related Questions

We try to convert from string to Byte[] using the following Java code: String
I try to convert a string into a date in t-sql. However get results
I try to convert a ASN.1 Byte Array into a String with Java. My
I try to include unsigned char variable into std::string.it throws compiletime error. unsigned char*
I want to try to convert a string to a Guid, but I don't
I really try to find on google how can we convert .doc and .txt
I try to convert keystrokes into chracters. In other question someone recommand to use
I try to convert a part of code from VBScript to C#. I have
I try to convert an svg into PNG. the svg document is coming from
I am try to convert the following NHibernate query using dyanmic instantiation into an

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.