gpt4 book ai didi

c - 在终端(C 文件)中运行程序但未运行完成

转载 作者:行者123 更新时间:2023-11-30 19:04:15 24 4
gpt4 key购买 nike

我刚刚完成了 C 类的第一份作业,但我的作业文件尚未完成。

这是我的编译器代码:

$ cd hw1
$ gcc -o hw1 hw1.c
$ ./hw1

这是来自 hw1.c 的代码

#include <stdio.h>
#include <limits.h>

void ranges() {
printf("Signed Char/n");
printf("Minimum Value = %d\n", SCHAR_MIN);
printf("Maximum Value = %d\n", SCHAR_MAX);
printf("Unsigned Char\n");
printf("Minimum Value = 0\n");
printf("Maximum Value = %d\n", UCHAR_MAX);
printf("Signed Short \n");
printf("Minimum value = %d\n", SHRT_MIN);
printf("Maximum value = %d\n", SHRT_MAX);
printf("Unsigned Short \n");
printf("Minimum Value = 0\n");
printf("Maximum value = %d\n", USHRT_MAX);
printf("Signed Integer \n");
printf("Minimum value = %d\n", INT_MIN);
printf("Maximum value = %d\n", INT_MAX);
printf("Unsigned Integer \n");
printf("Minimum Value = 0\n");
printf("Maximum value = %ud\n", UINT_MAX);
printf("Signed Long \n");
printf("Minimum value = %li\n", LONG_MIN);
printf("Maximum value = %li\n", LONG_MAX);
printf("Unsigned Long \n");
printf("Minimum Value = 0\n");
printf("Maximum value = %lu\n", ULONG_MAX);
}

int factorial1(int n) {
int res = 1;
int i;
for (i = 1; i <= n; i++)
res = res * i;
return res;
}

int factorial2(int n) {
int res = 1;
int i = 1;
while (i <= n);{
res = res * i;
i++;
}
return res;
}

int factorial3(int n) {
int res = 1;
int i = 1;
do{
res = res * i;
i++;
}
while (i <= n);
return res;
}

int factorial4(int n) {
if (n >= 1)
return n*factorial4(n-1);
else
return 1;
}


void types() {
char a1,b1,c1,*p1,*p2,*p3;
short a2,b2,c2,*p4,*p5,*p6;
int a3,b3,c3,*p7,*p8,*p9;
double a4,b4,c4,*p10,*p11,*p12;
p1=&a1;
p2=&b1;
p3=&c1;
p4=&a2;
p5=&b2;
p6=&c2;
p7=&a3;
p8=&b3;
p9=&c3;
p10=&a4;
p11=&b4;
p12=&c4;
printf("\nAddress of %p %u\t", p1,p1);
printf("\nAddress of %p %u\t", p2,p2);
printf("\nAddress of %p %u\t", p3,p3);
printf("\nAddress of %p %u\t", p4,p4);
printf("\nAddress of %p %u\t", p5,p5);
printf("\nAddress of %p %u\t", p6,p6);
printf("\nAddress of %p %u\t", p7,p7);
printf("\nAddress of %p %u\t", p8,p8);
printf("\nAddress of %p %u\t", p9,p9);
printf("\nAddress of %p %u\t", p10,p10);
printf("\nAddress of %p %u\t", p11,p11);
printf("\nAddress of %p %u\n\n\n\n", p12,p12);
}


// test code; do not modify

int main() {

ranges();

printf("factorial1(10) = %d\n", factorial1(10));
printf("factorial2(10) = %d\n", factorial2(10));
printf("factorial3(10) = %d\n", factorial3(10));
printf("factorial4(10) = %d\n", factorial4(10));
printf("\n");

types();

return 0;
}

但是,当我使用 $ ./hw1 运行程序时,输出在 Factorial1 函数之后停止。再说一次,我是 C 新手,所以这是正常的吗?我需要做些什么才能看到其余的输出吗?对新手问题表示歉意,并尝试搜索,但无法弄清楚我的代码是否错误或其他问题。这是输出:

Signed Char
Minimum Value = -128
Maximum Value = 127
Unsigned Char
Minimum Value = 0
Maximum Value = 255
Signed Short
Minimum value = -32768
Maximum value = 32767
Unsigned Short
Minimum Value = 0
Maximum value = 65535
Signed Integer
Minimum value = -2147483648
Maximum value = 2147483647
Unsigned Integer
Minimum Value = 0
Maximum value = 4294967295d
Signed Long
Minimum value = -9223372036854775808
Maximum value = 9223372036854775807
Unsigned Long
Minimum Value = 0
Maximum value = 18446744073709551615
factorial1(10) = 3628800

谢谢

最佳答案

factorial2 中存在拼写错误,导致无限循环:

while (i <= n);{

应该是:

while (i <= n) {

关于c - 在终端(C 文件)中运行程序但未运行完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52396717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com