c中的数据类型

来源:互联网 发布:四糸乃cos装淘宝 编辑:程序博客网 时间:2024/06/05 05:58
一.示例
例一.
#include<stdio.h>
int main(void)
{
    float weight;
    float value;
    printf("Are you worth your weight in rhodium?\n");
    printf("Let is check it out.\n");
    printf("Please enter your weight in pounds:");
    scanf("%f",&weight);
    value=770*weight*14.5833;
    printf("You weight in rhodium is worth $%.2f.\n",value);
    printf("You are easily worth that! If rhodium prices drop,\n");
    printf("eat more to maintain your value.\n");
    return 0;
}


包含的新元素:1.新的变量申明
              2.常量的新的写法
     3.scanf()提供键盘的输入
         4.交互性
二.变量与常量
变量与常量的区别在于,变量的值可以在程序执行过程中变化与指定,而常量缺不可以。
三.数据类型和关键字
关键字:int long short unsigned char float double signed void _Bool _Complex _lmaginary
数据类型有整数和浮点数。
四.数据类型
有符号整数:这种类型可以取正负值
int:系统的基本整数类型。至少有16位字长。
short或者short int:最大的short整数不大于最大的int整数值。c保证short类型至少有16位长。
long或者long int:这种类型的整数不小于最大的long的整数值。long long类型至少是64位长。
无符号整数:无符号整数只有0和正值,使得无符号数比有符号数可以表示更大的正整数。例如unsigned int,unsigned long,unsigned short.
例二.
#include<stdio.h>
int main(void)
{
    int ten=10;
    int two=2;
    printf("Doing it right:");
    printf("%d minus %d is %d\n",ten,2,ten-two);
    printf("Doing it wrong:");
    printf("%d minus %d is %d\n",ten);
    return 0;
}
ps:用%d对应int
例三.
#include<stdio.h>
int main(void)
{
    int x=100;
    printf("dec=%d;octal=%o;hex=%x\n",x,x,x);
    printf("dec=%d;octal=%#o;hex=%#x\n",x,x,x);
    return 0;
}
ps:用%o表示八进制,用%x表示十六进制
例四.
#include<stdio.h>
int main(void)
{
    unsigned int un=3000000000;
    short end=200;
    long big =65537;
    long long verybig=12345678908642;
    printf("un=%u and not %d\n",un,un);
    printf("end=%hd and %d\n",end,end);
    printf("big=%ld and not %hd\n",big,big);
    printf("verybig=%lld and not %ld\n",verybig,verybig);
    return 0;
}
ps:用%u表示unsigned int,用%ld表示long(%lo表示八进制长整形,%lx表示十六进制长整形),%hd表示short int,%lu表示unsigned long


字符:
字符包括印刷字符,如A,&和+。在定义中,char类型使用1个字节的存储空间表示一个字符。字节通常为8位,但也可以是16位。
例5.
#include<stdio.h>
int main(void)
{
    char ch;
    printf("Please enter a character.\n");
    scanf("%c",&ch);
    printf("The code for %c is %d.\n",ch,ch);
    return 0;
}
布尔值:
布尔值表示true和false;C使用1代表true,0代表false.
_Bool:此类型的关键字。布尔值是一个无符号整数,其储存只需要能够表示1或者0的空间。


实浮点数:
实浮点数可以有正值或负值
float:系统的基本浮点类型。至少能精确表示6位有效数字。
例7.
#include<stdio.h>
int main(void)
{
    float aboat=32000.0;
    double abet=2.14e9;
    double dip=5.32e-5;
    printf("%f can be written %e\n",aboat,aboat);
    printf("%f can be written %e\n",abet,abet);
    printf("%f can be written %e\n",dip,dip);
    return 0;
}
ps:用%f表示float或者double,用%e表示指数记数法
 
复数和虚浮点数:
float _Complex
double _Complex
long double _Complex
float _Imaginary
double _Imaginary
long double _Imaginary
 
例八.
#include<stdio.h>
int main(void)
{
    printf("Type int has a size of %u bytes.\n",sizeof(int));
    printf("Type char has a size of %u bytes.\n",sizeof(char));
    printf("Type long has a size of %u bytes.\n",sizeof(long));  
    printf("Type double has a size of %u bytes.\n",sizeof(double));
    return 0;
}
char:8 int:8 short:16 long:32 long long:64 double:15 float;6 long double:18 (后面默认位数) 
五.使用数据类型
开发程序时,应当注意所需变量及其类型的选择。初始化变量使用的常量应当同变量类型相匹配。应该养成系统化的变量命名规则,变量的名字可以表示它的类型。例如用i_前缀表示int,us_表示unsigned short等。
六.参数和易犯错误
#include<stdio.h>
int main(void)
{
    int f=4;
    int g=5;
    float h=5.0f;
    printf("%d\n",f,g);
    printf("%d %d\n",f);
    printf("%d %f\n",h,g);
    return 0;
}


我们应该比配printf()语句的参数类型和数目。若参数类型和数目不符合,编译器是无法检测出来的,我们也很难察觉这种错误,会导致程序的错误一直存在。
七.转义序列
#include<stdio.h>
int main(void)
{
    float salary;
    printf("\aEnter your desired monthly salary:");
    printf(" $__\b\b\b\b\b\b\b");
    scanf("%f",&salary);
    printf("\n\t$%.2f a month is $%.2f a year.",salary,salary*12.0);
    printf("\rGee!\n");
    return 0;


}


printf()函数的运行过程:首先printf()语句把输出传递给一个缓冲区,然后缓冲区的内容在不断传递给屏幕。
缓冲区传给屏幕的几种情况:1.缓冲区慢的时候
                          2.遇到换行符的时候
 3.需要输入的时候
0 0
原创粉丝点击