【问题标题】:I'm getting Conflict types error in a C program我在 C 程序中遇到冲突类型错误
【发布时间】:2020-07-25 21:43:18
【问题描述】:

我正在返回 C 语言编程,但在尝试编译程序时出现此错误:

错误:“getMedia”的类型冲突

我没有找到下面的错误,首先我认为这可能是一个类型错误,但事实并非如此,我试图将上面的函数声明为全局“变量”,但它不起作用。

有人可以帮我吗?


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

int main()
{

    float nota1,nota2,nota3,nota4;
    scanf("%f %f %f %f",&nota1,&nota2,&nota3,&nota4);
    float media = getMedia(nota1,nota2,nota3,nota4);
    if(media < 5.0 && media >= 0){
        printf("Aluno reprovado\n");
    }else if(media > 7 && media <= 10){
        printf("Aluno aprovado\n");
    }else{
        checkExam();
    }
}

float getMedia(float nota1,float nota2, float nota3, float nota4){
    return (nota1*2+nota2*3+nota3*4+nota4)/10;
}

【问题讨论】:

标签: c linux function compiler-errors


【解决方案1】:

您必须在使用之前声明该函数。 PS 我已经删除了checkExam();,因为没有给出函数

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

float getMedia(float nota1,float nota2, float nota3, float nota4);

int main()
{

    float nota1,nota2,nota3,nota4;
    scanf("%f %f %f %f",&nota1,&nota2,&nota3,&nota4);
    float media = getMedia(nota1,nota2,nota3,nota4);
    if(media < 5.0 && media >= 0){
        printf("Aluno reprovado\n");
    } else if(media > 7 && media <= 10){
        printf("Aluno aprovado\n");
    }
}

float getMedia(float nota1,float nota2, float nota3, float nota4){
    return (nota1*2+nota2*3+nota3*4+nota4)/10;
}

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

static float getMedia(float nota1,float nota2, float nota3, float nota4){
    return (nota1*2+nota2*3+nota3*4+nota4)/10;
}

int main()
{

    float nota1,nota2,nota3,nota4;
    scanf("%f %f %f %f",&nota1,&nota2,&nota3,&nota4);
    float media = getMedia(nota1,nota2,nota3,nota4);
    if(media < 5.0 && media >= 0){
        printf("Aluno reprovado\n");
    }else if(media > 7 && media <= 10){
        printf("Aluno aprovado\n");
    }
}

【讨论】:

  • 感谢奥雷尔!它解决了我的问题......我不知道我必须在使用它之前声明一个函数。
猜你喜欢
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2015-05-24
  • 2014-03-10
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多