C语言中使用函数计算GCD时出错

C语言中使用函数计算GCD时出错,c,C,这段代码显示了一些语法错误,我无法找出它在哪里,可能是在gcd函数中。这是我的密码 #include<stdio.h> #include<conio.h> int main(void) int gcd(int,int); { int a,b, temp; printf("Enter the value of a"); scanf("%d",&a); printf("Enter the value of b");

这段代码显示了一些语法错误,我无法找出它在哪里,可能是在gcd函数中。这是我的密码

#include<stdio.h>
#include<conio.h>
int main(void)

int gcd(int,int);
{
     int a,b, temp;
     printf("Enter the value of a");
     scanf("%d",&a);
     printf("Enter the value of b");
     scanf("%d",&b);
     if (b>=a)
     {
         int temp;
         temp=b;
         b=a;
         a=temp;

     }
     elseif(b!=0)
     {
         gcd(a,b);
         printf("The Gcd of the numbere is %d",gcd(a,b));
     }
     else
     {
         printf("The GCD of %d %d is %d:",a,b,a);
     }
     getch();

     return 0;
}

int gcd(int a,int b)
{
    int g;
    while(b!=0)
    {
               g=a%b;
               a=b;
               b=g;
               return g;
    }

}
#包括
#包括
内部主(空)
int gcd(int,int);
{
内部a、b、温度;
printf(“输入a的值”);
scanf(“%d”和“&a”);
printf(“输入b的值”);
scanf(“%d”和“b”);
如果(b>=a)
{
内部温度;
温度=b;
b=a;
a=温度;
}
elseif(b!=0)
{
gcd(a、b);
printf(“数字的Gcd为%d”,Gcd(a,b));
}
其他的
{
printf(“%d%d的GCD为%d:,a,b,a);
}
getch();
返回0;
}
内部gcd(内部a、内部b)
{
int g;
而(b!=0)
{
g=a%b;
a=b;
b=g;
返回g;
}
}

如果您能指出我的错误并用正确的代码进行解释,我将不胜感激。

切换这两行的位置:

int main(void)

int gcd(int,int);

另外,
elseif
->
else if

gcd函数使用。它计算
a mod b
,然后将
a
b
与a进行交换

#包括
int gcd(int,int);
内部主(空)
{
内部a、b、温度;
printf(“输入a:”)的值;
scanf(“%d”和“&a”);
printf(“输入b的值:”);
scanf(“%d”和“b”);
int res=gcd(a,b);
printf(“号码的Gcd为:%d\n”,res);
返回0;
}
内部gcd(内部a、内部b)
{
内部温度;
而(b!=0)
{
温度=a%b;
a=b;
b=温度;
}
返回a;
}

为什么你贴上了这个C++?错误信息包括一个行号。它告诉你它在哪里。编译器是否告诉你发生这些错误的行号<代码>整数gcd(整数,整数)是从第5行开始的可疑错误,错误是什么?您应该解释您所做的更改。代码看起来似乎是正确的(我没有编译它),但您没有解释您所做的。解释是答案的关键部分。代码本身不足以做出一个好的答案——或者至少,它几乎永远都不足以。我尽可能地解释了。)指向外部源的链接不是解释。而且,您(和链接)没有解释OP的代码有什么问题,或者存在什么语法错误。还需要另一个关键的更改—将
返回
从GCD函数的循环中移出。这是一个逻辑错误,而不是语法错误,这是最初的问题。
#include<stdio.h>

int gcd(int ,int );


int main(void)
{
     int a,b, temp;
     printf("Enter the value of a : ");
     scanf("%d",&a);
     printf("Enter the value of b : ");
     scanf("%d",&b);

     int res = gcd(a,b);
     printf("The Gcd of the numbere is : %d \n",res);

     return 0;
}

int gcd(int a, int b)
{
    int temp;
    while (b != 0)
    {
        temp = a % b;
        a = b;
        b = temp;
    }
    return a;
}