繁体   English   中英

代码块上的 C 隐式函数声明

[英]Implicit Declaration of Function in C on CodeBlocks

你好,我正在练习我的 C 语言知识 我正在尝试制作一个简单的计算器,但我遇到了这个警告Implicit Declaration of Function但我调用的函数已被执行。 我试图用这个void start();修复它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();失败void 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;
}

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");
    }

}

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

暂无
暂无

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

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