zoukankan      html  css  js  c++  java
  • printf那点事

    1、打印内存地址

    #include<stdio.h>
    int main()
    {
    	int a;
    	printf("%p
    ",&a);   //%p打印地址,自己主动加前缀00
    	printf("0x%x
    ",&a); //%x以十六进制打印
    	return 0;
    }
    输出结果:

    0012FF44
    0x12ff44


    2、printf。sprintf,snprintf

    原型:

    int printf(const char *format, ...);
    int sprintf(char *str, const char *format, ...);
    int snprintf(char *str, size_t size, const char *format, ...);
    sprintf 将字符串格式化输入到目标串中。

    snprintf将字符串最多复制 size-1 个字符到目标串中。

    #include<stdio.h>
    int main()
    {
    	char a[20];
    	char *p = "Hello,iot_xiaohe";
    	sprintf(a,"%s",p);
    	printf("sprintf a is: %s
    ",a);
    	snprintf(a,10,"%s",p);
    	printf("snprintf a is: %s
    ",a);
    	return 0;
    }
    输出结果:

    sprintf a is: Hello,iot_xiaohe
    snprintf a is: Hello,iot



  • 相关阅读:
    POJ 2486
    奇怪的电梯
    穿越泥地(mud)
    救援行动(save)
    As Fast As Possible
    Connecting Universities
    They Are Everywhere
    Cells Not Under Attack
    吃饭
    花店橱窗(flower)
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5058704.html
Copyright © 2011-2022 走看看