我在 scanf 中的输入不起作用,过程被返回并且我没有得到任何输出
My input in scanf is not working, process is returned and I don't get any output
斐波那契数列未在 运行 此程序中获得。整个过程在 scanf
.
中输入后终止
#include <stdio.h>
#include <stdlib.h>
int fibonacci(int);
int main()
{
int n, i = 0, c;
printf("Print the fibonacci series");
scanf("%d", n);
for (c = 1; c <= n; c++)
{
printf("%d\n", fibonacci(i));
i++;
}
return 0;
}
int fibonacci(int n)
{
if (n = 0)
return 0;
else if (n = 1)
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2));
}
对于 scanf,您需要给出变量的地址。
scanf("%d",&n); <= need to give the address of the integer
您可以在此处找到一些示例:
http://www.cplusplus.com/reference/cstdio/scanf/
正如您在 中所说,scanf
需要每个格式说明符的地址。因此,如果提供格式说明符 %d
,则需要整数地址:scanf
将在此处写入值。
如果n
是包含整数的变量,&n
是它的地址。传递不是地址的东西会引起麻烦:它是 未定义的行为,可能会导致分段错误。
您的斐波那契数列生成器也存在一些问题。我想你想打印序列中的 n-th 数字,但是你迭代 n
次调用 fibonacci()
函数(仅 returns 最后一个值) 始终带有参数 i
,其值为 0.
在 fibonacci
函数中您尝试检查退出条件,但 注意 :
if (n = 0)
return 0;
不检查 n
的值;它 执行赋值 (n
的值将为 0,条件将为假)。所以它将进行下一个“测试”
if (n = 1)
return 1;
也是一个赋值,1被赋值给n
所以条件为真返回1。这就是为什么你看到 1 n
次。
为了让它发挥作用
- 更正
scanf
问题
- 将
c
传给fibonacci()
- 更正函数以便测试值(
==
而不是 =
)
#include <stdio.h>
#include <stdlib.h>
int fibonacci(int);
int main()
{
int n, c;
printf("Print the fibonacci series\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
{
printf("%d\n", fibonacci(c));
}
return 0;
}
int fibonacci(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2));
}
您在 scanf
语句中错过了 &
符号,而且,我认为您对赋值运算符 =
和逻辑等于 ==
感到困惑,在斐波那契函数。
#include <stdio.h>
#include <stdlib.h>
int fibonacci(int);
int main()
{
int n, i = 0, c;
printf("Print the fibonacci series");
scanf("%d", &n);
for (c = 1; c <= n; c++)
{
printf("%d\n", fibonacci(i));
i++;
}
return 0;
}
int fibonacci(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2));
}
斐波那契数列未在 运行 此程序中获得。整个过程在 scanf
.
#include <stdio.h>
#include <stdlib.h>
int fibonacci(int);
int main()
{
int n, i = 0, c;
printf("Print the fibonacci series");
scanf("%d", n);
for (c = 1; c <= n; c++)
{
printf("%d\n", fibonacci(i));
i++;
}
return 0;
}
int fibonacci(int n)
{
if (n = 0)
return 0;
else if (n = 1)
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2));
}
对于 scanf,您需要给出变量的地址。
scanf("%d",&n); <= need to give the address of the integer
您可以在此处找到一些示例: http://www.cplusplus.com/reference/cstdio/scanf/
正如您在 scanf
需要每个格式说明符的地址。因此,如果提供格式说明符 %d
,则需要整数地址:scanf
将在此处写入值。
如果n
是包含整数的变量,&n
是它的地址。传递不是地址的东西会引起麻烦:它是 未定义的行为,可能会导致分段错误。
您的斐波那契数列生成器也存在一些问题。我想你想打印序列中的 n-th 数字,但是你迭代 n
次调用 fibonacci()
函数(仅 returns 最后一个值) 始终带有参数 i
,其值为 0.
在 fibonacci
函数中您尝试检查退出条件,但 注意 :
if (n = 0)
return 0;
不检查 n
的值;它 执行赋值 (n
的值将为 0,条件将为假)。所以它将进行下一个“测试”
if (n = 1)
return 1;
也是一个赋值,1被赋值给n
所以条件为真返回1。这就是为什么你看到 1 n
次。
为了让它发挥作用
- 更正
scanf
问题 - 将
c
传给fibonacci()
- 更正函数以便测试值(
==
而不是=
)
#include <stdio.h>
#include <stdlib.h>
int fibonacci(int);
int main()
{
int n, c;
printf("Print the fibonacci series\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
{
printf("%d\n", fibonacci(c));
}
return 0;
}
int fibonacci(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2));
}
您在 scanf
语句中错过了 &
符号,而且,我认为您对赋值运算符 =
和逻辑等于 ==
感到困惑,在斐波那契函数。
#include <stdio.h>
#include <stdlib.h>
int fibonacci(int);
int main()
{
int n, i = 0, c;
printf("Print the fibonacci series");
scanf("%d", &n);
for (c = 1; c <= n; c++)
{
printf("%d\n", fibonacci(i));
i++;
}
return 0;
}
int fibonacci(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2));
}