为什么我不能在这个程序中构建一个无返回类型的函数?

use*_*330 1 c return function void

这是一个使用函数乘以和添加两个数字的小程序.

#include<conio.h>
#include<stdio.h>

int main()

{
 int a,b,result;
 clrscr();
 printf("Enter two numbers to be added and multiplied...\n");
 scanf("%d%d",&a,&b);
add(a,b);
 getch();
 return 0;
}

int add(int a,int b)
{
int res;
 printf("%d + %d = %d", a,b,a+b);
 res=mult(a,b);
 printf("\n%d * %d = %d",a,b,res);
 return 0;
}

int mult(int a,int b)
{
 return a*b;
}
Run Code Online (Sandbox Code Playgroud)

虽然,我不认为我需要有一个返回类型添加功能,所以我试着使用这个代码...

#include<conio.h>
#include<stdio.h>

int main()

{
 int a,b,result;
 clrscr();
 printf("Enter two numbers to be added and multiplied...\n");
 scanf("%d%d",&a,&b);
add(a,b);
 getch();
 return 0;
}

void add(int a,int b)
{
int res;
 printf("%d + %d = %d", a,b,a+b);
 res=mult(a,b);
 printf("\n%d * %d = %d",a,b,res);
}

int mult(int a,int b)
{
 return a*b;
}
Run Code Online (Sandbox Code Playgroud)

但它告诉我错配类型声明有错误?

NPE*_*NPE 6

您需要在首次使用之前提供原型:

void add(int a, int b); /* This tells the compiler that add() takes */
                        /* two ints and returns nothing.            */

int main() {
  ...
  add(a, b);
}

void add(int a, int b) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

否则编译器必须假设add()返回一个int.

有关更多信息,请参阅必须在C中声明函数原型?