当前位置: C语言 -- 标准库 -- <stdbit.h> -- __STDC_ENDIAN_BIG__

__STDC_ENDIAN_BIG__宏


概要:
#include <stdbit.h>
#define __STDC_ENDIAN_BIG__ value //value值由实现定义。

描述:

多字节标量类型中常见的两种字节排序方法是小端序(little-endian)和大端序(big-endian)。

小端序中最低有效字节(the least significant byte)放在最前面,其余字节按升序排列;即最低有效字节存储在最小的内存地址中,以0x12345678为例:

小端序
有效字节(由高到低) 12 34 56 78
内存地址(由小到大) 78 56 34 12

大端序中最高有效字节(the most significant byte)放在最前面,其余字节按降序排列;即最高有效字节存储在最小的内存地址中,以0x12345678为例:

大端序
有效字节(由高到低) 12 34 56 78
内存地址(由小到大) 12 34 56 78

实现中还可能存在其它字节排序方式。


宏__STDC_ENDIAN_BIG__是整数常量表达式,表示大端序,其值由实现定义,但不等于宏__STDC_ENDIAN_LITTLE__。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
/*宏__STDC_ENDIAN_BIG__范例*/

#include <stdbit.h>
#include <stdio.h>

int main(void)
{
  switch(__STDC_ENDIAN_NATIVE__)
  {
    case __STDC_ENDIAN_LITTLE__:
      puts("The execution environment is little-endian.");
      break;

    case __STDC_ENDIAN_BIG__:
      puts("The execution environment is big-endian.");
      break;

    default:
      puts("The byte order storage for the execution environment is unknown.");
      break;
  }

  return 0;
}

结果:

(注:编译时实现未支持<stdbit.h>头文件。)


相关内容:
__STDC_ENDIAN_LITTLE__ 表示小端序的宏。
__STDC_ENDIAN_NATIVE__ 表示执行环境字节序存储方法的宏。


主要参考资料:

1、ISO/IEC 9899:2024