本文介绍了做`mod'时有符号和无符号的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我不明白为什么会发生这种情况?

代码1将输出`fff9''

和代码2将输出'1''

'mod 8'怎么没有效果?


/ *代码1 * /

#include< stdio.h>

#include< stdlib.h>


int main(int argc ,char * argv [])

{

unsigned short a,b,c;

a = 0;

b = 7;


c =(ab)%8;


printf("%x \ n",c);

返回0;

}


/ *代码2 * /

#include< stdio .h>

#include< stdlib.h>


int main(int argc,char * argv [])

{

unsigned short a,b,c;

a = 0;

b = 7;


c =(ab)%8U;


printf("%x \ n",c);

返回0;

}

解决方案



-

如果你撒谎到编译器,它将报复。 - Eric Sosman



stdlib.h是EXIT_SUCCESS的源代码,你可以将它作为返回代码而不是使用幻数0。




但是0是所有数字中最不神奇的,并且标准保证

返回0; "在main()中返回一个表示成功的状态。


-

Keith Thompson(The_Other_Keith)< http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。





当处理无符号整数类型的促销到更高的

排名整数类型时,这些促销有时会导致更高类型的签名

值。


-

Jack Klein

主页:

常见问题解答

comp.lang.c

comp.lang.c ++

alt.comp.lang.learn.c-c ++


Hi,

I don''t understand why this could happen?
The Code 1 will output `fff9''
and the Code 2 will output `1''
How could the `mod 8'' not have effect?

/* Code 1 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
unsigned short a, b, c;
a = 0;
b = 7;

c = (a-b)%8;

printf("%x\n", c);
return 0;
}

/* Code 2 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
unsigned short a, b, c;
a = 0;
b = 7;

c = (a-b)%8U;

printf("%x\n", c);
return 0;
}

解决方案


--
If you lie to the compiler, it will get its revenge. -- Eric Sosman




Note: you do not actually use stdlib.h in any of the code you show.
stdlib.h is, though, the source of EXIT_SUCCESS which you could be
using as your return code instead of using the magic number 0.



But 0 is the least magical of all numbers, and the standard guarantees
that "return 0;" in main() returns a status indicating success.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.




When dealing with promotions of unsigned integer types to higher
ranking integer types, these promotions sometimes result in signed
values of the higher types.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于做`mod'时有符号和无符号的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 15:46