C语言格式化字符串 %n 参数效果

代码片段1

#include <stdio.h>
int main()
{
    unsigned int x = 100;
    unsigned int y = 1;
    int *p = &x;
    printf("x=%d\n", x);
    //printf("lalalala%n\n", p);
    printf("%.66u%n\n", y, p);
    printf("x=%d\n", x);
    return 0;
}

代码片段2

#include <stdio.h>
int main()
{
    unsigned int x = 0;
    unsigned int y = 0;
    int *p = &y;
    printf("&x=%.8p, &y=%.8p\n", &x, &y); // y地址比x地址大4,先定义的整型参数先入栈,地址更高
    // printf("%.8p %.8p", p, (unsigned int*)p + 1); //(int)p+4);
    printf("x=%d, y=%d\n", x, y);
    printf("000%n000000%n\n", p, (unsigned int*)p + 1);
    printf("x=%d, y=%d\n", x, y);
    return 0;
}


这是64位Ubuntu系统,对32位系统,代码中(unsigned int)应该改成(unsigned long)。因为64位系统unsigned long长度为8字节。

posted @ 2021-01-17 23:00  爱吃砂糖橘的白龙  阅读(1757)  评论(0编辑  收藏  举报