在的printf(QUOT;%D&QUOT,5);被存储在存储器5? [英] In printf ( "%d", 5 ); is 5 stored in the memory?

查看:157
本文介绍了在的printf(QUOT;%D&QUOT,5);被存储在存储器5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
char* func ();
int main ( )
{
   char *p;
   p=func();
   printf("%c",*p);
}
char* func()
{

    return "hello";
}

下面,printf的打印h的,这意味着以你好,创建一个指针和该字符串你好被存储在存储器中。
同样地,在输出(%d个,5),是5存储在存储器

Here, printf prints "h", which means that a pointer to "hello" is created and that the string "hello" is stored in memory. Similarly, in printf ( "%d", 5 ), is '5' stored in the memory?

推荐答案

您的问题是非常依赖于平台,但大多数理智的编译器不应该存储 5 中的任何数据段您code,只有或阅读 - 它应该被简单地推的printf 调用之前堆栈上的

Your question is extremely platform dependent, but most sane compilers shouldn't store 5 in any data section of your code, read only or otherwise -- it should be simply pushed on the stack before the printf call.

编辑:我其实只是检查,GCC做更有趣的东西:

I actually just checked, and GCC does something even more interesting:

这完全避免了64位机上的堆栈,并使用可用的,因为众多的直寄存器。它仍然不存储 5 任何地方,但在OP code本身。

It avoids the stack entirely on a 64-bit machine and uses straight registers because of the multitude available. It still doesn't store the 5 anywhere but in the opcode itself.

这篇关于在的printf(QUOT;%D&QUOT,5);被存储在存储器5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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