Zoso
Zoso

Reputation: 3465

Deduce format specifier from data type?

Is it possible to deduce the format specifier programmatically for a data type? For instance if the print is for a long it automatically does something like:

printf("Vlaue of var is <fmt_spec> ", var);

I also feel it would reduce some errors on part of developer since something like

printf("Name is %s",int_val); //Oops, int_val would be treated as an address

printf("Name is %s, DOB is",name,dob); // missed %d for dob

printf("Name is %s DOB is %d", name);//Missed printing DOB

I understand that the latter two do have warnings but wouldn't it be better if errors were thrown since in most cases it is going to be problematic? Or am I missing something or are there constructs already in place to do so ?

Upvotes: 3

Views: 859

Answers (2)

chux
chux

Reputation: 154542

Is it possible to deduce the format specifier programmatically for a data type?

Not easily nor directly with printf(), yet...

Yes, with limitations to a select set of types, by using of _Generic.

This could be done various ways and used with *printf() with great dificulty, yet I found a similar approach to print data, without specifying individual format specifiers in this example:
Formatted print without the need to specify type matching specifiers using _Generic
Note: This code has a coding hole concerning pointer math, that I have since patched - though not posted.

GPrintf("Name is ", GP(name), " is ", GP(dob), GP_eol);

The key was to use _Generic(parameter) to steer the selection of the code used to convert the type to text by having the macro GP(x) expand to 2 parts: a string and x. Then GPrintf() interprets the arguments.
This is akin to @Michaël Roy's comment, yet staying in C rather than C++.

Upvotes: 1

gsamaras
gsamaras

Reputation: 73444

Deduce format specifier from data type?

No.

As melpomene stated:

"Format specifiers aren't just for types. E.g. %o and %x both take unsigned int; %e, %f, %g all take double; %d, %i, %c all take int. That's why you can't (in general) deduce them from the arguments."

Point is that if such a functionality existed, then would it deduce unsiged int to %o or %x, for example? And so on . . .


About whether some cases should issue a warning or an issue, you should think about how casting works in , and when it does make sense to allow something or not. In GCC, you could of course treat warning(s) as error(s):

-Werror
Make all warnings into errors.

-Werror=
Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors. This switch takes a negative form, to be used to negate -Werror for specific warnings; for example -Wno-error=switch makes -Wswitch warnings not be errors, even when -Werror is in effect.

The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)

Note that specifying -Werror=foo automatically implies -Wfoo. However, -Wno-error=foo does not imply anything.

as you can read here.

Upvotes: 2

Related Questions