3.1 platinum.c
/* platinum.c -- your weight in platinum */
#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("Plese enter your weight in pounds: ");
/* 捕获用户的输入 */
scanf_s("%f", &weight);
/* 假设白金价值的加个是每盎司$1700 */
/* 14.5833 用于把英镑常衡盎司转换为金衡盎司 */
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 drops,n");
printf("eat more to maintain your value.n");
return 0;
}
3.3-1
/* toobing.c -- 超出系统的最大int值 */
#include<stdio.h>
int main(void)
{
int i = 2147483647;
unsigned int j = 4294967295;
printf("%d %d %dn", i, i + 1, i + 2);
printf("%u %u %un", j, j + 1, j + 2);
return 0;
}
3.4 print.c
/* print2.c -- 更多printf()的特性 */
#include<stdio.h>
int main(void)
{
unsigned int un = 3000000000; /* int为32位和short()为16位的系统 */
short end = 200;
long big = 65537;
long long verybig = 12345678908642;
printf("un = %u and not %dn", un, un);
printf("end = %hd and %dn", end, end);
printf("big = %ld and not %hdn", big, big);
printf("verybig = %lld and not %ldn", verybig, verybig);
return 0;
}
3.5 charcode.c
/* charcode.c - 显示字符的代码编号 */
#include<stdio.h>
int main(void)
{
char ch;
printf("Please enter a character.n");
scanf_s("%c", &ch); /* 用户输入字符 */
printf("The code for %c is %d.n", ch, ch);
return 0;
}
3.7 showf_pt.c
/* showf_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 %en", aboat, aboat);
// 下一行要求编译器支持C99或其中的相关特性
printf("And it's %a in hexadecimal, pwoers of 2 notationn", aboat);
printf("%f can be written %en", abet, abet);
printf("%Lf can be written %Len", dip, dip);
return 0;
}
3.8 typesize.c
/* typesize.c -- 打印类型大小 */
#include<stdio.h>
int main(void)
{
/* c99为类型大小提供%zd转换说明 */
printf("Type int has a size of %zd bytes.n", sizeof(int));
printf("Type char has a size of %zd bytes.n", sizeof(char));
printf("Type long has a size of %zd bytes.n", sizeof(long));
printf("Type long long has a size of %zd bytes.n", sizeof(long long));
printf("Type double has a size of %zd bytes.n", sizeof(double));
printf("Type long double has a size of %zd bytes.n", sizeof(long double));
return 0;
}
3.9 badcount.c
/* badcount.c -- 参数错误的情况 */
#include<stdio.h>
int main(void)
{
int n = 4;
int m = 5;
float f = 7.0f;
float g = 8.0f;
printf("%dn", n, m); /* 参数太多 */
printf("%d %d %dn", n); /* 参数太少 */
printf("%d %dn", f, g); /* 值的类型不匹配 */
return 0;
}
3.10 escape.c
/* escape.c -- 使用转义序列 */
#include<stdio.h>
int main(void)
{
float salary;
printf("aEnter your desired monthly salary:"); /* 1 */
printf("$________bbbbbbbb"); /* 2 */
scanf_s("%f", &salary);
printf("nt$%.2f a month is $%.2f a year.", salary, salary * 12.0); /* 3 */
printf("rGee!n"); /* 4 */
return 0;
}