1

Hello I am practicing my knowledge in C language I am trying to make a simple calculator but I encountered this warning Implicit Declaration of Function but the function that I called has been executed. I tried to fix it with this void start(); but the function did not execute.

Successful executed the function start(); but have a implicit warning:

#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;
}

Failed to execute the function void start(); start but no implicit warning:

#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;
}

Pyromagne
  • 45
  • 8
  • Put `void start();` at the beginning of the program, before the `addition` function. – Barmar Nov 16 '21 at 06:25
  • 1
    Does this answer your question? [warning: implicit declaration of function](https://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function) – kaylum Nov 16 '21 at 06:28
  • 1
    `void start();` within a function simply declares that somewhere in the program there is a function called `start`. It does not actually call that function; that still would have to be done with just `start();` – Nate Eldredge Nov 16 '21 at 06:47
  • 1
    Note that in C, the proper way to declare a function taking no arguments is not `void start();` but `void start(void);`. See https://stackoverflow.com/questions/41803937/func-vs-funcvoid-in-c99. C++ is different, see https://stackoverflow.com/questions/51032/is-there-a-difference-between-foovoid-and-foo-in-c-or-c/51080#51080. – Nate Eldredge Nov 16 '21 at 06:49
  • 1
    Unrelated, but `char ope; scanf("%s", &ope);` is a serious bug. It reads a string of unlimited length into a space only large enough for one character, resulting in writing out of bounds and undefined behavior. Code like this is how people get hacked. – Nate Eldredge Nov 16 '21 at 06:52
  • @NateEldredge im gonna change it later with %c thanks for saying that. – Pyromagne Nov 16 '21 at 07:00
  • @NateEldredge ,ohh im gonna try that `void start(void);` thanks – Pyromagne Nov 16 '21 at 07:06
  • @NateEldredge it worked `void start(void);` thank you very much. – Pyromagne Nov 16 '21 at 07:10

2 Answers2

2

start() is declare after addition(), but in addition() you call start(), so the compiler don't know what start() is. Also, in start() you also call addition(), so the best way to reslove this is using forward declaration:

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

}
  • doing that will make more warnings a total of 8 warnings. 4 warnings with implicit to my addition, subtraction, multiplication and division functions. and 4 warnings saying that conflicting types for my addition, subtraction, multiplication and division functions. – Pyromagne Nov 16 '21 at 06:53
  • @Pyromagne What warning, exactly? – justANewb stands with Ukraine Nov 16 '21 at 06:54
  • 4 warnings with implicit to my addition, subtraction, multiplication and division functions. and 4 warnings saying that conflicting types for my addition, subtraction, multiplication and division functions. – Pyromagne Nov 16 '21 at 06:57
  • @Pyromagne those waring doesn't related to the changes, but rather something in your code that you haven't show us (I think) – justANewb stands with Ukraine Nov 16 '21 at 07:00
  • ohhh yeah I didnt realized it but I finished it with warnings but in the version I submited theres a total of 2 warning implicit for addition and conflicting type for addition. sorry – Pyromagne Nov 16 '21 at 07:05
  • Hi thanks for taking time with my question, I change `void start();` to `void start(void);` and it solved my problem. – Pyromagne Nov 16 '21 at 07:14
1

It is not advisable to take measures which allow you to call start from addition, leading to a potentially infinite recursion, only to fake a loop. Better drop the start(); call in addition and replace the scanf("%s", &ope); with while (scanf(" %c", &ope) > 0).

Armali
  • 18,255
  • 14
  • 57
  • 171
  • My plan is after printing the result I want the program go back to `start` to make another calculation how can I do that if I am going to remove `start();`? – Pyromagne Nov 19 '21 at 11:54
  • 1
    You can do that exactly as I wrote; if you don't believe it, you can try. (The function `addition` automatically returns to where it was called.) – Armali Nov 19 '21 at 13:06