如何解决与C语言相关的问题? [英] How to solve this problem Related to C language?
本文介绍了如何解决与C语言相关的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int num=20;
printf("\t%d",&num);
getch();
}
在TC 16位编译器上运行程序时,输出为-12.
我不知道变量num的地址怎么可能是-12,即负数.
等待答案....
问候
here the output which I get is -12 when I run the program on TC 16bit compiler.
I wan''t to know how come the address of a variable num can be -12,a negative number.
waiting for the answer....
regards
推荐答案
内存地址不是带符号的整数.符号取决于最高位.
对于16 bit
,0x8000
及更高版本地址将为负,并且对于32 bit
上的0x80000000
同样.
希望对您来说清楚.
Memory addresses are not signed integers. The sign depends on the highest bit.
For16 bit
,0x8000
and above addresses would be negative and same is for0x80000000
on32 bit
.
I hope it is clear for you.
尝试将其打印为unsigned
整数
Try printing it as anunsigned
integer
printf("\t%u",(unsigned int)&num);
因为应该正确使用格式说明符(printf
根据格式说明符解释其参数).
由于&num
是内存地址,因此应使用%p
,即:
Because format specifiers should be used wisely (printf
interprets its arguments based on format specifiers).
Since&num
is a memory address, you should use%p
, namely:
printf("\t%p",&num);
这篇关于如何解决与C语言相关的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文