数据类型

1.数据类型

整形:

    char: 字符以字符编码(数字)形式储存

    int:一般能存几万以内的数字

    short:一般能存几百几千的数字(1/2 int)

     long:一般能存几十亿以内的数字(2 int)

浮点数:

    float

    double(2 float)

符号:

    signed:有符号(所有数据默认有符号)

    unsigned:无符号(非负数,可以保存更大的数据,是int的两倍)

2.能用int存short,不能用short存int

例:  int x = 100000;

    short y = x;

  结果:y = -31072

  原因  x <- 0001 1000 0110 1010 0000

      计算机把这个数字存到short时,发现只能存2个字节,所以只保留了数字的右半边

      y <- 1000 0110 1010 0000

      y = -31072

3.两个整型数据相除结果是一个舍入的整数  7/2 = 3

4.数据类型临时转换 (int)x / (float)y

5.计算机位数(8位、32位)的含义

  CPU指令长度

  CPU一次从储存器中读取数据的大小

  计算机能够处理的数据的长度

练习:(数据类型)

 1 #include<stdio.h>
 2 
 3 float total = 0.0;
 4 short count = 0;
 5 short tax_percent = 6;
 6 float add_with_tax(float f)
 7 {
 8     float tax_rate = 1 + tax_percent / 100.0;//100.0使计算结果返回浮点型数据
 9     total = total + (f * tax_rate);
10     count = count + 1;
11     return total;
12 }
13 
14 int main()
15 {
16     float val;
17     printf("Price of item:");
18     while (scanf("%f",&val) == 1)
19     {
20         printf("Total so far:%.2f
",add_with_tax(val));//.2f把浮点数格式化为小数点后两位
21         printf("Price of item:");
22     }
23     printf("
Final total:%.2f
",total);
24     printf("Number of items:%hi
",count);//hi格式化short
25     return 0;
26 }
原文地址:https://www.cnblogs.com/syyy/p/5696861.html