宏替换printf_printf打印指定长度字符串

宏替换printf、printf打印指定长度字符串

  1. 宏替换printf可以有如下形式:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #include <stdio.h>

    #define FUN1(msg) printf(#msg"\n")
    #define FUN2(param1,param2) printf("the %d\n",param1##param2)
    #define FUN3(...) printf(__VA_ARGS__)
    #define FUN4(format ,...) printf(format,##__VA_ARGS__)

    int main()
    {
    printf("hello \n");
    FUN1(hello\n);
    FUN2(11, 22);
    FUN3("testtttt %d ,%f , %c\n", 10, 100.23, 'a');
    FUN4("the %d \n",100);
    return 0;
    }
运行结果
1
2
3
4
5
6
hello
hello

the 1122
testtttt 10 ,100.230000 , a
the 100

  1. 原样输出字符串:
    printf(“%s”, str);

  2. 输出指定长度的字符串, 超长时不截断, 不足时右对齐:
    printf(“%Ns”, str); –N 为指定长度的10进制数值

  3. 输出指定长度的字符串, 超长时不截断, 不足时左对齐:
    printf(“%-Ns”, str); –N 为指定长度的10进制数值

  4. 输出指定长度的字符串, 超长时截断, 不足时右对齐:
    printf(“%N.Ms”, str); –N 为最终的字符串输出长度
    –M 为从参数字符串中取出的子串长度

  5. 输出指定长度的字符串, 超长时截断, 不足时左对齐:
    printf(“%-N.Ms”, str); –N 为最终的字符串输出长度
    –M 为从参数字符串中取出的子串长度

用到的地方 时间显示到屏幕的时候,需要用到这些指定长度等。