Inserting format specifier from variable string value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codinginc
    New Member
    • Sep 2008
    • 3

    #1

    Inserting format specifier from variable string value

    Hi Everyone,
    Is it possible to insert format specifier in a form of variable. See example below
    Standard from

    char result[100];
    float fnum = 3.14159;
    sprintf( result, "%f", fnum );

    My question is

    char specifier = "%f"; < - or specifier defined from input such as scanf(specifier )
    char result[100];
    float fnum = 3.14159;
    sprintf( result,specifie r, fnum );

    I tried that way and it doesnt work. My question is, is it possible or not. If it is, whats the trick? Thanks.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    It might be, but I don't think "%f" is a character, it looks more like 3 characters: '%', 'f', and '\0'.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      There's no trick. The function argument is a const char* so you should be able to build your string at run-time and then call the function with your string.

      However, the ... arguments must be known at compile time so as long as you use the same ... arguments then it should work. That is, you cannot vary the number of arguments at run time. But even here you should be able to write a function with various sprintf calls to cover your needs and control this by having the function parse the specifier and select the correct sprintf call.

      If you go down this road, write your sprintf calls with exactly one specifier argument and pass in a pointer to the variable to display. The function can make the sprintf call by de-referencing the argument. Since there is one argument, you won't need a specifier string at all.


      This code, however:
      Originally posted by codinginc
      char specifier = "%f"; < - or specifier defined from input such as scanf(specifier )
      won't compile. specifier must be a char* and not a char.

      Comment

      • codinginc
        New Member
        • Sep 2008
        • 3

        #4
        Originally posted by weaknessforcats
        There's no trick. The function argument is a const char* so you should be able to build your string at run-time and then call the function with your string......

        won't compile. specifier must be a char* and not a char.
        Thanks for reply,
        I fixed code by changing char specifier to const char* specifier;
        however,
        sprintf(a,speci fier,b); will not compile because sprintf cannot parse specifier variable, it doesnt read the string value inside the specifier.
        I guess the only option is to modify the sprintf funtion itself.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by weaknessforcats
          However, the ... arguments must be known at compile time
          Actually even that isn't strictly speaking true if you are willing to go for a non-portable bodgy hack, you can put your arguments into an array and pass that on the stack allowing you to build differing argument lists at run time.

          However I would not necessarily recommend doing that, it is probably one of those things that crosses the line between what you can do and what you should do.


          codinginc
          Code:
              char specifier[] = "%f";
              char result[100];
              float fnum = 3.14159;
          
              sprintf( result,specifier, fnum );
          works fine for me as does declaring

          const char *specifier = "%f";

          You should not be thinking about "changing" sprintf it is more likely that there is an error in your code than there is an error in the standard library functions that have been around for 30+ years.

          Perhaps you should post the actual code you are compiling?

          Comment

          • codinginc
            New Member
            • Sep 2008
            • 3

            #6
            This what I get as an error message

            WARNING (251) Can't preprocess format to (s)printf

            using exact code mentioned on a previous thread.

            char specifier[] = "%f";
            char result[100];
            float fnum = 3.14159;

            sprintf( result,specifie r, fnum );

            I am using zilog c compiler. Maybe its just a compiler. I'll try another compiler just for comparison. (MSF VC++)

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              This compiles OK using Visual Studio.NET 2008. I did have to change your 3.14159 from a double to a float: 3.14159F and I had to suppress Microsoft's deprecation warning on this function.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by codinginc
                WARNING (251) Can't preprocess format to (s)printf
                I believe this is an ignorable (or or you can switch off) compiler diagnostic. Some compilers read the format string and then check that the arguments supplied as the parameters to (s)printf match the format string.

                It you are passing a variable as the format string then it can't do that, hence the diagnostic.

                Actually baring the bodgy hack I mentioned earlier I am struggling thinking of situations where you would need a variable format string but fixed parameters.

                Comment

                Working...