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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:56:41+00:00 2026-05-14T00:56:41+00:00

I would like to use the syntax that printf uses, using the %d, %s

  • 0

I would like to use the syntax that printf uses, using the %d, %s and adding values after to assign a value to a char[]. Is this possible?

e.g. Given an output of:

printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

I’d like to assign that to char[] output;

How can this be done?

I tried:

sprintf(output, "now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

but that didn’t seem to work. Is sprintf used differently… or is that not what I should be using?

Thanks!

EDIT: It turned out I simply needed to increase the initialized length of output…

  • 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-14T00:56:41+00:00Added an answer on May 14, 2026 at 12:56 am

    That should work fine provided you have enough space in the output buffer (at least 26 bytes) to take the largest string you’ll create:

    char output[26];
    sprintf(output, "now: %d-%d-%d %d:%d:%d\n",
        tm.tm_year + 1900,
        tm.tm_mon + 1,
        tm.tm_mday,
        tm.tm_hour,
        tm.tm_min,
        tm.tm_sec);
    

    You need:

    • 5 for "now: ".
    • 4 for the year (for at least the next 79 centuries).
    • 2 each for the other five numerics.
    • 5 for the separators ("-- ::").
    • 1 for the newline.
    • 1 for the terminating null character.

    You may also want to consider changing the format string to "now: %04d-%02d-%02d %02d:%02d:%02d" so that the datetimes will line up if you’re outputting them, to avoid something like:

    2009-12-31 23:59:58 blah blah blah
    2010-1-1 0:0:7 yada yada yada
    

    when

    2009-12-31 23:59:58 blah blah blah
    2010-01-01 00:00:07 yada yada yada
    

    looks so much better. You’ll notice I’ve left the "\n" off the end – since this is the sort of code you see a lot in logging, you’d want the datetime to be in a format you could easily append to, something like:

    fprintf (logFile, "%s Cannot write to '%s', error = %d\n",
        output, fileSpec, errno);
    

    If I’ve misunderstood what you’re using this for, feel free to ignore the last few paragraphs.

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

Sidebar

Ask A Question

Stats

  • Questions 485k
  • Answers 485k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the built-in getattr() function: class Foo: def bar1(self): print(1)… May 16, 2026 at 7:45 am
  • Editorial Team
    Editorial Team added an answer You can use javax.swing.JTextPane Check TextSamplerDemo You can find plenty… May 16, 2026 at 7:45 am
  • Editorial Team
    Editorial Team added an answer Use http://secrets-for-android.googlecode.com/svn/trunk/ location to check out. Read the page http://code.google.com/p/secrets-for-android/source/checkout… May 16, 2026 at 7:45 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.