我想知道我编写的用户定义函数的格式,即 return(xxx) 是否正确。 因为当我编译代码时,我必须输入两次。这可能是一个愚蠢的错误,因为我刚刚开始学习 C 语言 ****我的代码:****``
#include<stdio.h>
long cube(long x );
long input,answer;
int main (void )
{
printf("Enter a number:");
scanf("%ld ",&input);
answer = cube(input);
printf(" The cube of %ld is %ld",input ,answer);
return 0;
}
long cube(long x )
{
return (x*x*x);
}
****回答****
#include <stdio.h>
long cube(long x);
long input, answer;
int main( void )
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.\n", input, answer);
return 0;
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
最佳答案
删除“%ld”后面的空格,它将接受一个输入。 根据你的代码, scanf("%ld'&输入); 这里,编译器首先需要“%ld”的一个输入,然后等待您在“%ld”之后使用的空格。删除它然后它会在输入一次后进入下一步。 你应该使用, scanf("%ld%",&input);
关于c - 简单的 C 源代码中有错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45148162/