简体   繁体   English

function [-Wimplicit-function-declaration] 的隐式声明

[英]implicit declaration of function [-Wimplicit-function-declaration]

I am new to programming in C and I am making a C program to find the average and percentage of marks obtained by a student in three subjects using a single function.我是 C 编程的新手,我正在制作一个 C 程序,以查找学生使用单个 ZC1C425268E68385D1AB5074C17A94F14 在三个科目中获得的分数的平均值和百分比。 My code is:我的代码是:

#include <stdio.h>
int main()
{
    float aver, per, mark1, mark2, mark3;
    printf("Enter the marks of subject 1: ");
    scanf(" %f", &mark1);
    printf("Enter the marks of subject 2: ");
    scanf(" %f", &mark2);
    printf("Enter the marks of subject 3: ");
    scanf(" %f", &mark3);
    averper(mark1, mark2, mark3, &aver, &per);
    printf("The average of marks entered by you = %f\n", aver);
    printf("The percentage of marks entered by you = %f", per);
    return 0;
}
float averper(float a, float b, float c, float *d, float *e)
{
    float sum = a + b + c;
    *d = sum / 3;
    *e = (sum / 300) * 100;
}

The errors obtained are:得到的错误是:

main.c: In function ‘main’:
main.c:11:2: warning: implicit declaration of function ‘averper’ [-Wimplicit-function-declaration]
  averper(mark1, mark2, mark3, &aver, &per);
  ^~~~~~~
main.c: At top level:
main.c:16:7: error: conflicting types for ‘averper’
 float averper(float a, float b, float c, float *d, float *e)
       ^~~~~~~
main.c:11:2: note: previous implicit declaration of ‘averper’ was here
  averper(mark1, mark2, mark3, &aver, &per);
  ^~~~~~~ 

Thanks谢谢

The defects in the program:程序中的缺陷:

  1. Used the function before defining the function signature or directly function at the top of the code.在定义 function 签名之前使用 function 或在代码顶部直接使用 function。

  2. The function doesn't returns anything and the if the program is designed to return, yet the value is nowhere used. function 不返回任何内容,如果程序设计为返回,但该值无处使用。


Try this approach:试试这个方法:

#include <stdio.h>

typedef struct { // declaring a struct to return avg and per together
    float avg;
    float per;
} averS;

averS averper(float, float, float, float *, float *); // function signature

int main()
{
    float aver, per, mark1, mark2, mark3;

    printf("Enter the marks of subject 1: ");
    scanf(" %f", &mark1);

    printf("Enter the marks of subject 2: ");
    scanf(" %f", &mark2);

    printf("Enter the marks of subject 3: ");
    scanf(" %f", &mark3);

    averS s = averper(mark1, mark2, mark3, &aver, &per); // holding values

    printf("The average of marks entered by you = %f\n", s.avg);
    printf("The percentage of marks entered by you = %f", s.per);

    return 0;
}

averS averper(float a, float b, float c, float *d, float *e)
{
    averS as; // declaring a local structure for returning value purpose.

    float sum = a + b + c;

    as.avg = sum / 3;
    as.per = (sum / 300) * 100;

    return as; // returning the struct
}

Here we've used a struct which holds two values and return them together and then they're used in the main() .在这里,我们使用了一个struct ,它保存两个值并将它们一起返回,然后在main()中使用它们。


After the successful compilation, it'll output something like:编译成功后会出现 output 类似:

Enter the marks of subject 1: 10 // --- INPUT
Enter the marks of subject 2: 20
Enter the marks of subject 3: 30
The average of marks entered by you = 20.000000 // --- OUTPUT
The percentage of marks entered by you = 20.000000

Is this valid for only C99, or for later versions too?这是否仅适用于 C99 或更高版本?

暂无
暂无

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

相关问题 GCC |警告:隐式声明函数&#39;_stricmp&#39;[-Wimplicit-function-declaration] | - GCC |warning: implicit declaration of function '_stricmp' [-Wimplicit-function-declaration]| 函数&#39;scan_s&#39;的隐式声明[-Wimplicit-function-declaration] - Implicit declaration of function 'scan_s' [-Wimplicit-function-declaration] 警告:function 'colcheck' [-Wimplicit-function-declaration] 的隐式声明 - warning: implicit declaration of function ‘colcheck’ [-Wimplicit-function-declaration] function '时间' [-Wimplicit-function-declaration]| 的隐式声明 - implicit declaration of function 'time' [-Wimplicit-function-declaration]| 警告:隐式声明 function 'gettimeofday' [-Wimplicit-function-declaration] - warning: implicit declaration of function ‘gettimeofday’ [-Wimplicit-function-declaration] 警告:函数“gets”的隐式声明; 您指的是 &#39;fgets&#39; 吗? [-Wimplicit-function-declaration] - warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration] 警告:function 'strcpy' [-Wimplicit-function-declaration] 的隐式声明程序工作,但我如何修复编译器错误 - warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration] the program work but how i fix the compiler error 我收到此错误“solution.c:9:4: 警告:函数‘stricmp’的隐式声明 [-Wimplicit-function-declaration] x= stricmp(ap,c);” - i get this error "solution.c:9:4: warning: implicit declaration of function 'stricmp' [-Wimplicit-function-declaration] x= stricmp(ap,c);" 错误:function 'rl_replace_line' 的隐式声明在 C99 中无效 [-Werror,-Wimplicit-function-declaration] - error: implicit declaration of function 'rl_replace_line' is invalid in C99 [-Werror,-Wimplicit-function-declaration] 程序警告中的 gettinan 错误:隐式声明 function 'Binsearch' [-Wimplicit-function-declaration] res= Binsearch(arr,n-1,0,x); - gettinan error in the program warning: implicit declaration of function 'Binsearch' [-Wimplicit-function-declaration] res= Binsearch(arr,n-1,0,x);
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM