C语言数组求和的方法有以下几种:
int sum = 0;
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < length; i++) {
sum += arr[i];
}
printf("数组的和为:%d\n", sum);
int sum = 0;
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);
int *p = arr;
for (int i = 0; i < length; i++) {
sum += *(p + i);
}
printf("数组的和为:%d\n", sum);
int sum(int arr[], int length) {
if (length == 0) {
return 0;
} else {
return arr[length - 1] + sum(arr, length - 1);
}
}
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);
printf("数组的和为:%d\n", sum(arr, length));
以上是几种常见的求和方法,根据实际需求选择合适的方法。