【问题标题】:Implicit Declaration of Function in C on CodeBlocks代码块上的 C 中函数的隐式声明
【发布时间】:2021-12-27 06:16:06
【问题描述】:

您好,我正在练习我的 C 语言知识,我正在尝试制作一个简单的计算器,但我遇到了这个警告 Implicit Declaration of Function,但我调用的函数已被执行。我试图用这个void start(); 修复它,但该函数没有执行。

成功执行了函数start();,但有一个隐含的警告:

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

void addition()
{
    int vala, valb, resu;
    system("cls");
    printf("SiMPLE CALCULATOR 1.0a\n");
    printf("ADDITION\n");
    printf("\n");
    printf("Enter the first value of addend: ");
    scanf("%d", &vala);
    printf("Enter the second value of addend: ");
    scanf("%d", &valb);
    resu=vala+valb;
    printf("The sum of %d and %d is: %d\n", vala, valb, resu);
    printf("PRESS [ANY KEY] TO CONTINUE...");
    getch();

    start(); \\THIS CODE
}

void start()
{
    char ope;
    system("cls");
    printf("SiMPLE CALCULATOR 1.0a\n");
    printf("What operation will be used:");
    scanf("%s", &ope);

    if (ope == 'a')
    {
        addition();
    }
    else if (ope == 'b')
    {
        printf("bbbbbbbbbbbb\n");
    }
    else
    {
        printf("ccccccccccc\n");
    }

}

int main()
{
    int choices;
    printf("SiMPLE CALCULATOR 1.0a\n");
    printf("choose an option:");
    scanf("%d", &choices);

    if (choices == 1)
    {
        start();
    }

    getch();
    return 0;
}

无法执行函数void start(); start 但没有隐式警告:

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

void addition()
{
    int vala, valb, resu;
    system("cls");
    printf("SiMPLE CALCULATOR 1.0a\n");
    printf("ADDITION\n");
    printf("\n");
    printf("Enter the first value of addend: ");
    scanf("%d", &vala);
    printf("Enter the second value of addend: ");
    scanf("%d", &valb);
    resu=vala+valb;
    printf("The sum of %d and %d is: %d\n", vala, valb, resu);
    printf("PRESS [ANY KEY] TO CONTINUE...");
    getch();

    void start(); \\THIS CODE
}

void start()
{
    char ope;
    system("cls");
    printf("SiMPLE CALCULATOR 1.0a\n");
    printf("What operation will be used:");
    scanf("%s", &ope);

    if (ope == 'a')
    {
        addition();
    }
    else if (ope == 'b')
    {
        printf("bbbbbbbbbbbb\n");
    }
    else
    {
        printf("ccccccccccc\n");
    }

}

int main()
{
    int choices;
    printf("SiMPLE CALCULATOR 1.0a\n");
    printf("choose an option:");
    scanf("%d", &choices);

    if (choices == 1)
    {
        start();
    }

    getch();
    return 0;
}

【问题讨论】:

  • void start();放在程序的开头,addition函数之前。
  • 这能回答你的问题吗? warning: implicit declaration of function
  • void start(); 在函数中只是简单地声明在程序的某个地方有一个名为start 的函数。它实际上并没有调用该函数;这仍然需要start();
  • 请注意,在 C 中,声明不带参数的函数的正确方法不是void start();,而是void start(void);。见stackoverflow.com/questions/41803937/func-vs-funcvoid-in-c99。 C++ 不同,请参阅stackoverflow.com/questions/51032/…
  • 无关,但char ope; scanf("%s", &amp;ope); 是一个严重的错误。它将一个无限长度的字符串读入一个仅能容纳一个字符的空间,从而导致写入超出范围和未定义的行为。像这样的代码就是人们被黑的方式。

标签: c codeblocks implicit-declaration


【解决方案1】:

start()addition() 之后声明,但是在addition() 中你调用start(),所以编译器不知道start() 是什么。此外,在start() 中,您还调用了addition(),因此最好的解决方法是使用前向声明:

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

void start(void); /* forward declaration */
void addition(void); /* forward declaration */

void addition(void)
{
    int vala, valb, resu;
    system("cls");
    printf("SiMPLE CALCULATOR 1.0a\n");
    printf("ADDITION\n");
    printf("\n");
    printf("Enter the first value of addend: ");
    scanf("%d", &vala);
    printf("Enter the second value of addend: ");
    scanf("%d", &valb);
    resu=vala+valb;
    printf("The sum of %d and %d is: %d\n", vala, valb, resu);
    printf("PRESS [ANY KEY] TO CONTINUE...");
    getch();

    start(); 
}

void start(void)
{
    char ope;
    system("cls");
    printf("SiMPLE CALCULATOR 1.0a\n");
    printf("What operation will be used:");
    scanf("%s", &ope);

    if (ope == 'a')
    {
        addition();
    }
    else if (ope == 'b')
    {
        printf("bbbbbbbbbbbb\n");
    }
    else
    {
        printf("ccccccccccc\n");
    }

}

【讨论】:

  • 这样做会产生更多警告,总共 8 个警告。我的加法、减法、乘法和除法函数隐含的 4 个警告。和 4 个警告说我的加法、减法、乘法和除法函数的类型冲突。
  • @Pyromagne 到底是什么警告?
  • 4 个隐含在我的加法、减法、乘法和除法函数中的警告。和 4 个警告说我的加法、减法、乘法和除法函数的类型冲突。
  • @Pyromagne 那些警告与更改无关,而是您的代码中您没有向我们展示的内容(我认为)
  • 哦,是的,我没有意识到这一点,但我用警告完成了它,但在我提交的版本中,总共有 2 个隐含的加法警告和冲突的加法类型。对不起
【解决方案2】:

不建议采取允许您从addition 调用start 的措施,这会导致潜在的无限递归,只是为了伪造一个循环。最好在addition 中删除start(); 调用并将scanf("%s", &amp;ope); 替换为while (scanf(" %c", &amp;ope) &gt; 0)

【讨论】:

  • 我的计划是在打印结果后我希望程序返回start 进行另一个计算,如果我要删除start();,我该怎么做?
  • 你可以完全按照我写的那样做;如果你不相信,你可以试试。 (函数addition 自动返回到它被调用的地方。)
猜你喜欢
  • 2012-02-29
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
相关资源
最近更新 更多