当前位置:   article > 正文

C中printf()的常用输出_printf输出字符串

printf输出字符串

1、输出整数:%d

  1. int a = 10;
  2. printf("%d\n",a);

2、输出无符号数:%u

  1. unsigned int b = 25;
  2. printf("%u\n",b);

3、输出十六进制数:%x

  1. int hex = 0x25;
  2. printf("%x\n",hex);

4、输出指针指向的地址:%p

  1. int a = 0x12345678;
  2. int *p = &a;
  3. printf("%p\n",p);

5、输出字符串:%s

  1. //char str[] = "abcde";
  2. char *str = "abcde";
  3. printf("%s\n",str);

6、输出单个字符:%c

  1. char c = '1';
  2. printf("%c\n",c);

7、输出二进制格式

  1. void print_bin(int number){
  2. int bit = sizeof(int)*8;
  3. int i;
  4. for(i = bit - 1;i >= 0;i--){
  5. int bin = (number & (1 << i)) >> i;
  6. printf("%d", bin);
  7. }
  8. printf("\n");
  9. }
  10. int main(void){
  11. unsigned int b = 5;
  12. print_bin(b);
  13. return 0;
  14. }

输出二进制需要用到自定义函数,效果如图

8、输出二进制无符号格式

  1. /*输出二进制无符号数*/
  2. void print_bin(unsigned int number){
  3. unsigned int bit = sizeof(unsigned int)*8;
  4. int i;
  5. for(i = bit - 1;i >= 0;i--){
  6. unsigned int bin = (number & (1 << i)) >> i;
  7. printf("%u", bin);
  8. }
  9. printf("\n");
  10. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
  

闽ICP备14008679号