472,131 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

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,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
7 5256
Ganon11
3,652 Expert 2GB
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
9,208 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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.

Similar topics

11 posts views Thread by John Lenton | last post: by
reply views Thread by Josiah Carlson | last post: by
3 posts views Thread by J. Muenchbourg | last post: by
6 posts views Thread by Stuart McGraw | last post: by
5 posts views Thread by siliconwafer | last post: by
18 posts views Thread by Money | last post: by
6 posts views Thread by gokhalen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.