简体   繁体   English

c中的sizeof运算符的大小是多少?

[英]What is size for the sizeof operator in c?

In recent I was attend one Interview on "SureSoft Technology"...... In that interview, they ask one question lik "What is size for the sizeof operator in c? " 最近,我参加了一次有关“ SureSoft Technology”的采访……在那次采访中,他们问了一个问题,例如“ c中的sizeof运算符的大小是多少?”

If any one Know answer Share with me? 如果有人知道答案与我分享?

The sizeof operator itself has no size. sizeof运算符本身没有大小。 Its result will generally* turn into a constant value at compile time. 其结果通常会在编译时*变为恒定值。

As for the value it returns, that would be the size, in bytes, of the argument. 至于返回的值,则为参数的大小(以字节为单位)。 The type of the result is size_t (defined in <stdlib.h> ) (§6.5.3.4.4) 结果的类型为size_t (在<stdlib.h>定义)(第6.5.3.4.4节)

* - with the notable exception of dynamically-sized automatic arrays. *-动态大小自动数组的显着例外。

You'll find the answers here and here : the result of sizeof is of type size_t, and: 您将在此处此处找到答案:sizeof的结果为size_t类型,并且:

"The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors when moving from 32 to 64-bit architecture, for example. According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bits." “ size_t的实际类型取决于平台;常见的错误是假定size_t与unsigned int相同,例如,从32位架构转换为64位架构时,这可能导致编程错误。根据1999 ISO C标准(C99),size_t是至少16位的无符号整数类型。”

在stddef.h中定义的size_t类型。

The answer is simple - it's the size in bytes of the given data structure that you pass to sizeof. 答案很简单-传递给sizeof的给定数据结构的大小(以字节为单位)。

For example: 例如:

sizeof(char) // one bytes
sizeof(int) // four bytes

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM