如何在linux上修复这个简单的代码错误? [英] How can I fix this simple code error on linux?

查看:70
本文介绍了如何在linux上修复这个简单的代码错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我在Linux上练习使用Vim。

我做了一个这样的简单代码



当我输入-1和0 , 有用。它们返回0和1.

然而,当我在n上放置正整数值时,它不起作用。

我试图找出原因以便我使用gdb

但它只是这样说:



程序收到信号SIGSEGV,分段错误。

0x0000000000400620 in factorial()



我的代码出了什么问题?我甚至无法理解这一点。

为什么GDB没有注意到哪一行出错?



我尝试了什么:



 #include< stdio.h> 

int factorial(int n){
if(n< 0){return 0; }
else if(n == 0){return 1; }
else {return n * factorial(n); }
}

int main(void){
int n = 0;

printf(Put n value:);
scanf(%d,& n)

printf(%d!=%d \ n,n,factorial(n));

返回0;
}

解决方案

尝试:

  else  { return  n * factorial(n-  1 ) ; } 


Now I am practicing using Vim on Linux.
I made a simple code like this

when I put -1 and 0, it works. they return 0 and 1.
However, when I put positive integer values on n, it didn't work.
I tried to find out the reason so that I used gdb
but it just said like this :

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400620 in factorial ()

What's wrong with my code?? I even cannot understand the point.
Why didn't GDB notice which line got the error?

What I have tried:

#include <stdio.h>
        
        int factorial(int n){
           if (n<0) { return 0; }
           else if (n==0) { return 1; }
           else { return n * factorial(n); }
        }
        
        int main(void){
           int n = 0;
        
           printf("Put n value : ");
           scanf("%d", &n)
        
           printf("%d! = %d\n", n, factorial(n));
        
           return 0;
    }

解决方案

try:

else { return n * factorial(n-1); }


这篇关于如何在linux上修复这个简单的代码错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆