在C中不使用循环,递归和宏扩展即可打印100次

在本节中,我们将看到如何在C中打印数字100次。存在一些约束。我们不能使用循环,递归或宏扩展。

为了解决这个问题,我们将使用C的的setjump和跳远踏板setjump()longjump()位于SETJMP.H库。这两个函数的语法如下。

示例

#include <stdio.h>
#include <setjmp.h>
jmp_buf buf;
main() {
   int x = 1;
   setjmp(buf); //set the jump position using buf
   printf("5"); // Prints a number
   x++;
   if (x <= 100)
      longjmp(buf, 1); // Jump to the point located by setjmp
}

输出结果

5555555555555555555555555555555555555555555555555555555555555555555555555555
555555555555555555555555