1 .typeof 参数是变量或表达式
#define min(x, y) ({ \
typeof(x) _min1 = (x); \
typeof(y) _min2 = (y); \
(void) (&_min1 == &_min2); \
_min1 < _min2 ? _min1 : _min2; })
当x的类型为是 int 时 _min1变量的数据类型则为 int。
当x为一个表达式时(例: x = 3-4), _min1变量的数据类型为这个表达式结果的数据类型。
2 .typeof 参数是函数
int function(int, int);
typeof(function(1. 2)) val;
此时val的数据类型为 函数function(int, int)返回值的数据类型 ,即int类型。(注意: typeof并不会执行函数function)。
typeof关键字有点类似与c++中的decltype关键字。
本文深入探讨了C语言中typeof关键字的使用方法及其特性。详细解释了当typeof参数为变量或表达式时,如何确定其数据类型;以及当参数为函数时,如何获取函数返回值的类型。通过具体示例,帮助读者理解typeof与C++中decltype关键字的相似之处。

5317

被折叠的 条评论
为什么被折叠?



