By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,112 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,112 IT Pros & Developers. It's quick & easy.

Inserting format specifier from variable string value

P: 3
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,specifier, fnum );

I tried that way and it doesnt work. My question is, is it possible or not. If it is, whats the trick? Thanks.
Sep 21 '08 #1
Share this Question
Share on Google+
7 Replies


Ganon11
Expert 2.5K+
P: 3,652
It might be, but I don't think "%f" is a character, it looks more like 3 characters: '%', 'f', and '\0'.
Sep 21 '08 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
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:
char specifier = "%f"; < - or specifier defined from input such as scanf(specifier)
won't compile. specifier must be a char* and not a char.
Sep 21 '08 #3

P: 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......

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,specifier,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.
Sep 22 '08 #4

Banfa
Expert Mod 5K+
P: 8,950
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
Expand|Select|Wrap|Line Numbers
  1.     char specifier[] = "%f";
  2.     char result[100];
  3.     float fnum = 3.14159;
  4.  
  5.     sprintf( result,specifier, fnum );
  6.  
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?
Sep 22 '08 #5

P: 3
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,specifier, fnum );

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

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Sep 22 '08 #7

Banfa
Expert Mod 5K+
P: 8,950
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.
Sep 22 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.