簡體   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