论学院网站建设项目的进度管理做网站那个程序好
学习了C语言的基本数据类型后,我们可能会想这些数据如何进行运算,是否可以让不同类型的数据直接进行运算呢?
一、数据类型转换
1.1 int类型与float类型之间的转换
    int i = 5;
//    j值为2.000000  因为左右操作数均为整型float j = i/2;
//    l值为2.500000  float l = (float) i/2;printf("%f\n",j);printf("%f\n",l);
 
上述的 j 值为2.000000,是因为,i和2,均为整型,当整型进行除法运算时,默认进行整除运算。
 要想得到正确的结果,我们需要先进行类型转换。就像上述的l中,(float)i,就将int类型的i转换成了float类型的数据,再与2进行除法运算。
 指的一提的是,int类型与float类型可以直接进行运算,这是因为默认进行了隐式转换,在计算前就把n转成了float型。
	int i = 1;float f = 5.4;
//  输出的结果是6.400000printf("%f",i+f);
 
1.2 int类型与char类型之间的转换
int类型与char类型之间的转换,大致服从以下规则。
 
	int i = 65;char c = (char) i;
//  结果输出为A printf("%c",c);
 
1.3 进制转换
printf中的
 %d 表示十进制
 %o 表示八进制
 %x 表示十六进制
    int s = 123;
//    十进制printf("%d\n",s);
//    八进制printf("%o\n",s);
//    十六进制printf("%x\n",s);
 
二、数据的输出
输出的格式:printf("%",);
 %c 字符
 %d (有符号)整型
 %f 浮点型
 %s 字符串
   int age = 20;
//  输出结果为:His name is Mianded, and he is 20 years oldprintf("His name is %s, and he is %d years old\n","Mianded",age);
 
在上面所提到的数据类型转换时,输出浮点型数据时,总是默认输出6位小数,现在我们可以通过更改printf的相关属性,对输出小数进行控制。
 printf输出值占位,可以在%后加数字实现,如:%4d,即输出数字在四位以内,均占四位,默认对齐方式为右对齐,如果左对齐,需要在%+‘-’。
 控制浮点型小数位数,可以在%后+.+数字,如%.2d ,即输出的数字小数点保留两位,符合四舍五入的规则。
   int p = 90;int q = 1000;
//    the int-number is   90 and .printf("the int-number is %4d and .\n",p);
//    the int-number is 1000 and .printf("the int-number is %4d and .\n",q);float a = 99.994;float b = 3.6;
//    the float-number is 99.99 and .printf("the float-number is %.2f and .\n",a);
//    the float-number is 3.60 and .printf("the float-number is %.2f and .\n",b);
 
三、数据的输入
输入的格式为:
int i;
scanf("%d",&i);
 
如果是其他类型的数据,可以参考printf进行更改,将%d,更改为%c …
 需要注意的是,i前面有个&,表示的是,获取的是i的地址值。类比printf,同样是整数参数, 为什么printf只需要传入参数本身,而scanf需要取地址?
 这是因为,printf只需要获得参数值,而scanf则是需要对值进行更改。
 scanf, 传入的参数是值传递, 没办法修改到原变量,所以必须要取地址.
	printf("please input a number:\n");scanf("%d",&i);printf("please input another number:\n");scanf("%d",&j);printf("i=%d\n",i);printf("j=%d\n",j);
 
特别注意:行缓冲:在输入和输出中遇到换行符时,将执行I/O操作。这时,我们输入的字符会先存放到缓冲区中,摁下回车键后执行I/O操作。
 scanf由“回车”触发,当我们输入数据后,输入’\n’后,scanf进行匹配,匹配成功后,会把相关数据放入指定变量中。scanf函数匹配到一个字符后,会在缓冲区删除对应的字符。
