You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从C语言函数返回两个整数?多函数多值返回方案咨询

C语言函数返回多个值的实现方案

你的代码存在两个核心问题:

  1. C语言函数无法直接返回多个值,return root1, root2;是逗号表达式,实际只会返回最后一个值,还存在返回值类型不匹配的问题(比如rootinquad返回int,但计算的根是double)。
  2. 调用函数时没有传递变量地址或接收返回值,导致main里的root1、root2是未初始化的垃圾值。

下面提供两种简洁的实现方法:


方法一:使用指针参数传递(最常用简洁)

通过指针参数,让函数直接修改外部变量的值,不需要额外定义结构体。

修改closedinterval函数

将函数改为void类型,接收两个int指针作为参数:

#include <math.h>
#include <stdio.h>

void closedinterval(int *r1, int *r2)
{
    printf("Closed interval: ");
    // 直接将输入写入指针指向的内存地址
    scanf("%d%d", r1, r2);
}

修改rootinquad函数

考虑二次方程可能有实根或复根,让函数返回一个状态值区分根的类型,同时通过指针传递根的值:

// 返回值:1=两个不同实根,0=一个实根,-1=复根
int rootinquad(double *r1, double *r2, double *real_part, double *imag_part)
{
    double a, b, c, discriminant;
    printf("Enter coefficients a, b and c: ");
    scanf("%lf %lf %lf", &a, &b, &c);
    discriminant = b * b - 4 * a * c;

    if (discriminant > 0) {
        *r1 = (-b + sqrt(discriminant)) / (2 * a);
        *r2 = (-b - sqrt(discriminant)) / (2 * a);
        return 1;
    }
    else if (discriminant == 0) {
        *r1 = *r2 = -b / (2 * a);
        return 0;
    }
    else {
        *real_part = -b / (2 * a);
        *imag_part = sqrt(-discriminant) / (2 * a);
        return -1;
    }
}

修改main函数

调用时传递变量地址,并根据返回状态处理结果:

int main()
{
    int root1, root2;
    char ka;
    printf("Is there a closed interval in the question?(y/n) ");
    scanf(" %c", &ka);

    switch(ka)
    {
        case 'y':
            // 传递root1和root2的地址
            closedinterval(&root1, &root2);
            printf("Closed interval: %d %d\n", root1, root2);
            break;
        case 'n':
            double quad_r1, quad_r2, real, imag;
            int type = rootinquad(&quad_r1, &quad_r2, &real, &imag);
            if (type == 1) {
                printf("Two distinct real roots: %.2lf %.2lf\n", quad_r1, quad_r2);
            } else if (type == 0) {
                printf("One real root: %.2lf\n", quad_r1);
            } else {
                printf("Complex roots: %.2lf+%.2lfi %.2lf-%.2lfi\n", real, imag, real, imag);
            }
            break;
        default:
            printf("Please answer with y/n\n");
            break;
    }
    return 0;
}

方法二:使用结构体封装返回值

如果需要把多个相关值作为一个整体返回,可以用结构体打包数据,代码可读性更好。

定义结构体

#include <math.h>
#include <stdio.h>

// 封装闭区间的整数对
typedef struct {
    int start;
    int end;
} Interval;

// 封装二次方程的根信息
typedef struct {
    int type; // 1=两实根,0=重根,-1=复根
    double root1;
    double root2;
    double real;
    double imag;
} QuadRoots;

修改closedinterval函数

返回Interval结构体:

Interval closedinterval()
{
    Interval interval;
    printf("Closed interval: ");
    scanf("%d%d", &interval.start, &interval.end);
    return interval;
}

修改rootinquad函数

返回QuadRoots结构体:

QuadRoots rootinquad()
{
    QuadRoots roots;
    double a, b, c, discriminant;
    printf("Enter coefficients a, b and c: ");
    scanf("%lf %lf %lf", &a, &b, &c);
    discriminant = b * b - 4 * a * c;

    if (discriminant > 0) {
        roots.type = 1;
        roots.root1 = (-b + sqrt(discriminant)) / (2 * a);
        roots.root2 = (-b - sqrt(discriminant)) / (2 * a);
    }
    else if (discriminant == 0) {
        roots.type = 0;
        roots.root1 = roots.root2 = -b / (2 * a);
    }
    else {
        roots.type = -1;
        roots.real = -b / (2 * a);
        roots.imag = sqrt(-discriminant) / (2 * a);
    }
    return roots;
}

修改main函数

接收结构体返回值并处理:

int main()
{
    int root1, root2;
    char ka;
    printf("Is there a closed interval in the question?(y/n) ");
    scanf(" %c", &ka);

    switch(ka)
    {
        case 'y':
            Interval interval = closedinterval();
            root1 = interval.start;
            root2 = interval.end;
            printf("Closed interval: %d %d\n", root1, root2);
            break;
        case 'n':
            QuadRoots quad_roots = rootinquad();
            if (quad_roots.type == 1) {
                printf("Two distinct real roots: %.2lf %.2lf\n", quad_roots.root1, quad_roots.root2);
            } else if (quad_roots.type == 0) {
                printf("One real root: %.2lf\n", quad_roots.root1);
            } else {
                printf("Complex roots: %.2lf+%.2lfi %.2lf-%.2lfi\n", quad_roots.real, quad_roots.imag, quad_roots.real, quad_roots.imag);
            }
            break;
        default:
            printf("Please answer with y/n\n");
            break;
    }
    return 0;
}

内容的提问来源于stack exchange,提问作者Onur Alp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 08:35:05