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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:52:09+00:00 2026-06-03T19:52:09+00:00

I have a format string like this: buf[] = A%d,B%d,C%d,D%d,F%d,G%d,H%d,I%d,J%d; and I want to

  • 0

I have a format string like this:

buf[] = "A%d,B%d,C%d,D%d,F%d,G%d,H%d,I%d,J%d";

and I want to insert the same integer for each %d so I use:

 int val = 6;
 sprintf(buf2, buf, val,val,val,val,val,val,val,val,val);

Can I use sprintf in a way that only requires me to write val once, and sprintf will use it for each %d?

  • 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-06-03T19:52:11+00:00Added an answer on June 3, 2026 at 7:52 pm

    Yes, you can use %1$d everytime. The 1$ references the second argument, you could obviously do it with other arguments, too.

    Demo: http://codepad.org/xVmdJkpN

    Note that the position specifier is a POSIX extension – so it might not work with every single compiler. If you need it to work e.g. with the Visual C++ compiler, consider using the ugly way of repeating the argument or do not use a printf-style function at all. Another option would be using a POSIX-compatible sprintf implementation or using multiple calls to append one number everytime in a loop (in case the format string is built dynamically which would prevent you from specifying the correct number of arguments).


    On a side-note, sprintf should be avoided. Use snprintf(buf2, sizeof(buf2), ....) instead. Of course this requires buf2 to have a static size known at compile-time – but if you allocate it manually you can simply use the variable containing the length instead of sizeof(buf2).

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

Sidebar

Related Questions

I have a string.Format like this : string Test = string.Format({0:#,0}, NegativeNumber); how can
I have a date/time string like this: 180510_112440 in this format ddmmyy_hhmmss I need
I have a string like this (YYYYMMDD): 20120225 And I want to have a
I have an URL like this: http://site.com/images/961b7d0e50f0462a8828d31bf0874a71/lightbox I need to string.format replace the GUID
I have datetime data in string format like this: Sat Mar 24 23:59:59 GMT
I want to format the Timespan to have format like this 49 hr 34
I have a string like this mystr = K1_L1_L2 XX I want to break
I have a string like this: 250920111414 I want to create a DateTime object
I have a url in string format like this : str=http://code.google.com and some other
I have a string in the format: 'nn.nnnnn' in Python, and I'd like to

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.