在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;
}