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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:03:44+00:00 2026-06-18T16:03:44+00:00

I’ve been using C# for the last few years, and right now I’m trying

  • 0

I’ve been using C# for the last few years, and right now I’m trying to write some C. I’m trying to format a string from an array of values. The “format string” and the array aren’t known until runtime.

In C# I can invoke a variadic function with an array, like this:

using System;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            string formatString = "{0}.{1}.{2}.{3}";
            string[] formatValues = new[] { "a", "b", "c", "d" };

            string formatted = String.Format(formatString, formatValues);

            //Do something with formatted (now looks like "a.b.c.d")
        }
    }
}

In C I’ve got this:

#include <stdio.h>
#include <malloc.h>

    int main(int argc, char* argv[])
    {
        char *formatString = "%s.%s.%s.%s";
        char *formatValues[] = {"a","b","c","d"};

        char *buffer = (char*)malloc(100 * sizeof(char));

        //This doesn't work.....
        sprintf(buffer, formatString, formatValues);

        //... buffer is junk

        return 0;
    }

How can I do this in C?

(Is there a nice function in the C standard library I can use to help me, or perhaps, is there a way to call a varargs function with an array?)

Please note, the number of arguments will never be greater than the length of the array I have. And the types will always be strings. So I might have

char *formatString = "My Formatted String %s.%s.%s";
char *formatValues[] = {"a","b","c","d","e"};

But I’ll never have too few %s.

Note: the C has to work on GCC for Linux, and Visual Studio for Windows (C90).

  • 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-18T16:03:45+00:00Added an answer on June 18, 2026 at 4:03 pm

    I don’t think C provides a standardized way to do this. If you understood the internal implementation of <stdarg.h> on your system, it would probably be possible to kludge up a system-specific solution involving vprintf(3), but I have a different, conforming kludge for you…

    Something that would work would involve declaring an array of some large size, set the values that you have, and then just pass every element of the array at the call site, regardless of whether they are set.

    char *a[5]; // or a[50], whatever you need
    
    // assign the elements you actually have
    
    printf(format_string, a[0], a[1], a[2], a[3], a[4], a[5]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using an ASP request returning a XML file containing some latin characters. By
I am using jsonparser to parse data and images obtained from json response. When

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.