C:为什么不是size_t型为C的关键字? [英] C: Why isn't size_t a C keyword?

查看:119
本文介绍了C:为什么不是size_t型为C的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的sizeof 是一个C的关键字的。它返回一个名为为size_t 类型的大小。然而,为size_t 不会一个关键字,但在主要定义的 STDDEF.H 并可能其他C标准头文件了。

sizeof is a C keyword. It returns the size in a type named size_t. However, size_t is not a keyword, but is defined primarily in stddef.h and probably other C standard header files too.

考虑要建立一个C程序,的场景不会包含任何C标准头文件和库。 (例如像,如果要创建一个操作系统内核)。现在,在这样的code,的sizeof 可以使用(这是一个C的关键字,所以它是的一部分的语言的),但它返回类型(为size_t )不可用!

Consider a scenario where you want to create a C program which does not include any C standard headers or libraries. (Like for example, if you are creating an OS kernel.) Now, in such code, sizeof can be used (it is a C keyword, so it is a part of the language), but the type that it returns (size_t) is not available!

这是否意味着某种问题,在C标准规范?你能澄清这?

Does not this signify some kind of a problem in the C standard specification? Can you clarify this?

推荐答案

它不从字面上返回size_t类型的值,因为为size_t本身并不是一个具体类型,而是一个typedef到未指定内置类型。 typedef的标识符(如为size_t)是完全等同于它们各自的基础类型(在编译时转换此)。如果size_t会在平台上被定义为一个unsigned int,那么当你的系统上编译的sizeof返回一个unsigned int。为size_t仅仅是保持便携性的一种方便的方法,只需要你明确地使用它的名字被列入STDDEF.H。

It does not literally return a value of type size_t since size_t is not a concrete type in itself, but rather a typedef to an unspecified built-in type. Typedef identifiers (such as size_t) are completely equivalent to their respective underlying types (and are converted thereto at compile time). If size_t is defined as an unsigned int on your platform, then sizeof returns an unsigned int when it is compiled on your system. size_t is just a handy way of maintaining portability and only needs to be included in stddef.h if you are using it explicitly by name.

这篇关于C:为什么不是size_t型为C的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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