简体   繁体   English

C语言中函数的声明和使用

[英]Declaration and use of function in c

I can't find errors in this code. 我在此代码中找不到错误。 I'm trying to write a program that returns the number of days (ex: 60) in the year based on day, month and year [ex: 1/3/2000 (3 Mar 2000)]. 我正在尝试编写一个程序,该程序根据日,月和年返回一年中的天数(例如:60)[例如:2000年3月3日(2000年3月3日)]。

The compiler give me these errors: 编译器给我这些错误:

  • too few arguments to function 'day_of_year' 函数'day_of_year'的参数太少
  • conflicting types for 'day_of_year' 'day_of_year'的类型冲突
#include <stdio.h>

int day_of_year(int day, int month, int year);

int main(){
    int day, month, year, i, count=0;
    int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
    printf ("Enter the day: ");
    scanf ("%d", &day);
    printf ("Enter the month: ");
    scanf ("%d", &month);
    printf ("Enter the year: ");
    scanf ("%d", &year);
    count=day_of_year();
    printf ("Count: %d", count);
    return 0;
}

int day_of_year (int day, int month, int year, int i, int count){
    int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
    if (year%4==0) a[2]++;
    count = day;
    for (i=0;i<month;i++)
        count+=a[i];
    return count;
}

Declaration: 宣言:

int day_of_year(int day, int month, int year);

Call: 呼叫:

count=day_of_year();

Definition: 定义:

int day_of_year (int day, int month, int year, int i, int count){

C is a strongly typed language meaning the number and the type of functions arguments much match. C是一种强类型语言,表示函数参数的数量和类型非常匹配。

So, you need correct the argument list and add the missing parameters to the function call. 因此,您需要更正参数列表并将缺少的参数添加到函数调用中。

It looks like you don't really need the last two arguments in the definition - declare them as local variables instead: 看起来您实际上并不需要定义中的最后两个参数,而是将它们声明为局部变量:

int day_of_year (int day, int month, int year){
    int i, count;

I put some comments. 我发表了一些意见。

#include <stdio.h>

int day_of_year(int day, int month, int year);

int main(){
    // you don't need to declare 'i' here, or a[]
    int day, month, year,count;

    printf ("\nEnter the day: ");
    scanf ("%d", &day);
    printf ("\nEnter the month: ");
    scanf ("%d", &month);
    printf ("\nEnter the year: ");
    scanf ("%d", &year);

    // you need to pass the parameters to the function
    count=day_of_year(day,month,year);

    printf ("\nCount: %d", count);
    return 0;
}
// here you put in the function signature two more variables.
//they are not used and also they differ from the initial definition.
int day_of_year (int day, int month, int year){
    int count=0,i=0;
    int a[]={31,28,31,30,31,30,31,31,30,31,30,31};

    if (year%4==0) a[1]++;
    count = day;

    for (i=0;i<month;i++)
        count+=a[i];

    return count;
}

Thanks to all. 谢谢大家。 I corrected the errors. 我更正了错误。 This is the new code. 这是新代码。

#include <stdio.h>

int day_of_year(int day, int month, int year);

int main(){
    int day, month, year, i, count=0;
    int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
    printf ("Enter the day: ");
    scanf ("%d", &day);
    printf ("Enter the month: ");
    scanf ("%d", &month);
    printf ("Enter the year: ");
    scanf ("%d", &year);
    count=day_of_year(day, month, year);
    printf ("Count: %d", count);
    return 0;
}

int day_of_year (int day, int month, int year){
    int i, count;
    int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
    if (year%4==0) a[2]++;
    count = day;
    for (i=0;i<month;i++)
        count+=a[i];
    return count;
}

您尚未将任何参数传递给函数day_of_year。

There were quite a few errors in your original code, but a solution that compiles can be like this: 您的原始代码中有很多错误,但是编译的解决方案可以是这样的:

#include <stdio.h>

int day_of_year(int day, int month, int year);

int main(){
    int day, month, year, count=0;
    printf ("Enter the day: ");
    scanf ("%d", &day);
    printf ("Enter the month: ");
    scanf ("%d", &month);
    printf ("Enter the year: ");
    scanf ("%d", &year);
    count=day_of_year(day, month, year);
    printf ("Count: %d", count);
    return 0;
}

int day_of_year (int day, int month, int year){
    int i = 0, count = 0; // declaration was missing
    int a[]={31,28,31,30,31,30,31,31,30,31,30,31};

    if (year%4==0) a[2]++;

    count = day;
    for (i=0;i<month;i++)
        count+=a[i];

    return count;
}

First of all you declare your day_of_year function at the beginning of your code, but when you implement it you use additional arguments which is forbidden. 首先,您在代码的开头声明了day_of_year函数,但是在实现它时,您将使用其他禁止使用的参数。

You should declare count and i inside the body of the function so I moved them from the function's argument list. 您应该在函数体内声明counti ,以便将它们从函数的参数列表中移出。

Second, when you call day_of_year in main you pass nothing, although you have them read from console. 其次,当您main调用day_of_year时,尽管您从控制台读取了它们,但是您什么也没传递。

Other than that the logic to determine whether the input year is a leap year is not an ideal solution but I leave that up to you to correct. 除此之外,确定输入年份是否为a年的逻辑不是理想的解决方案,但我让您自己纠正。

Hope it helps. 希望能帮助到你。

Apart from mismatching prototype % definition, you are also not passing any arguments to the function. 除了原型%定义不匹配之外,您也没有将任何参数传递给该函数。

You have few off-by-one errors. 您几乎没有一对一的错误。 Index in C starts from 0. C中的索引从0开始。

Here's a modified version. 这是修改后的版本。 Pay close attention to the changes in day_of_year function. 请密切注意day_of_year函数的更改。 Particularly, for February, you want to increment a[1] (not a[2] ). 特别是,对于2月,您想增加a[1] (而不是a[2] )。 Similarly, the for loop's condition must be month - 1 (not month ). 同样, for循环的条件必须为month - 1 (而不是month )。

#include <stdio.h>

int day_of_year(int day, int month, int year);

int main(){
    int day, month, year, i, count=0;
    int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
    printf ("Enter the day: ");
    scanf ("%d", &day);
    printf ("Enter the month: ");
    scanf ("%d", &month);
    printf ("Enter the year: ");
    scanf ("%d", &year);
    count=day_of_year(day, month, year);
    printf ("Count: %d", count);
    return 0;
}

int day_of_year (int day, int month, int year)
{

    int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
    int count = day, i;

    if (year%4==0) a[1]++;

    for (i=0;i<month - 1;i++)
        count+=a[i];

    return count;
}

compiler gives error because of below inline reason: 编译器由于以下内联原因而给出错误:

° too few arguments to function 'day_of_year' °函数'day_of_year'的参数太少

Reason: called day_of_year() function without parameter. 原因:调用了不带参数的day_of_year()函数。

° conflicting types for 'day_of_year' °与“ day_of_year”的类型冲突

Reason: prototype of day_of_year() function is different from day_of_year() definition. 原因: day_of_year()函数的原型与day_of_year()定义不同。 Prototype says to compiler that it take 3 parameter but function definition having 5 parameter. 原型对编译器说,它带有3个参数而函数定义具有5个参数。 this mismatch causing the error. 这种不匹配会导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM