[英]Is there any compile-time (or sth similar) function in C that will give me format specifier for any primitive type?
我寻求format_spec function。 上下文描述如下。
有没有像这样的编译时 function ?
#include <stdio.h>
#define typeof(x) __typeof(x)
int main(){
int x; /* Plain old int variable. */
typeof(x) y=8; /* Same type as x. Plain old int variable. */
//printing
printf(format_spec(typeof(x)), x);
/*I don't want to use:
printf("%d", y);
I want generic way to get format specifier for any type.*/
}
C 没有通用的格式化方式,仅提供有限的类型自省功能。 对于固定的类型列表,您可以使用_Generic
:
printf(
_Generic(x,
int: "%d\n",
unsigned: "%u\n",
long int: "%ld\n",
char *: "%s\n"),
x);
但是,这很麻烦,通常不是 C 的设计目的。 在 C 中,您应该主要了解您正在使用的类型并避免此类问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.