如果你传递一个int在C字节参数会发生什么? [英] What happens if you pass an int to a byte parameter in C?
本文介绍了如果你传递一个int在C字节参数会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何C / C ++的协议,如果你传递一个int作为参数成发生在一个字节(一个字符)的方法?是否INT被截断?还是其他什么东西?
How does C/C++ deal if you pass an int as a parameter into a method that takes in a byte (a char)? Does the int get truncated? Or something else?
例如:
void method1()
{
int i = //some int;
method2(i);
}
void method2(byte b)
{
//Do something
}
如何整型得到投到一个字节(一个字符)?它是否会被截断?
How does the int get "cast" to a byte (a char)? Does it get truncated?
推荐答案
刚截断,位模式(字节一般无符号的字符,但是,你必须检查)
Just truncated AS bit pattern (byte is in general unsigned char, however, you have to check)
INT I = -1;
int i = -1;
变为
字节B = 255;当字节= unsigned char型
byte b = 255; when byte = unsigned char
字节B = -1;当字节=符号字符
byte b = -1; when byte = signed char
I = 0; B = 0;
i = 0; b = 0;
我= 1024; B = 0;
i = 1024; b = 0;
我= 1040; B = 16;
i = 1040; b = 16;
这篇关于如果你传递一个int在C字节参数会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文