初始化数组是否保留其在 C/C++ 和类似语言中的顺序?

Does an Initialized Array retain its order in C/C++ and similar languages?

假设我在 C/C++ 中有一个数组:

int myArray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

在Java中相同:

int[] myArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

以及其他编程语言中的等价物。

是否保证元素始终与我在初始化时键入的顺序相同?例如:1, 2, 3, 4, 5, 6, 7, 8, 9, 10.


注意: 我已经问过 具体到 Java 编程语言,我知道这是真的。但我想知道这种行为是否是任何(或大多数)编程语言的规则,而且我不确定编辑是否是提出这个问题的最佳方式。

让我们听听 C 的创造者对此有何看法:

An array may be initialized by following its declaration with a list of initializers enclosed in braces and separated by commas. For example, to initialize an array days with the number of days in each month:
int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
When the size of the array is omitted, the compiler will compute the length by counting the initializers, of which there are 12 in this case. If there are fewer initializers for an array than the specified size, the others will be zero for external, static and automatic variables. It is an error to have too many initializers.
There is no way to specify repetition of an initializer, nor to initialize an element in the middle of an array without supplying all the preceding values as well.
Character arrays are a special case of initialization; a string may be used instead of the braces and commas notation:
char pattern = "ould"; is a shorthand for the longer but equivalent
char pattern[] = { 'o', 'u', 'l', 'd', '[=12=]' }; In this case, the array size is five (four characters plus the terminating '[=13=]').

C BRIAN W KERNIGHAN & DENNIS M RITCHIE,第二版,第 95 页。

It will be same in both the case

int[] myArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

这就够了

int[0] = 1
int[1] = 2
.
.
.
int[9] = 10

它不会因语言而异 int[] arrayjava 中是理想的,只是为了程序方便 int array[] 是允许的。