【发布时间】: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", &ope);是一个严重的错误。它将一个无限长度的字符串读入一个仅能容纳一个字符的空间,从而导致写入超出范围和未定义的行为。像这样的代码就是人们被黑的方式。
标签: c codeblocks implicit-declaration