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

时间:2018-09-19 01:28:11

标签: c unix

我刚刚在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

谢谢

1 个答案:

答案 0 :(得分:1)

您在factorial2中遇到错字,导致无限循环:

while (i <= n);{

应为:

while (i <= n) {