//程序清单3.1
#include<stdio.h>
int main(void)
{
float weight;
float value;
printf("Are you worth your weight in platinum?\n");
printf("Let's check it out.\n");
printf("Please enter your weight in pounds:");
scanf("%f",&weight);
value=1700.0*weight*14.5833;
printf("Your weight in platinum is worth $%.2f.\n",value);
printf("You are easily worth that!If platinum prices drop.\n");
printf("eat more to maintain your value.\n");
getchar();
getchar();
return 0;
}
//代码清单3.2 print1.c——演示printf()的一些特性
#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); //forgot 2 arguments
getchar();
return 0;
}
//代码清单3.3 bases.c——prints 100 in decimal,octal,and hex
#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);
getchar();
return 0;
}
/*toobig.c——超出系统允许的最大值*/
#include<stdio.h>
int main(void)
{
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);
getchar();
return 0;
}
/*代码清单3.4 print2.c——更多的printf()的特性*/
#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);
getchar();
return 0;
}
/*代码清单3.5 charcode.c——显示字符的代码编号*/
#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);
getchar();
getchar();
return 0;
}
/*代码清单3.7 show_pt.c——以两种方式显示float类型的值*/
#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\n",aboat,aboat);
//下一行要求编译器支持C99或其中相关特性
printf("And it's %a in hexadecimal,power of 2 notation\n",aboat);
printf("%f can be written %e\n",abet,abet);
printf("%Lf can be written %Le\n",dip,dip);
getchar();
return 0;
}
/* floaterr.c——演示舍入错误*/
#include<stdio.h>
int main(void)
{
float a,b;
b=2.0e20 + 1.0;
a=b-2.0e20;
printf("%f \n",a);
getchar();
return 0;
}
//程序清单3.8 typesize.c程序——打印类型大小
#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 long long has a size of %u bytes.\n",sizeof(long long));
printf("Type double has a size of %u bytes.\n",sizeof(double));
printf("Type long double has a size of %lu bytes.\n",sizeof(long double));
getchar();
return 0;
}
//程序清单3.10 escape.c程序——使用转义序列
#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 year.",salary,salary*12.0);
printf("\rGee!\n");
getchar();
getchar();
return 0;
}
网友评论