gpt4 book ai didi

c - 在 C 中的运行时确定变量的类型

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:56 24 4
gpt4 key购买 nike

我有几个类型为 char(数组)、int 和 double 的变量。有没有办法在运行时识别它们是什么类型?

例如,我正在寻找类似的东西:

int dummyInt = 5;    
double dummyDouble = 5.0;
dummyInt == int ?
printf("yes, it's of int type\n") : printf("no, it's not of int type\n");
dummyDouble == int ?
printf("yes, it's of int type\n") : printf("no, it's not of int type\n");

明显的结果将是:

yes, it's of int type
no, it's not of int type

嗯,我需要它的原因是因为我要将数据从变量传输到 SQL 数据库(使用 SQLite)。现在,每次运行程序时, header 都可以更改,具体取决于正在使用的变量。所以当我创建表时,我需要告诉它它是 VARCHAR、INTEGER、DOUBLE 等。

最佳答案

不,你不能,但你为什么要这样做? C 是静态类型的,所以当您声明 int dummyInt = 5 时,您已经知道它是一个 int

编辑:

Now, the headers can change each time I run the program depending on which variables are being used. So when I'm creating the table, I need to tell it if it's VARCHAR, INTEGER, DOUBLE etc.

这仍然没有太大变化。这些类型在编译时仍然是已知的,因此您可能只需要一个 #define 以及定义 SQL 类型的实际变量,比如:

int someVar;
#define SOMEVAR_SQL_TYPE "INTEGER"

关于c - 在 C 中的运行时确定变量的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3908730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com