C语言函数定义及应用:char 函数示例

以下是一个简单的例子,定义了一个名为'getGrade'的函数,该函数没有参数,返回一个char类型的值:

#include <stdio.h>

char getGrade(void) {
    int score;
    printf("Enter your score: ");
    scanf("%d", &score);

    if (score >= 90) {
        return 'A';
    } else if (score >= 80) {
        return 'B';
    } else if (score >= 70) {
        return 'C';
    } else if (score >= 60) {
        return 'D';
    } else {
        return 'F';
    }
}

int main() {
    char grade = getGrade();
    printf("Your grade is: %c\n", grade);

    return 0;
}

在上面的例子中,定义了一个函数getGrade,没有参数(void表示没有参数),返回一个char类型的值。该函数根据用户输入的分数,判断其所属的等级(A、B、C、D、F),然后将等级作为返回值。

在主函数main中,调用getGrade函数,将返回值赋给变量grade,并打印出最终的等级。


原文地址: https://www.cveoy.top/t/topic/QSl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录