数据与C

//以十进制、八进制和十六进制输出100,加入#会显示前缀
#include<stdio.h>
int main()
{
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;

}


//整数溢出
#include<stdio.h>
int main()
{
int i = 2147483647;
unsigned int j = 4294967295;

printf("%d %d %d\n",i,i+1,i+2);
printf("%u %u %u\n",j,j+1,j+2);
return 0;
}


//printf()更多的属性
#include<stdio.h>
int main()
{

unsigned int un = 3000000000;
short end = 200;
long big = 65537;
const __int64 verybig = 12345678908642;//long long 我的编译器不支持
printf("un = %u and not %d\n",un,un);
printf("end = %hd and %d\n",end,end);
printf("big = %ld and %hd\n",big,big);
printf("verybig= %lld and not %ld\n",verybig,verybig);
printf("%d\n",sizeof(__int64));

return 0;
}


//显示一个字符的编码值
#include<stdio.h>
int main()
{
char ch;
printf("Please enter a character.\n");
scanf("%c",&ch);
printf("The code for %c is %d.\n",ch,ch);
return 0;

}

//可移植的整数类型名
#include<stdio.h>
#include<inttypes.h>
int main(void)
{
int16_t me16;

me16 = 4593;
printf("Frist,assume int16_t is short:");
printf("me16 = %hd\n",me16);
printf("Next,let's not make any assumptions.\n" );
printf("Instead,use a \"macro\" from inttypes.h: ");
printf("me16 = %" PRID16"\n",me16);
return 0;

}

 

//%f 与%e的转换
#include<stdio.h>
int main()
{
float about = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;
printf("%f can be written %e\n",about,about);
printf("%f can be written %e\n",abet,abet);
printf("%f can be written %e\n",dip,dip);
return 0;

}

//float 在系统假设最大值为3.4E38时,下面代码会发生上溢
#include<stdio.h>
int main()
{
float toobig = 3.4E38 * 100.0f;
printf("%e\n",toobig);

 

}


//结果不等于1,因为计算机缺乏足够的进行正确运算所需的十进制位数。
//数字2.0e20为2后面加上20个零,如果对它加一改变的将是第21位,而float只有6,7位有效位数

#include<stdio.h>
int main()
{
float a,b;

b = 2.0e20 +1.0;
a = b - 2.0e20;
printf("%f \n",a);
return 0;

}

 


/*输出类型的大小*/
#include<stdio.h>
int main()
{
printf("Type int has a size of %lu 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;

}

 


//将浮点值变为整数,c简单地丢弃小数部分(截尾),而不进行四舍五入
#include<stdio.h>
int main()
{
int cost = 3.5415926;
printf("%d\n",cost);

}

 

/*
printf中参数不足和类型不匹配所造成的结果随平台的不同而不同。
%d显示float值不会进行转换,而是显示垃圾值,其他类型也一样
*/

#include<stdio.h>
int main()
{
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;


}

 

//转义字符
#include<stdio.h>
int main()
{
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;

}

posted @ 2017-04-10 22:08  Zhao_Xu_Jie  阅读(129)  评论(0编辑  收藏  举报