c - k 的无关值

标签 c

程序陷入无限循环。我尝试获取 k 的值,但它显示为 854。似乎我在 checktrue() 函数中犯了错误。尝试了几个小时。没有得到问题。

#include <stdio.h>
#include <conio.h>
int checktrue(int *p);
int k;
long long int m;
void main()
{
long long int fir=1;
long long int pfir=0,n=0;
long long int sec=2;
long long int sum=fir;
clrscr();
while (n!=5)
{
sum=sum+sec;
pfir=fir;
fir=sec;
sec=sec+pfir;
n=checktrue(&sec);

}
printf("The sum is %llu",sum);
getch();
}
int checktrue(int *p)
{
k=0;
m=*p;
while(m!=0)
{
m=m/10;
k++;
}
return(k);
}

最佳答案

您传递一个指向sec的指针,该指针允许函数checktrue()操作输入。如果你去掉它,我会得到输出 15

#include <stdio.h>
#include <stdlib.h>

unsigned long long checktrue(unsigned long long p);
int main()
{
  unsigned long long fir = 1, n = 0;
  unsigned long long sec = 2;
  unsigned long long sum = fir;
  while (n != 2) {
    sum = sum + sec;
    fir = sec;
    sec = sec + fir;
    n = checktrue(sec);
  }
  printf("The sum is %llu\n", sum);
  exit(EXIT_SUCCESS);
}

unsigned long long checktrue(unsigned long long p)
{
  unsigned long long k = 0;
  while (p != 0) {
    p /= 10;
    k++;
  }
  printf("Exited Succesfully %llu\n", k);
  return k;
}

关于c - k 的无关值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39966049/

相关文章:

c - 下面这个简单程序的复杂度是多少?

c - 使用 const 运算符发出警告(在结构中)

c - 用 C 打印文件中的所有重复单词

c - 在 C 中以独立于机器的方式引用堆地址

c++ - 按顺序移动数组中的元素

c - 3n+1算法

c - c中的迭代目录遍历

c - fwrite 是否可以写入一些字节但返回零?

c - 对 if( argv[1][0] != ~'t' ) 等表达式求值

c - 无法使用 fopen() 打开文件