• C中的一些函数


    简述:printf、sprintf函数

    转载自http://www.cnblogs.com/adslg/archive/2008/08/22/1274164.html 部分进行了修改,参考http://www.cplusplus.com/reference/cstdio/printf/?kw=printf


    printf()函数是格式化输出函数,一般用于向标准输出设备按规定格式输出信息,调用格式为:printf("<格式化字符串>", <参量表>);其中格式化字符串包括两部分内容:
    1.正常字符。这些字符将按原样输出;
    2.格式化规定字符。以"%"开始,后跟一个或几个规定字符,用来确定输出内容格式。
    参量表是需要输出的一系列参数,其个数必须与格式化字符串所说明的输出参数个数一样多,各参数之间用","隔开,且顺序一一对应,否则将会出现意想不到的错误。

    格式化规定符如下:
    %d    十进制有符号整数
    %u    十进制无符号整数
    %f    浮点数
    %s    字符串
    %c    字符
    %p    指针的值
    %e    指数形式的浮点数
    %x, %X    十六进制表示的无符号整数
    %0    八进制表示的无符号整数
    %g    自动选择合适的表示法

    1.可以在"%"与字母之间插进数字表示最大场宽,例如"%3d"表示输出3位整型数,不够3位右对齐;"%9.2f"表示输出场宽为9的浮点数,其中小数位2位,整数位6位,小数点占一位,不够9位右对齐;"%8s"表示输出8个字符的字符串,不够8个字符右对齐。
    对于整型数或者字符串,如果长度超过说明的场宽,则按实际长度输出;
    对于浮点数,如果整数部分的长度超过说明的场宽,则按实际整数位输出;如果小数部分的长度超过说明的小数位宽度,则按说明的小数位宽度四舍五入输出。

    .precison
    For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
    [对于整型数,precision表示要显示的最小长度,如果整型数的长度比precision小,则会自动添加前导零;如果整型数的长度比precision大,则按实际长度输出]
    For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).
    [对于浮点数,precision表示表示小数位的位数,默认情况下为6位]
    For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
    [对于字符串,precision表示要打印的字符串长度]

    2.如果想在输出值前加一些0,就应在场宽项前加个0,例如"%04d"表示在输出一个小于4位的整型数时,将在前面补0使其总宽度为4位。
    3.可以在"%"与字母之间加小写字母l表示输出的是长型数,例如"%ld"表示输出长整型数,"%lf"表示输出double类型的浮点数。
    4.可以在"%"与字母之间加"-"表示输出为左对齐,例如"%-7d"表示输出7位整数左对齐

    一些特殊规定字符
        换行
        回车
        Tab符
    f    清屏并换页
    xhh    用十六进制表示一个ASCII码,其中hh是1到2个16进制数

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char c, s[20], *p;
        int a = 1234, b=12, *i;
        float f=3.141592653589f;
        double x = 0.12345678987654321;
        p = "How do you do"; 
        strcpy(s, "Hello, Comrade");
        i = &b;
        c = 'x41';
    
        printf("a=%d
    ", a);         /*结果输出十进制整数a=1234*/
        printf("a=%6d
    ", a);        /*结果输出6位十进制数a=  1234*/
        printf("a=%06d
    ", a);       /*结果输出6位十进制数a=001234*/
        printf("a=%2d
    ", a);        /*a超过2位, 按实际值输出a=1234*/
        printf("*i=%4d
    ", *i);      /*输出4位十进制整数*i=  12*/
        printf("*i=%-4d
    ", *i);     /*输出左对齐4位十进制整数*i=12*/
        printf("i=%p
    ", i);         /*输出地址i=06E4*/
        printf("f=%f
    ", f);         /*输出浮点数f=3.141593*/
        printf("f=6.4f
    ", f);       /*输出6位其中小数点后4位的浮点数f=3.1416*/
        printf("x=%lf
    ", x);        /*输出长浮点数x=0.123457*/
        printf("x=%18.16lf
    ", x);   /*输出18位其中小数点后16位的长浮点数x=0.1234567898765432*/
        printf("c=%c
    ", c);         /*输出字符c=A*/
        printf("c=%x
    ", c);         /*输出字符的ASCII码值c=41*/
        printf("s[]=%s
    ", s);       /*输出数组字符串s[]=Hello, Comrade*/
        printf("s[]=%6.9s
    ", s);    /*输出最多9个字符的字符串s[]=Hello, Co*/
        printf("s=%p
    ", s);         /*输出数组字符串首字符地址s=FFBE*/
        printf("*p=%s
    ", p);        /* 输出指针字符串p=How do you do*/
        printf("p=%p
    ", p);         /*输出指针的值p=0194*/
    
        getchar();
        return 0; 
    }
    /*
    int sprintf ( char * str, const char * format, ... );
    
    Write formatted data to string
    [将格式化数据写入到string中]
    Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str.
    [不同于printf()的将格式化数据输出到标准输出设备上,sprintf()是将格式化数据保存在一个C字符串缓冲中]
    The size of the buffer should be large enough to contain the entire resulting string (see snprintf for a safer version).
    [字符串缓冲的大小应该大到足以包含所有的结果字符串]
    A terminating null character is automatically appended after the content.
    [sprintf()函数会自动添加一个null作为字符串结束符]
    After the format parameter, the function expects at least as many additional arguments as needed for format.
    [sprintf()中参量表的个数需要与格式化规定符的个数一一对应,且数量相等]
    
    Return Value
    On success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string.
    [如果成功,则返回被写入的字符总数(不包含自动添加null结束符)]
    On failure, a negative number is returned.
    [如果失败,则返回一个负数]
    */
    
    #include <stdio.h>
    
    int main()
    {
        char buffer[50];
        int n, a=5, b=3;
        n = sprintf(buffer, "%d plus %d is %d", a, b, a+b);
        printf("[%s] is a string %d chars long.
    ", buffer, n);
    
        getchar();
        return 0;
    }
  • 相关阅读:
    zw版【转发·台湾nvp系列Delphi例程】HALCON CropPart
    zw版【转发·台湾nvp系列Delphi例程】HALCON ObjToInteger1-4
    zw版【转发·台湾nvp系列Delphi例程】HALCON TestObjDef
    zw版【转发·台湾nvp系列Delphi例程】HALCON DispCross
    ios-toolchain-based-on-clang-for-linux
    使用gdb调试theos tweak插件
    设置RabbitMQ远程ip登录
    ARC下带CF前缀的类型与OC类型转换
    laravel部署常用命令
    国产手机插入mac os 系统中无法被识别的解决方法
  • 原文地址:https://www.cnblogs.com/kevinq/p/4633731.html
Copyright © 2020-2023  润新知