zoukankan      html  css  js  c++  java
  • c语言之数据类型

    #include<stdio.h>
    
    int main(void)
    {
    	float weight, value;
    	printf("Are you worth your weight in rhodium?
    ");
    	printf("Let's check it out.
    ");
    	printf("Please enter your weight in pounds:");
    	scanf_s("%f", &weight);
    	value = 770 * weight * 14.5833;
    	printf("Your weight in rhodium is worth $%.2f.
    ", value);
    	printf("You are easily worth that!If rhodium prices drop,
    ");
    	printf("eat more to maintain your value.
    ");
    	return 0;
    }
    

    int类型

    int是符号的整数,其取值范围依赖于计算机系统

    int num_1;
    int num_1,num_2;
    
    printf("number:%d",number)//"%d"是要求打印整数,即decimal
    
    八进制和十六进制
    #include<stdio.h>
    
    int main(void)
    {
    	int x = 100;
    	printf("dec = %d;octal = %o:hex = %x
    ", x, x, x); //"%o"和"%x"分别显示八进制和十六进制
    	printf("dec = %d;octal = %#o;hex = %#x
    ", x, x, x);//在前面的基础上显示符号
    	return 0;
    }
    
    其他int数据类型
    short int类型

    可能占用比int类型更少的储存空间,用于仅需要小数值的场合以节省空间。

    long int类型

    可能占用比int类型更少的储存空间,用于使用大数值的场合。

    long long int类型

    可能占用比long类型更多的储存空间,用于使用更大数值的场合。

    unsigned int类型

    无符号的int类型。

    还有unsigned long intunsigned long long int等等。

    数值溢出
    #include<stdio.h>
    
    int main(void)
    {
    	int num_1 = 2147483647;
    	unsigned int num_2 = 2147483647 * 2;
    	printf("num_1:%d %d %d",num_1,num_1+1, num_1+2);
    	printf("num_2:%d %d %d", num_2, num_2 + 1, num_2 + 2);
    }
    
    /*
    num_1:2147483647 -2147483648 -2147483647
    num_2:-1 0 1
    */
    //int num_1和unsigned int num_2达到最大值都会溢出,但是int的起始点是-2147483648,而unsigned int的起始点是0
    
    long常量与long long常量

    如果希望把一个较小的常量作为long类型对待,可以使用“l”或“L”后缀,同理·还有“ll”(“LL”)、“ull”(“ULL”)等

    打印shortlonglong longunsigned类型数
    #include<stdio.h>
    
    int main(void)
    {
    	unsigned int un = 10000;
    	short end = 200;
    	long big = 6553710241024;
    	long long verybig = 123456789012254;
    	printf("un = %u and not %d
    ", un, un);  //使用"%u"打印
    	printf("end = %hd and %d
    ", end, end);  //使用"%hd"打印
        /*"%hd"和"%d"打印的是也一样的,因为int被认为是最有效的整数类型*/
    	printf("big = %ld and not %d
    ", big, big); //使用"%ld"打印
    	printf("verybig = %lld and not %d
    ", verybig, verybig); //使用"%lld"打印
    	return 0;
    }
    

    char类型

    char类型用于储存字母和标点符号之类的字符,但是在技术上char却是整数类型,这是因为char类型实际储存的是整数而不是字符。

    其中,单引号中的是字符常量;不使用单引号的是一个变量名;使用双引号的是一个字符串。

    转义字符 意义 ASCII码值(十进制)
    a 响铃 007
     退一格 008
    f 换页(在cmd里是垃圾字符) 012
    换行 010
    水平制表符 009
    回车 013
    v 垂直制表符 011
    \ 代表一个反斜字符“” 092
    ' 代表一个单引号 039
    " 代表一个双引号 034
    空字符(null) 000
    ddd 一个八进制任意字符 三位八进制
    xhh 一个十六进制任意字符 三位十六进制

    我们也可以使用“”加上ASCII码来代表相关字符。

    打印字符
    #include<stdio.h>
    
    int main(void)
    {
    	char ch;
    	printf("Please enter a character.
    ");
    	scanf_s("%c", &ch);           //使用"%c"说明符来打印字符
    	printf("The code for %c is %d.
    ", ch, ch);
    	return 0;
    }
    

    _Bool类型

    用于表示布尔值,即逻辑真(True)和逻辑假(False)

    floatdoublelong double类型

    C标准规定,float类型必须至少能表示6位有效数字,取值范围至少为e-37到e+37;而double(双精确)的浮点类型和float类型具有相同的最小取值范围要求,但是它至少能表示10位有效数字。

    可以省略正号。可以没有小数点(2E5)或指数部分(19.28),但是不能同时没有二者;可以省略纯小数部分(3.E16)或整数部分(.45E-6),但是二者不能同时省略。下面为一些有效的浮点常量:

    3.12159
    .2
    4e16
    .8E-5
    100.
    

    在默认情况下,编译器将浮点常量当做double类型,比如乘积运算使用双精度,结果被截为正常的float长度,这样会确保数字精度而减慢程序的执行。也可以通过添加f或F后缀使编译器把浮点常量当作float类型,比如2.3f和9.11E9F。1或L后缀使一个数字成为long double类型,比如54.31和4.32e4L。

    打印浮点数

    使用“%f”说明符打印十进制记数法的float和double数字,用“%e”打印指数记数法的数字

    #include<stdio.h>
    
    int main(void)
    {
    	float aboat = 32000.0;
    	double abet = 2.14e9;
    	long double dip = 5.32e-5;
    	printf("%f can be written %e
    ", aboat, aboat);
    	printf("%f can be written %e
    ", abet, abet);
    	printf("%f can be written %e
    ", dip, dip);
    	return 0;
    }
    
    浮点值的上溢和下溢

    当计算结果是一个大得不能表示的数时,会发生上溢,且用printf()函数显示此值为inf或infinity。

    当除以一个非常小的数时,通常将是指数部分减小,但是指数已经达到了最小值;所以计算机只好将尾数部分的位进行右移,空出首位二进制,并丢弃最后一位二进制,这就是下溢。

    sizeof()函数

    返回数据类型所占的字节大小

    #include<stdio.h>
    
    int main(void)
    {
    	printf("Type int has a size of %u bytes.
    ", sizeof(int));
    	printf("Type char has a size of %u bytes.
    ", sizeof(char));
    	printf("Type long has a size of %u bytes.
    ", sizeof(long));
    	printf("Type double has a size of %u bytes.
    ", sizeof(double));  
    	return 0;
    }
    
  • 相关阅读:
    Vs2010 编辑器自动提示crash的解决办法
    史丹佛毕业25年 我最有钱的同学是...zz
    Workerman3 创建wss服务 利用apache代理wss
    vue面试装X知识汇总
    接:this is incompatible with sql_mode=only_full_group_by
    vue2 nuxtjs 使用与安装
    linux 升级安装最新版本的composer2.0
    Ubuntu20.04LTS 添加环境变量
    this is incompatible with sql_mode=only_full_group_by
    CC2420与IEEE802.15.4关系
  • 原文地址:https://www.cnblogs.com/MingleYuan/p/10628553.html
Copyright © 2011-2022 走看看