C语言-Initialization of one-dimensional array

数组初始化:

指定数组长度时,

可以只给数组中部分元素赋初始值,其余未赋值元素自动为0

int a[10]={1,4,6,8};

 

不指明数组长度时,

而由初始值元素个数决定数组的长度

int a[]={1,4,8};   //数组的长度就是3

 

#include
#include

/*Compute the sum of all elements from a array*/
int main(void) {
	int a[10] = { 1,3,5,7,9,2,4,6,8 };
	int sum = 0;
	for (int i = 0; i < 10; i++) {
		sum = sum + a[i];
	}
	printf("%d\n", sum);
	system("pause");
	return 0;
}

C语言-Initialization of one-dimensional array_第1张图片

你可能感兴趣的:(C,Programming)