C 中的 size_t 是什么? [英] What is size_t in C?
问题描述
我对 C 中的 size_t
感到困惑.我知道它是由 sizeof
运算符返回的.但究竟是什么呢?是数据类型吗?
I am getting confused with size_t
in C. I know that it is returned by the sizeof
operator. But what exactly is it? Is it a data type?
假设我有一个 for
循环:
Let's say I have a for
loop:
for(i = 0; i < some_size; i++)
我应该使用 int i;
还是 size_t i;
?
Should I use int i;
or size_t i;
?
推荐答案
根据 1999 ISO C 标准(C99), size_t
是一个无符号整数至少 16 位的类型(参见章节7.17 和 7.18.3).
According to the 1999 ISO C standard (C99),
size_t
is an unsigned integer type of at least 16 bit (see sections 7.17 and 7.18.3).
size_t
是无符号数据类型由几个 C/C++ 标准定义,例如C99 ISO/IEC 9899 标准,stddef.h
中定义的.1 它可以通过包含进一步进口stdlib.h
作为这个文件内部子包括 stddef.h
.
size_t
is an unsigned data type
defined by several C/C++ standards,
e.g. the C99 ISO/IEC 9899 standard,
that is defined in stddef.h
.1 It can
be further imported by inclusion of
stdlib.h
as this file internally sub
includes stddef.h
.
该类型用于表示物体的大小.库函数接受或返回尺寸期望它们属于类型或具有返回类型size_t
.此外,最常用的基于编译器的运算符 sizeof 应评估为兼容的常数值size_t
.
This type is used to represent the
size of an object. Library functions
that take or return sizes expect them
to be of type or have the return type
of size_t
. Further, the most
frequently used compiler-based
operator sizeof should evaluate to a
constant value that is compatible with
size_t
.
作为暗示,size_t
是一种保证保存任何数组索引的类型.
As an implication, size_t
is a type guaranteed to hold any array index.
这篇关于C 中的 size_t 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!