[英]Implicit Declaration of Function in C on CodeBlocks
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.你好,我正在练习我的 C 语言知识 我正在尝试制作一个简单的计算器,但我遇到了这个警告Implicit Declaration of Function
但我调用的函数已被执行。 I tried to fix it with this void start();
我试图用这个void start();
修复它void start();
but the function did not execute.但函数没有执行。
Successful executed the function start();
成功执行函数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();
执行函数void start();
失败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;
}
start()
is declare after addition()
, but in addition()
you call start()
, so the compiler don't know what start()
is. start()
是申报后, addition()
但addition()
调用start()
所以编译器不知道什么start()
是。 Also, in start()
you also call addition()
, so the best way to reslove this is using forward declaration:此外,在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");
}
}
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.不建议采取允许您从addition
调用start
措施,从而导致潜在的无限递归,只是为了伪造一个循环。 Better drop the start();
最好删除start();
call in addition
and replace the scanf("%s", &ope);
addition
调用并替换scanf("%s", &ope);
with while (scanf(" %c", &ope) > 0)
.使用while (scanf(" %c", &ope) > 0)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.