C: Looping without using looping statements or recursion

前端 未结 16 1842
走了就别回头了
走了就别回头了 2021-02-04 07:56

I want to write a C function that will print 1 to N one per each line on the stdout where N is a int parameter to the function. The function should not use while, for, do-while

16条回答
  •  一整个雨季
    2021-02-04 09:03

     #include "stdio.h"
    
     #include "stdlib.h"
    
     #include "signal.h"
    
     int g_num;
    
     int iterator;
    
     void signal_print()
    
     {
    
            if(iterator>g_num-1)
    
                    exit(0);
    
            printf("%d\n",++iterator);
    
     }
    
     void myprintf(int n)
    
     {
    
         g_num=n;
    
            int *p=NULL;
    
         int x= *(p); // the instruction is reexecuted after handling the signal
    
     }
    
     int main()
    
     {
    
            signal(SIGSEGV,signal_print);
    
            int n;
    
            scanf("%d",&n);
    
            myprintf(n);
    
            return 0;
    
     }
    

提交回复
热议问题