在C語言中,函數只能返回一個值。但可以使用指針或結構體的方式來實現返回多個值。
void multipleValues(int a, int b, int *sum, int *diff) {
*sum = a + b;
*diff = a - b;
}
int main() {
int a = 5, b = 3, sum, diff;
multipleValues(a, b, &sum, &diff);
printf("Sum: %d\n", sum);
printf("Difference: %d\n", diff);
return 0;
}
typedef struct {
int sum;
int diff;
} Result;
Result multipleValues(int a, int b) {
Result result;
result.sum = a + b;
result.diff = a - b;
return result;
}
int main() {
int a = 5, b = 3;
Result result = multipleValues(a, b);
printf("Sum: %d\n", result.sum);
printf("Difference: %d\n", result.diff);
return 0;
}