从数据类型推导格式说明符?

佐佐

是否可以通过编程方式推断出数据类型的格式说明符?例如,如果打印很长时间,它将自动执行以下操作:

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

我也觉得这会减少部分开发人员的错误,因为类似

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

我知道后两者确实有警告,但是如果抛出错误会不会更好,因为在大多数情况下会出现问题?还是我错过了一些东西,或者已经有适当的构造可以做到这一点?

萨马拉斯

从数据类型推导格式说明符?

没有

正如美洛美酮所说:

“格式说明不只是类型EG。%o而且%x都接受unsigned int; %e%f%g都把double; %d%i%c都以int。这就是为什么你不能(一般)演绎他们的观点。”

关键是,如果存在这样的功能,那么它会推论unsiged int%o%x吗?等等 。


关于某些情况下应该发出警告还是问题,您应该考虑在强制转换的工作方式以及何时允许某些内容有意义。在GCC中,您当然可以将警告视为错误:

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

如您在这里阅读的

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章