尼莫点Nemo 2023-09-10 11:41 采纳率: 50%
浏览 28
已结题

C语言Clion函数报错

#C语言

请教大家,这样的应该怎么解决,刚刚开始学用Clion,这段代码明明在CB上运行是没有问题的。

报错为:
Call to undeclared function 'yearkind'; ISO C99 and later do not support implicit function declarations
Parameter 'year' was not declared, defaults to 'int'; ISO C99 and later do not support implicit int

以下为完整代码:

#include<stdio.h>


int main()
{
    int ret, year, month, flag;

    ret = scanf("%d %d", &year, &month);
    if (ret != 2)
    {
        printf("ERROR!");
        fflush(stdin);
    }
    else
    {
        flag = yearkind(year);

        if (flag == 1)           //闰年
        {
            switch(month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    printf("这个月有31天。");
                    break;
                case 2:
                    printf("这个月有29天。");
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    printf("这个月有30天。");
                    break;

            }
        }
        else
        {
            switch(month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    printf("这个月有31天。");
                    break;
                case 2:
                    printf("这个月有28天。");
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    printf("这个月有30天。");
                    break;
            }
        }
    }

    return 0;
}

int yearkind(year)
{
    return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 1 : 0;
}


我还想问一嘴,似乎CB的编码跟Clion不一样,怎么解决?

  • 写回答

4条回答 默认 最新

  • 呈两面包夹芝士 2023-09-10 11:44
    关注

    你的代码在Clion中报错是因为你使用的编译器遵循了C99标准,而C99标准不支持隐式函数声明和隐式int。在你的代码中,函数yearkind并没有在调用前声明,同时year参数也没有明确声明类型,因此编译器给出了错误信息。

    解决这个问题的方法是在调用yearkind函数前进行函数声明,并明确声明year参数的类型。可以将代码修改为如下形式:

    #include<stdio.h>
    
    // 函数声明
    int yearkind(int year);
    
    int main()
    {
        int ret, year, month, flag;
     
        ret = scanf("%d %d", &year, &month);
        if (ret != 2)
        {
            printf("ERROR!");
            fflush(stdin);
        }
        else
        {
            flag = yearkind(year);
     
            if (flag == 1)           //闰年
            {
                switch(month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        printf("这个月有31天。");
                        break;
                    case 2:
                        printf("这个月有29天。");
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        printf("这个月有30天。");
                        break;
                }
            }
            else
            {
                switch(month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        printf("这个月有31天。");
                        break;
                    case 2:
                        printf("这个月有28天。");
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        printf("这个月有30天。");
                        break;
                }
            }
        }
     
        return 0;
    }
     
    // 函数定义,此处已将year参数的类型明确为int
    int yearkind(int year)
    {
        return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 1 : 0;
    }
    

    这样修改后,代码应该可以在Clion中正常运行。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 9月18日
  • 已采纳回答 9月10日
  • 创建了问题 9月10日

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址