zoukankan      html  css  js  c++  java
  • printf函数

    printf

    From: http://www.cplusplus.com/reference/cstdio/printf/

    int printf ( const char * format, ... );
    Print formatted data to stdout
    Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers(subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.

    Parameters

    format
    C string that contains the text to be written to stdout.
    It can optionally contain embedded format specifiers that are replaced by the values specified in subsequent additional arguments and formatted as requested.

    format specifier follows this prototype: [see compatibility note below] 
    %[flags][width][.precision][length]specifier 

    Where the specifier character at the end is the most significant component, since it defines the type and the interpretation of its corresponding argument:
    specifier Output Example
    d or i Signed decimal integer 392
    u Unsigned decimal integer 7235
    o Unsigned octal 610
    x Unsigned hexadecimal integer 7fa
    X Unsigned hexadecimal integer (uppercase) 7FA
    f Decimal floating point, lowercase 392.65
    F Decimal floating point, uppercase 392.65
    e Scientific notation (mantissa/exponent), lowercase 3.9265e+2
    E Scientific notation (mantissa/exponent), uppercase 3.9265E+2
    g Use the shortest representation: %e or %f 392.65
    G Use the shortest representation: %E or %F 392.65
    a Hexadecimal floating point, lowercase -0xc.90fep-2
    A Hexadecimal floating point, uppercase -0XC.90FEP-2
    c Character a
    s String of characters sample
    p Pointer address b8000000
    n Nothing printed.
    The corresponding argument must be a pointer to a signed int.
    The number of characters written so far is stored in the pointed location.

    % % followed by another % character will write a single % to the stream. %

    The format specifier can also contain sub-specifiers: flagswidth.precision and modifiers (in that order), which are optional and follow these specifications:

    flags description
    - Left-justify within the given field width; Right justification is the default (see width sub-specifier).
    + Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.
    (space) If no sign is going to be written, a blank space is inserted before the value.
    # Used with ox or X specifiers the value is preceeded with 00x or 0X respectively for values different than zero.
    Used with aAeEfFg or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.
    0 Left-pads the number with zeroes (0) instead of spaces when padding is specified (see width sub-specifier).

    width description
    (number) Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
    * The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

    .precision description
    .number For integer specifiers (diouxX): 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.
    For aAeEf and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).
    For g and G specifiers: This is the maximum number of significant digits to be printed.
    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.
    If the period is specified without an explicit value for precision0 is assumed.
    .* The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

    The length sub-specifier modifies the length of the data type. This is a chart showing the types used to interpret the corresponding arguments with and without length specifier (if a different type is used, the proper type promotion or conversion is performed, if allowed):

    specifiers
    length d i u o x X f F e E g G a A c s p n
    (none) int unsigned int double int char* void* int*
    hh signed char unsigned char



    signed char*
    h short int unsigned short int



    short int*
    l long int unsigned long int
    wint_t wchar_t*
    long int*
    ll long long int unsigned long long int



    long long int*
    j intmax_t uintmax_t



    intmax_t*
    z size_t size_t



    size_t*
    t ptrdiff_t ptrdiff_t



    ptrdiff_t*
    L

    long double



    Note that the c specifier takes an int (or wint_t) as argument, but performs the proper conversion to a charvalue (or a wchar_t) before formatting it for output.

    Note: Yellow rows indicate specifiers and sub-specifiers introduced by C99. See <cinttypes> for the specifiers for extended types.
    ... (additional arguments)
    Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
    There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.

    Return Value

    On success, the total number of characters written is returned.

    If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.

    If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned.

    Example

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    /* printf example */
    #include <stdio.h>
    
    int main()
    {
       printf ("Characters: %c %c 
    ", 'a', 65);
       printf ("Decimals: %d %ld
    ", 1977, 650000L);
       printf ("Preceding with blanks: %10d 
    ", 1977);
       printf ("Preceding with zeros: %010d 
    ", 1977);
       printf ("Some different radices: %d %x %o %#x %#o 
    ", 100, 100, 100, 100, 100);
       printf ("floats: %4.2f %+.0e %E 
    ", 3.1416, 3.1416, 3.1416);
       printf ("Width trick: %*d 
    ", 5, 10);
       printf ("%s 
    ", "A string");
       return 0;
    }


    Output:
    
    Characters: a A
    Decimals: 1977 650000
    Preceding with blanks:       1977
    Preceding with zeros: 0000001977
    Some different radices: 100 64 144 0x64 0144
    floats: 3.14 +3e+000 3.141600E+000
    Width trick:    10
    A string
    



    printf函数


    转载自:百度百科


    目录

    功能

    用法

    1.      type

    2.      flags

    3.      width

    4.      prec

    5.      F|N|h|l

    函数返回值

    程序举例

    1.      例一

    2.      例二

    3.      例三

    4.      例四

    命令

    1.      用途

    2.      语法

    3.      格式

    4.      描述

    5.      语法详细介绍

    6.      示例

    7.      文件

     

    功能

    c语言中产生格式化输出的函数(定义在 stdio.h 中)。向终端输出(显示器、控制台等)

    用法

    int printf(const char *format,[argument]);

    format 参数输出的格式,定义格式为:%[flags][width][.prec][F|N|h|l]type

    规定数据输出方式,具体如下:[1]

    type

    含义如下:

    字符

    输入数据类型

    含义

    d、i

    int

    有符号10进制数,i是老式写法

    o

    unsigned int

    无符号8进制数

    u

    unsigned int

    无符号10进制数

    x、X

    unsigned int

    无符号16进制数,x用abcdef,X用ABCDEF表示10~15的数

    f

    double

    小数

    e、E

    double

    科学计数法表示的数,大小写代表用的“e”的大小写

    g、G

    double

    使用以上两种中最短的形式,大小写的使用同%e和%E

    c

    char

    把输入的数字转换为对应的字符

    s、S

    char *、wchar_t *

    字符串

    p

    void *

    以16进制形式输出指针

    n

    int *

    到此字符之前为止,一共输出的字符个数,不输出文本

    %

    不输入

    输出字符“%”本身

    注:%g、%G在小数点位数四位或指数大于等于精度时用%e、%E,否则用%f。

    flags

    规定输出格式,取值和含义如下:

    字符

    名称

    说明

     

    空白

    右对齐,左边填充0和空格

    -

    减号

    左对齐,右边填充空格

    +

    加号

    在数字前增加符号 + 或 -

    0

    数字零

    将输出的前面补上0,直到占满指定列宽为止(不可以搭配使用“-”)

     

    空格

    输出值为正时加上空格,为负时加上负号

    #

    井号

    type是o、x、X时,增加前缀0、0x、0X

    type是e、E、f、g、G时,一定使用小数点

    type是g、G时,尾部的0保留

    width

    用于控制显示数值的宽度,取值和含义如下

    n(n=1,2,3...) 宽度至少为n位,不够以空格填充

    0n(n=1,2,3...) 宽度至少为n位,不够左边以0填充

    * 格式列表中,下一个参数还是width

    prec

    用于控制小数点后面的位数,取值和含义如下:

    无 按缺省精度显示

    0 当type=d,i,o,u,x时,没有影响

    type=e,E,f时,不显示小数点

    n(n=1,2,3...) 当type=e,E,f时表示的最大小数位数

    type=其他,表示显示的最大宽度

    .* 格式列表中,下一个参数还是width

    F|N|h|l

    表示指针是否是远指针或整数是否是长整数

    F远指针

    n 近指针

    h 短整数或单精度浮点数

    l 长整数或双精度浮点数

    函数返回值

    printf函数的一般形式为:

    int printf(const char *format,[argument]);

    以上形式,我们在Visual C++里输入“printf(”将会看到。

    说明printf函数类型为整型,其返回值是整型值。

    其值实际为printf控制输出的字符数。

    printf()函数实际上是将所有参数按字符输出,根据该函数的参数1(const char *format),我们不难理解。

    例如:

    int a,b;

    a=printf("gelin "); //a的值为6,

    b=printf("the value of printf is:%d",a); //b的值为24

    printf(" %d ",b);

    以上程序将会输出:

      

    程序举例

    例一

    #include <stdio.h>

    #defineC"gelin"

    int main(void)

    {

    int a=12345;

    float b=5.12345678;

    char e,d,f;

    scanf("%c%c%c",&e,&d,&f);

    //分别演示:%d、%4d、%.4d、%d%*d%d

    printf("int is:%d ",a);

    //分别演示:%d、%9d、%.9d、%8.4d、%-8.4d、%+8.4d

    printf("float is:%f ",b);

    //分别演示:%f、%8f、%.4f、%8.4f、%-8.4f

    printf("char is:%s ",C);

    //分别演示:%s、%8s、%.4s、%8.4s、%-8.4s

    return 0;

    }

    例二

    printf也可以这样用:

    printf("123 ""456 ""789 ");

    输出:

    123

    456

    789

    注意:

    printf("123 " "456 " "789 ");的输出结果与printf("123 ");printf("456 ");printf("789 ");相同。

    因此输出多行时,也并不需要每行调用一次printf。

    例三

    妙用printf判断闰年程序

    #include<stdio.h>

    int main(void)

    {

    int a;

    scanf("%d",&a);

    printf("%s",a%(a%100?4:400)?"NO":"YES");

    return 0;

    }

    例四

    #include <stdio.h>

    #include<string.h>

    int main()

    {

    char ch[20];

    int m,n;

    strcpy(ch,"Happy!");

    scanf("%d%d",&m,&n);

    printf("%*.*s ",m,n,ch);

    return 0;

    }

    其中前边*定义的是总的宽度,后边*是指定输出字符个数。分别对应外边参数m和n。

    输入: 10 3

    输出: Hap

    命令

    用途

    通过标准输出设备输出一组数据。

    语法

    printf Format [ Argument ... ]

    格式

    printf(格式控制,输出表列),格式控制由要输出的文字和数据格式说明组成。要输出的的文字除了可以使用字母、数字、空格和一些数字符号意外,还可以使用一些转义字符表示特殊的含义。

    例:printf("Hello!");

    printf("%f,%f,the max is %f ",a,b,c);

    描述

    printf 命令转换、格式化并写 Argument 参数到标准输出。Argument 参数是由 Format 参数控制格式化的。格式化输出行不能超出 LINE_MAX 字节长度。

    下列环境变量影响 printf 命令的执行:

    LANG 在 LC_ALL 和相应的环境变量(以 LC_ 开头)没有指定语言环境时,确定语言环境编目使用的语言环境。

    LC_ALL 确定用于覆盖由 LANG 或其它任何 LC_环境变量设置的任何语言环境编目值的语言环境。

    LC_CTYPE 确定把文本字节数据顺序解释为字符的语言环境;例如,单一字节对应多字节字符的参数。

    LC_MESSAGES 确定写消息使用的语言。

    LC_NUMERIC 确定数字格式编排的语言环境。此环境变量影响使用 e、E、f、g 和 G 转换字符编写的数字的格式。

    Format 参数是包含三种对象类型的一个字符串:

    * 无格式字符复制到输出流。

    * 转换规范,每个规范导致在值参数列表中检索 0 个或更多个项。

    * 以下转义序列。在复制到输出流时,这些序列导致它们的相关操作在有此功能的设备上显示:

    \反斜杠

    a 警告

     退格

    f 换页

    换行

    回车

    跳格

    v 垂直跳格

    ddd ddd 是 1、2 或 3 位八进制数字。这些转义序列作为由八进制数指定的具有数字值的字节显示。

    Argument 参数是一个或多个字符串的列表,它在Format 参数的控制下被写到标准输出。

    Format 参数在必要的情况下会经常重新使用以满足Argument 参数。将好像提供了空字符串Argument一样评估任何额外的 c 或者 s 转换规范;其它额外转换规范将好像提供了 0Argument 一样评估。此处 Format 参数不包含转换规范仅出现 Argument 参数,结果是不确定的。

    语法详细介绍

    每个 Format 参数中的转换规范都具有如下顺序的语法:

    1. % (百分号)。

    2. 零或更多的选项,修改转换规范的含义。选项字符和它们的含义是:

    - 转换结果在字段中左对齐。

    + 符号转换结果常以符号(+ 或者 -)开始。

    空格 如果符号转换的第一个字符不是符号,结果的前缀将是空格。如果空格和 + 选项字符都显示,则忽略空格选项字符。

    # 此选项指定值转换到备用格式。对于 c、d、i, u 和 s 转换,选项没有作用。对于 o 转换,它增加精度来强制结果的第一数字是 a、0(零)。对于 x 和 X 转换,非零结果分别具有 0x 或 0X 前缀。对于 e、E、 f、g 和 G 转换,结果通常包含基数字符,即使基数字符后没有数字。对于 g 和 G 转换,结尾零不象通常一样除去。

    0 对于 d、i、o、 u、x、e、 E、f、g 和 G 转换,前导零(跟在符号或底数的后面)用于填充字段宽度,将不用空格填充。如果显示 0(零)和 -(减号)选项,0(零)选项被忽略。对于 d、i、o、u、x 和 X 转换,如果指定精度,0(零)选项将被忽略。

    注:

    其它转换,没有定义其行为。

    3. 可选的指定最小值字段宽度的十进制数字字符串。如果转换值字符少于字段宽度,该字段将从左到右按指定的字段宽度填充。如果指定了左边调整选项,字段将在右边填充。如果转换结果宽于字段宽度,将扩展该字段以包含转换后的结果。不会发生截断。然而,小的精度可能导致在右边发生截断。

    4. 可选的精度。精度是一个 .(点)后跟十进制数字字符串。如果没有给出精度,按 0(零)对待。精度指定:

    * d、o、i、 u、x 或 X 转换的最少数字显示位数。

    * e 和 f 转换的基数字符后的最少数字显示位数。

    * g 转换的最大有效数字位数。

    * s 转换中字符串的最大打印字节数目。

    5. 指示要应用的转换类型的一个字符,例如:

    % 不进行转换。打印一个 %(百分号)。

    d, i 接受整数值并将它转换为有符号的十进制符号表示法。

    o 接受整数值并将它转换为有符号的八进制符号表示法。精度指定显示的最小数字位数。如果值转换后可以用更少的位数来表示,将使用前导零扩展。缺省精度是 1。精度为零的零值转换的结果是空字符串。用零作为前导字符来指定字段宽度,导致用前导零填充字段宽度值。不用八进制值表示字段宽度。

    u 接受整数值并将它转换为无符号的十进制符号表示法。

    x, X 接受整数值并将它转换为十六进制符号表示法。字母abcdef 用于 x 转换,字母 ABCDEF 用于 X 转换

    f 接受浮点或者双精度值并将它转换为十进制符号表示法,格式为[-] ddd.ddd。基数字符(在这里显示为十进制点)后的数字位数等于规定的精度。 LC_NUMERIC 语言环境编目确定在这个格式中使用的基数字符。如果不指定精度,则输出六个数字。如果精度是 0(零),将不显示基数字符

    e, E 接受浮点或者双精度值并将它转换为指数表示的形式[-] d.dde{+|-}dd。在基数字符前有一个数字(在这里显示为十进制点),基数字符后的数字位数等于规定的精度。 LC_NUMERIC 语言环境编目确定在这个格式中使用的基数字符。如果不指定精度,则输出六个数字。如果精度是 0(零),将不显示基数字符。E 转换字符在指数前生成带 E 而不是带 e 的数字。指数通常至少包含两个数字。然而,如果要打印的指数值大于两个数字,必要时需要打印附加指数数字。

    g、G 接受浮点和双精度值并转换为 f 或 e 转换字符的样式(或在 G 转换的情况下是 E),用精度指定有效数字的个数。尾零将从结果中除去。基数字符只有在其后是数字时显示。使用的样式取决于转换的值。样式 g 仅在转换的指数结果小于 -4,或大于或等于精度时使用。

    c 接受值将其作为字符串并打印字符串中的第一个字符。

    s 接受值将其作为字符串并打印字符串中的字符直到字符串结束或者达到精度指示的字符个数。如果没有指定精度,打印全部字符直到出现第一个空字符

    b 接受值将其作为字符串,可能包含反斜杠转义序列。打印来自转换字符串的字节直到字符串结束或者达到精度规范指示的字节数。如果没有指定精度,打印全部字节直到出现第一个空字符。

    支持下列反斜杠转义序列:

    * 先前列出的反斜杠转义序列在 Format 参数描述下。这些转义序列将被转换到它们表示的单个字符

    * c(反斜杠c)序列,它不显示并使 printf 命令忽略 Format 参数中的字符串参数包含的剩余的所有字符串,所有剩余的字符串参数和所有附加字符。

    退出状态

    该命令返回以下出口值:

    0 成功完成。

    >0 发生错误。

    示例

    1. 输入下列命令:

    printf ("%5d%4d ",1213,43);

    产生下列输出:

    _1213_ _43

    三次使用 Format 参数打印所有给定字符串。0(零)由 printf 命令提供以满足最后的 %4d 转换规格。

    2. 输入下列命令

    printf ("%c %c ",78,79);

    产生下列输出:

    N_O

    文件

    /usr/bin/printf 包含printf 命令。

  • 相关阅读:
    只会写“Hello World”的菜鸟心声
    新的旅程
    生成[1,2,,3,4,5,6,7,8,9]的随机数组?
    计算java集合中自定义对象出现的次数
    iOS https 证书信任漏洞解决办法
    自定义 URL Scheme 完全指南
    Apple移动设备处理器指令集 armv6、armv7、armv7s及arm64
    url schemes格式错误
    给UITapGestureRecognizer添加tag
    iOS GIF 格式动画 图片显示
  • 原文地址:https://www.cnblogs.com/techstone/p/3321265.html
Copyright © 2011-2022 走看看