int和焦炭的实际差别 [英] Practical difference between int and char

查看:98
本文介绍了int和焦炭的实际差别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来分析这些code片段的输出:

I have to analyse the output of these code fragments:

int x, y;
x = 200; y = 100;
x = x+y; y = x-y; x = x-y;
printf ("%d %d\n", x, y);

char x, y;
x = 200; y = 100;
x = x+y; y = x-y; x = x-y;
printf ("%d %d\n", x, y);

所以,我现在知道 INT 为整数,字符字符;我读过有关的区别,如果我把在的printf %d个,它的形式返回数字和%C ,在字符的形式。

So, I know now that int stands for integer and char for character; I've read about the differences and if I put in the printf the %d, it returns in the form of digits, and %c, in the form of a character.

ASCII字符code为'A' 65为例,但为什么第二个函数打印 100 -56 , 100 200

The ASCII character code for 'A' is 65 for example, but why does the second function print 100 -56, instead of 100 200?

推荐答案

在问题中所使用的平台,键入字符好像是1字节(8位)大小是有符号类型与1符号位和7位值(使用2的补码运算)。它存储从-128到127值,因此,这是发生了什么 X

On the platform used in the question, the type char seems to be 1 byte (8 bits) size and is a signed type with 1 sign bit and 7 value bits (and using 2's complement arithmetic). It stores values from -128 to 127. So, this is what's happening to x and y:

x = 200 => x takes value -56
y = 100 => y takes value 100
x = x+y => x takes value 44
y = x-y => y takes value -56
x = x-y => x takes value 100

这篇关于int和焦炭的实际差别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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