我可以从 C 中的变量中检索数据类型吗? [英] Could I retrieve the datatype from a variable in C?

查看:28
本文介绍了我可以从 C 中的变量中检索数据类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 C 中获取数据类型?

例如:

<前>国际富;如果(foo 是整数){//做点什么}

或类似:

<前>if (typeof(foo) == typeof(int)){//做点什么}

提前致谢.

解决方案

这称为类型自省或反射,C 语言不支持.您可能需要编写自己的反射库,这将是一项巨大的工作.

Has a way to get the datatype in C?

For example:

int foo;

if (foo is int)
{
    // do something
}

or something like:

if (typeof(foo) == typeof(int))
{
    // do something
}

Thanks in advance.

解决方案

This is called type introspection or reflection and is not supported by the C language. You would probably have to write your own reflection library, and it would be a significant effort.

这篇关于我可以从 C 中的变量中检索数据类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆