1、输出整数:%d
int a = 10;
printf("%d\n",a);
2、输出无符号数:%u
unsigned int b = 25;
printf("%u\n",b);
3、输出十六进制数:%x
int hex = 0x25;
printf("%x\n",hex);
4、输出指针指向的地址:%p
int a = 0x12345678;
int *p = &a;
printf("%p\n",p);
5、输出字符串:%s
//char str[] = "abcde";
char *str = "abcde";
printf("%s\n",str);
6、输出单个字符:%c
char c = '1';
printf("%c\n",c);
7、输出二进制格式
void print_bin(int number){
int bit = sizeof(int)*8;
int i;
for(i = bit - 1;i >= 0;i--){
int bin = (number & (1 << i)) >> i;
printf("%d", bin);
}
printf("\n");
}
int main(void){
unsigned int b = 5;
print_bin(b);
return 0;
}
输出二进制需要用到自定义函数,效果如图
文章来源:https://www.toymoban.com/news/detail-588820.html
8、输出二进制无符号格式文章来源地址https://www.toymoban.com/news/detail-588820.html
/*输出二进制无符号数*/
void print_bin(unsigned int number){
unsigned int bit = sizeof(unsigned int)*8;
int i;
for(i = bit - 1;i >= 0;i--){
unsigned int bin = (number & (1 << i)) >> i;
printf("%u", bin);
}
printf("\n");
}
到了这里,关于C中printf()的常用输出的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!