473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 5403
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

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

Similar topics

13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
11
by: John Lenton | last post by:
Is there any reason python's printf-style formatting is missing the (C99) '%a' specifier? I'm sorry if this has been asked and answered before; I can't find it on google ('%a' is a very awkward...
0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
3
by: J. Muenchbourg | last post by:
while inserting new records into SQL, i'm using the folloinwg sqlstatement> Dim MySQL as string = "Insert into roster (pname, pnotes, thedate) values (@pname, @pnotes, @thedate)" the sql...
6
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string...
5
by: siliconwafer | last post by:
Hi All, What does a 'format specifier' do? Suppose I do, int a = 43; //decimal number printf("%x",a); I will get hex equivalent of 43. Does the format specifier do an implicit "decimal to hex"...
18
by: Money | last post by:
Here in this thread http://groups.google.co.in/group/comp.lang.c/browse_frm/thread/c16d280238a95c9/e3b0dbf76f3e02e3?q=finding+endianness&rnum=1#e3b0dbf76f3e02e3 Tydr Schnubbis in 3rd reply used...
6
by: gokhalen | last post by:
How can I print between column number 8 and column number 80?. Is there a format specifier that will do it easily?. I am printing an array of doubles and I want the number to be printed on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.