E Balaguruswamy 书第 1 章主题子程序第 3 版中的 C 代码错误
error in C codes from edition 3 of E Balaguruswamy book chapter-1 topic-subroutines
printf 行出错,调试器说 printf 原型。
你能以给出预期值的方式编辑程序吗?
在 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
的声明必须在函数的body内部
并且您必须在声明函数 printf
的地方包含 header <stdio.h>
。
请注意,通常两个整数相乘会导致溢出。因此,更好的函数定义可以如下所示,如演示程序中所示。
#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;
}
printf 行出错,调试器说 printf 原型。 你能以给出预期值的方式编辑程序吗?
在 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
的声明必须在函数的body内部
并且您必须在声明函数 printf
的地方包含 header <stdio.h>
。
请注意,通常两个整数相乘会导致溢出。因此,更好的函数定义可以如下所示,如演示程序中所示。
#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;
}