温馨提示×

c语言typeof的用法是什么

小亿
161
2023-12-08 01:59:34
栏目: 编程语言

在C语言中,typeof是一个类型宏,用于获取一个表达式的数据类型。它的语法形式如下:

typeof(expression)

其中,expression是任意C语言表达式。typeof会返回expression的数据类型。

以下是typeof的用法示例:

  1. 获取变量的数据类型:
int num = 10;
typeof(num) numType; // numType的类型为int
  1. 获取数组元素的数据类型:
int arr[5];
typeof(arr[0]) arrType; // arrType的类型为int
  1. 获取指针类型:
int *ptr;
typeof(ptr) ptrType; // ptrType的类型为int *
  1. 获取函数的返回类型:
int add(int a, int b) {
    return a + b;
}
typeof(add) addType; // addType的类型为int

需要注意的是,typeof是一个编译时的操作符,而不是运行时的函数。因此,它只能用于已知的类型,而不能用于运行时才能确定的类型。

0