非常教程

C参考手册

C 语法

sizeof operator

查询对象或类型的大小。

在必须知道对象的实际大小时使用。

句法

sizeof( type )

(1)

sizeof expression

(2)

两个版本都返回一个类型值size_t

说明

1)返回类型的对象表示的大小(以字节为单位)

2)返回表达式类型的对象表示的大小(以字节为单位)

笔记

根据计算机体系结构的不同,一个字节可能由8个或更多位组成,确切的数字为CHAR_BIT

sizeof(char), sizeof(signed char), and sizeof(unsigned char) always return 1.

sizeof不能用于函数类型,不完整类型(包括void)或位域左值。

当应用于具有结构或联合类型的操作数时,结果是此类对象中的总字节数,包括内部和尾部填充。尾部填充是这样的:如果对象是数组的元素,则该数组的下一个元素的对齐要求将被满足,换句话说,sizeof(T)返回T []数组的元素的大小。

除非表达式的类型是VLA,否则(因为C99)表达式不计算,sizeof操作符可能用于整型常量表达式。

如果表达式的类型是可变长度数组类型,则计算expression并在运行时计算其计算的数组大小。

(自C99以来)

任何阵列中的元素数量,a包括VLA(自C99开始)可以用表达式确定sizeof a / sizeof a[0]。请注意,如果a具有指针类型(例如在函数参数类型调整的数组到指针转换之后),则此表达式将简单地将指针类型中的字节数除以指向类型中的字节数。

关键词

sizeof.

采样输出对应于具有64位指针和32位int的平台。

#include <stdio.h>
 
int main(void)
{
    // type argument:
    printf("sizeof(float)         = %zu\n", sizeof(float));
    printf("sizeof(void(*)(void)) = %zu\n", sizeof(void(*)(void)));
    printf("sizeof(char[10])      = %zu\n", sizeof(char[10]));
//  printf("sizeof(void(void)) = %zu\n", sizeof(void(void))); // Error: function type
//  printf("sizeof(char[]) = %zu\n", sizeof(char[])); // Error: incomplete type
 
    // expression argument:
    printf("sizeof 'a'   = %zu\n", sizeof 'a'); // type of 'a' is int
//  printf("sizeof main = %zu\n", sizeof main); // Error: Function type
    printf("sizeof &main = %zu\n", sizeof &main);
    printf("sizeof \"hello\" = %zu\n", sizeof "hello"); // type is char[6]
}

可能的输出:

sizeof(float)         = 4
sizeof(void(*)(void)) = 8
sizeof(char[10])      = 10
sizeof 'a'   = 4
sizeof &main = 8
sizeof "hello" = 6

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Escape sequences
41.Expressions
42.External and tentative definitions
43.File scope
44.floating constant
45.for loop
46.Function declarations
47.Function definitions
48.Functions
49.Generic selection
50.goto statement
51.Identifier
52.if statement
53.Implicit conversions
54.Increment/decrement operators
55.Initialization
56.inline function specifier
57.integer constant
58.Lifetime
59.Logical operators
60.Lookup and name spaces
61.Main function
62.Member access operators
63.Memory model
64.Objects and alignment
65.Order of evaluation
66.Other operators
67.Phases of translation
68.Pointer declaration
69.Preprocessor
70.restrict type qualifier
71.return statement
72.Scalar initialization
73.Scope
74.Statements
75.static assert declaration
76.Static storage duration
77.Storage-class specifiers
78.string literals
79.Struct and union initialization
80.Struct declaration
81.switch statement
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。