【问题标题】:Why it doesn't work ( C Lang in Dev C++ ) [closed]为什么它不起作用(Dev C++ 中的 C 语言)[关闭]
【发布时间】:2014-04-29 21:52:06
【问题描述】:

请告诉我,这里出了什么问题,它可以编译,但是当我输入数字时控制台崩溃。我不知道接下来要写什么,我只是写点东西让我的帖子成为可能。

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) 
{
    unsigned int i,l,p,w;
    printf("Enter natural number excluding 0: ");
    scanf("%d",l);
    p = 1;
    for(i=1;i<=l;i++)
    {
        p=p*i;
    }
    w=p;
    printf("\nFactorial of entered number %d",w);
    return 0;
}

【问题讨论】:

  • scanf 的第二个参数应该是指向变量的指针,而不是变量本身。
  • 获得更好的编译器。 GCC 打印:warning: format '%d' expects type 'int *', but argument 2 has type 'unsigned int'

标签: c crash console factorial


【解决方案1】:

scanf("%d",l);需要插入l的地址,即&amp;l

对于无符号整数,您还应该使用%u,而不是%d

【讨论】:

  • @user3587082 不客气。不要忘记接受以后对您最有帮助的答案。
猜你喜欢
  • 2017-07-31
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 2018-02-06
  • 2017-11-07
相关资源
最近更新 更多