C语言 线程:信号 SIGABRT (lldb) 和程序以退出代码 -1 结尾



程序停止,线程出现在冲刺行中:

sprintf(x,"%d",x2);

这不是将 int (x2) 转换为字符串 (x) 的最佳方法吗?

int check(int n,char [8]);
int main(){
    char x[8]="0";
     int N,x2;
    scanf("%d",&N);
    while(strlen(x)<9){
        if(check(N,x)) printf("%sn",x);
        x2=atoi(x);
        x2++;
        sprintf(x,"%d",x2);
        }
    return 0;
    }

int check(int n,char x[8]){
    int l,i,y,count;
    l=strlen(x);
    y=atoi(x);
    count=0;
    for(i=0;i<l;i++){
        count=count+pow((x[i]-'0'),n);
    }
    if(count==y) return 1;
    else return 0; }

如果您发布编译行 + 实际编译的代码(即包含正确的标头!移动到 10 的数组大小后,这工作正常(尽管需要一段时间才能退出!

修改后的代码:

C:...>cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int check(int n,char [10]);
int main(){
    char x[10]="0";
     int N,x2;
    scanf("%d",&N);
    while(strlen(x)<9){
        if(check(N,x)) printf("%sn",x);
        x2=atoi(x);
        x2++;
        sprintf(x,"%d",x2);
        }
    return 0;
    }

int check(int n,char x[10]){
    int l,i,y,count;
    l=strlen(x);
    y=atoi(x);
    count=0;
    for(i=0;i<l;i++){
        count=count+pow((x[i]-'0'),n);
    }
    if(count==y) return 1;
    else return 0; }

编译和链接:

C:...>gcc -Wall -g main.c

调试:

C:...>gdb a.exe
GNU gdb (GDB) 7.6.2
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from C:...a.exe...done.
(gdb) run
Starting program: C:.../a.exe
[New Thread 8396.0x199c]
3
0
1
153
370
371
407
[Inferior 1 (process 8396) exited normally]
(gdb) quit

世界一切都很好!:D

最新更新