Is there a printf converter to print in binary format?

前端 未结 30 3333
盖世英雄少女心
盖世英雄少女心 2020-11-21 06:20

I can print with printf as a hex or octal number. Is there a format tag to print as binary, or arbitrary base?

I am running gcc.

printf(\"%d %x %o         


        
30条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-21 06:52

    The following recursive function might be useful:

    void bin(int n)
    {
        /* Step 1 */
        if (n > 1)
            bin(n/2);
        /* Step 2 */
        printf("%d", n % 2);
    }
    

提交回复
热议问题