这将是什么输出?请也解释一下 [英] What will be the output of this? Please explain it also

查看:75
本文介绍了这将是什么输出?请也解释一下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int main(){
int a;
printf("%u\n ",&a); 
printf("%p\n ",a); 
printf("%p\n ",&a); 
printf("%fp\n ",&a); 
return 0;
}

我已经尝试过这段代码,但是我无法理解输出内容

I have tried this code but I am not able to understand the output

4193177020
 (nil)
 0x7ffff9eecdbc
 0.000000p

这是什么存储的内存地址,哪一部分是偏移量?

What is semgmented memory address in this and what part is offset?

推荐答案

显然,您的a变量是用0初始化的.

Apparently your a variable was initialized with 0.

  • %u显示一个无符号整数,并且您将内存地址作为参数传递
  • %p通常显示指针的值,因此如果为0,则将其显示为(nil)
  • %p的作用相同,但现在您传递的地址a(以十六进制显示).
  • %fp%f(浮动格式)和文字p.我很确定这会导致未定义的行为,因为printf期望一个浮点数,并且您传递一个整数(指针是long/integer值).
  • %u displays an unsigned integer, and you pass the memory address as an argument
  • %p usually displays the value of a pointer, so in case of 0 it shows it as (nil)
  • %p does the same, but you now pass the address of a, which is displayed in hex.
  • %fp is %f (float formatting) and a literal p. I'm pretty sure this one causes undefined behavior since printf expects a float and you pass an integer (pointers are long/integer values).

我们从中学到什么? 不要编写废话代码,并且不要将参数传递给printf样式的函数,除非您有一个格式字符串完全需要这些参数.

What do we learn from it? Don't write nonsense code and don't pass arguments to printf-style functions unless you have a format string that expects exactly those arguments.

这篇关于这将是什么输出?请也解释一下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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