Recursive functions in C and printf

后端 未结 2 1945
無奈伤痛
無奈伤痛 2021-01-26 23:20

So, first of all I\'m a total beginner in C, we\'re studying it at University on the course \'Structured Programming\'.

Now, the last few lectures about \'Recursive func

2条回答
  •  失恋的感觉
    2021-01-26 23:53

    If one wants to print recursively the bits of a char with leading zeros, he may use following code:

    #include 
    
    void print_bit_iter(char x, int n)
    {
        int bit = (x & (1 << n - 1)) != 0;
        printf("%d", bit);
    
        if(n != 0)
          print_bit_iter(x, n - 1);
    }
    
    int main()
    {
      print_bit_iter('U', 8);
    }
    

    This will have

    01010101
    

    as the output.

提交回复
热议问题