c - 简单的 C 源代码中有错误吗?

标签 c

我想知道我编写的用户定义函数的格式,即 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/

相关文章:

c - 解码偏移宏

c - 未收到后续消息

c - 如何从函数中引用指针?

c - dup2() 正在阻止输出

c - 在函数 ‘yylex’ : 'Variable’ undeclared 中

c - 如何在ARMv6+上实现16bit立体声混音?

c - 如何设置 "General->TargetName"、 "Debugging->Command"和 "Linker->General->Output File"

c++ - va_arg 返回错误的参数

c++ - C++(C?)中变量的多个预增量操作

c - C中二维数组的段错误