zoukankan      html  css  js  c++  java
  • (C语言)char类型与int类型相加

     1 #include <stdio.h>
     2 
     3 int main(void) {
     4     char a = 127;
     5     int b = 4;
     6     int c = a + b;
     7     a += b;
     8     printf("c=%d",c);
     9     //printf("c=%s",c);
    10     printf("a=%d",a);
    11     //printf("a=%c",a);
    12     //printf("a=%s",a);
    13     // your code goes here
    14     return 0;
    15 }
    结果c=131 a=-125
    char类型是1 byte = 8 bits

    正数的原码,补码,反码都相同,都等于它本身 
    负数的补码是:符号位为1,其余各位求反,末位加1
    反码是:符号位为1,其余各位求反,但末位不加1
    在计算机中,数据是以补码的形式存储的
    补码 0111 1111 + 0000 0100
    = 1000 00011
    补码 = 1111 11101 = - (127-2)= -125
    int c = a + b;
    a 和 b直接转换为int类型相加!
        printf("c=%s",c);

      此句发生错误。

    类型之间计算时候转换的规则     
         double<--float
         ↑
         long
         ↑
         unsigned
         ↑
         int<--char、short
    ↑表示运算两边类型不一致时的转换顺序、<-表示即便两边类型一致依然要转换为箭头指向的数据类型再进行运算  
    整型提升

    整型提升是C程序设计语言中的一项规定:在表达式计算时,各种整形首先要提升为int类型,如果int类型不足以表示则要提升为unsigned int类型;然后执行表达式的运算。

    参考:http://blog.csdn.net/dyllove98/article/details/9004872

  • 相关阅读:
    uva1610 Party Games
    uva1442 Cav
    uva1609 Foul Play
    uva1608 Non-boring sequences
    uva12174 滑动窗口+预处理
    uva 1451 数形结合
    light oj 1336 sigma function
    找常用词(字符串处理)问题
    指定排序问题
    完数问题
  • 原文地址:https://www.cnblogs.com/tanshuai1001/p/4931941.html
Copyright © 2011-2022 走看看