gpt4 book ai didi

c - E Balaguruswamy 书第 1 章主题子例程第 3 版中的 C 代码错误

转载 作者:行者123 更新时间:2023-11-30 20:17:38 25 4
gpt4 key购买 nike

printf 行出错,调试器显示 printf 原型(prototype) 。你能以给出预期值的方式编辑程序吗?

在 code::blocks、turbo c 和 c 编译器在线测试版(android)上尝试过,但出现错误

/*program using function*/
int mul(int a, int b); /*declaration*/
main()
{
int a,b,c;
a=5;
b=10;
c= mul (a,b);
printf("multiplication of %d and %d is %d" , a, b, c);
}
/*main program ends*/
/*mul() function starts*/
int mul (int x, int y)
int p;
{
p=x*y;
return(p);
}

预期输出-5 和 10 相乘是 50

最佳答案

该函数应该定义如下

int mul (int x, int y)
{
int p;

p = x * y;

return p;
}

即局部变量p的声明必须位于函数体内。

并且您必须包含 header <stdio.h>其中函数 printf已声明。

请考虑到,通常两个整数相乘可能会导致溢出。因此,更好的函数定义可以如下所示,如演示程序中所示。

#include <stdio.h>

long long int mul( int, int );

int main(void)
{
int a = 5, b = 10;
long long int c = mul( a, b );

printf( "multiplication of %d and %d is %lld\n" , a, b, c );
}

long long int mul( int x, int y )
{
return ( long long int )x * y;
}

关于c - E Balaguruswamy 书第 1 章主题子例程第 3 版中的 C 代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56723180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com