Extract format specifier from data type?
Is it possible to programmatically infer the format specifier for a data type? For example, if printing takes a long time, it automatically does something like:
printf("Vlaue of var is <fmt_spec> ", var);
I also feel that this will reduce some of the bugs for the developer part as 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 last two have warnings, but would it be better if there were errors, since this would be problematic in most cases? Or am I missing something or are there constructions already created?
source to share
Derive data format specifier from data type?
Not.
As Melpoman stated:
"Format specifications, not only for the types, for example,. %o
And %x
both take unsigned int
, %e
, %f
, %g
all take double
, %d
, %i
, %c
everyone accepts int
So you can not (in general) put them out of arguments.."
The point is that if such functionality existed, could it output unsiged int
to %o
or %x
, for example? Etc.,.
As to whether certain cases should throw a warning or a problem, you should think about how casting works in c and when it makes sense to allow something or not. In GCC, you can of course treat the warning 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 .
source to share
Is it possible to programmatically infer the format specifier for a data type?
Not easy or direct from printf()
, but ...
Yes, with restrictions on choosing a set of types using _Generic
.
This can be done in a variety of ways and can be used with *printf()
great value, but I found a similar approach to printing data without specifying separate format specifiers in this example:
Formatted printing without specifying type matching specifiers using _Generic
Note. This code has a coding hole related to pointer math, which I have fixed - although not posted.
GPrintf("Name is ", GP(name), " is ", GP(dob), GP_eol);
The key was to be used _Generic(parameter)
to control the selection of the code used to convert the type to text by adding a macro GP(x)
to 2 parts: strings and x
. Then it GPrintf()
interprets the arguments.
This is similar to @ MichaΓ«l Roy's comment , but staying in C and not C ++.
source to share