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

C语言程序无报错但无运行输出问题的排查与解决

问题分析与解决

你的程序编译通过但无输出,核心原因是main函数里没有正确调用tuitonFee函数——你写的那行代码是函数声明,不是实际调用。

错误点

在main函数末尾,你写了:

double tuitonFee(student_id, credit, year, tuiton_0, tuiton_1);

这行代码的语法是声明一个返回double类型的tuitonFee函数,参数类型由变量推导,并没有执行函数内部的逻辑,所以函数里的printf语句根本没运行,自然没有输出。

修正后的代码

把函数声明改成函数调用即可:

#include <stdio.h>
#include <stdlib.h>

double tuitonFee(int student_id, int credit, int year, double tuiton_0, double tuiton_1);

int main()
{
    int student_id, credit, year;

    printf("Enter the student id (-1 to exit): ");
    scanf_s("%d", &student_id);

    if (student_id == -1)
    {
        exit(0);
    }

    printf("Enter the credit: ");
    scanf_s("%d", &credit);

    printf("Enter year: ");
    scanf_s("%d", &year);

    double tuiton_0 = 525 * credit;
    double tuiton_1 = 4500 + (750 * credit);

    // 去掉前面的double,改为实际函数调用
    tuitonFee(student_id, credit, year, tuiton_0, tuiton_1);

    return 0;
}

double tuitonFee(int student_id, int credit, int year, double tuiton_0, double tuiton_1)
{
    if (credit <= 12)
    {
        if (year == 4)
        {
            printf("Student with id %d will pay tuiton %.0lf$\n", student_id, tuiton_0);
        }
        else
        {
            printf("Student with id %d will pay tuiton %.0lf$\n", student_id, tuiton_0 * 0.15);
        }
    }
    else
    {
        if (year == 4)
        {
            printf("Student with id %d will pay tuiton %.0lf$\n", student_id, tuiton_1);
        }
        else
        {
            printf("Student with id %d will pay tuiton %.0lf$\n", student_id, tuiton_1 * 0.15);
        }
    }
    // 修正返回值类型不匹配问题,返回计算后的学费金额
    return (credit <=12) ? (year==4 ? tuiton_0 : tuiton_0*0.15) : (year==4 ? tuiton_1 : tuiton_1*0.15);
}

额外优化建议

  1. 返回值规范:原函数声明返回double,但实际返回int类型的credit,类型不匹配,建议返回计算后的学费金额,让函数逻辑更合理。
  2. 输出格式:在printf末尾加\n,避免终端提示符和输出内容挤在一起,输出更整洁。
  3. 条件简化:else if (credit > 12)可以直接改成else,因为credit要么<=12要么>12,逻辑更简洁。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 14:40:39