没有参数解释的 printf [英] Printf with no arguments explanation

查看:51
本文介绍了没有参数解释的 printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果没有给 printf 提供参数,它会输出一个意想不到的值.

I understand that if printf is given no arguments it outputs an unexpected value.

示例:

#include <stdio.h>

int main() {
    int test = 4 * 4

    printf("The answer is: %d\n");
    return 0;
}

这将返回一个随机数.在使用了 %p、%x 等不同格式后,它不会打印 16(因为我没有将变量添加到参数部分)我想知道的是,这些值在哪里被采用从?它是栈顶吗?每次编译都不是新值,很奇怪,好像是固定值.

This returns a random number. After playing around with different formats such as %p, %x etc, it doesn't print 16(because I didn't add the variable to the argument section) What i'd like to know is, where are these values being taken from? Is it the top of the stack? It's not a new value every time I compile, which is weird, it's like a fixed value.

推荐答案

printf("The answer is: %d\n");

调用未定义的行为.C 要求转换说明符具有关联的参数.虽然这是未定义的行为并且任何事情都可能发生,但在大多数系统上,您最终会转储堆栈.这是格式字符串攻击中使用的那种技巧.

invokes undefined behavior. C requires a conversion specifier to have an associated argument. While it is undefined behavior and anything can happen, on most systems you end up dumping the stack. It's the kind of trick used in format string attacks.

这篇关于没有参数解释的 printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆